Skip to content

Commit

Permalink
replication: support for volumegroup replication
Browse files Browse the repository at this point in the history
update the RPC calls to set the
replication source for group replication
and add support for volumegroup replication

Signed-off-by: Madhu Rajanna <[email protected]>
  • Loading branch information
Madhu-1 committed Jul 3, 2024
1 parent 95aacc3 commit f7ac9e8
Show file tree
Hide file tree
Showing 12 changed files with 929 additions and 259 deletions.
20 changes: 11 additions & 9 deletions cmd/csi-addons/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ const (
// command contains the parsed arguments that were passed while running the
// executable.
type command struct {
endpoint string
stagingPath string
operation string
persistentVolume string
drivername string
secret string
cidrs string
clusterid string
legacy bool
endpoint string
stagingPath string
operation string
persistentVolume string
volumeGroupReplicationContent string
drivername string
secret string
cidrs string
clusterid string
legacy bool
}

// cmd is the single instance of the command struct, used inside main().
Expand All @@ -58,6 +59,7 @@ func init() {
flag.StringVar(&cmd.stagingPath, "stagingpath", stagingPath, "staging path")
flag.StringVar(&cmd.operation, "operation", "", "csi-addons operation")
flag.StringVar(&cmd.persistentVolume, "persistentvolume", "", "name of the PersistentVolume")
flag.StringVar(&cmd.volumeGroupReplicationContent, "volumegroupreplicaitoncontent", "", "name of the VolumeGroupReplicationContent")
flag.StringVar(&cmd.drivername, "drivername", "", "name of the CSI driver")
flag.StringVar(&cmd.secret, "secret", "", "kubernetes secret in the format `namespace/name`")
flag.StringVar(&cmd.cidrs, "cidrs", "", "comma separated list of cidrs")
Expand Down
112 changes: 87 additions & 25 deletions cmd/csi-addons/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type VolumeReplicationBase struct {
secretName string
secretNamespace string
volumeID string
groupID string
}

func (rep *VolumeReplicationBase) Init(c *command) error {
Expand All @@ -59,19 +60,34 @@ func (rep *VolumeReplicationBase) Init(c *command) error {
return errors.New("secret name is not set")
}

pv, err := getKubernetesClient().CoreV1().PersistentVolumes().Get(context.Background(), c.persistentVolume, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get pv %q", c.persistentVolume)
}

if pv.Spec.CSI == nil {
return fmt.Errorf("pv %q is not a CSI volume", c.persistentVolume)
if c.persistentVolume != "" && c.volumeGroupReplicationContent != "" {
return errors.New("only one of persistentVolume or volumeGroupReplicationContent should be set")
}
if c.persistentVolume != "" {
pv, err := getKubernetesClient().CoreV1().PersistentVolumes().Get(context.Background(), c.persistentVolume, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get pv %q", c.persistentVolume)
}

if pv.Spec.CSI == nil {
return fmt.Errorf("pv %q is not a CSI volume", c.persistentVolume)
}

if pv.Spec.CSI.VolumeHandle == "" {
return errors.New("volume ID is not set")
}
rep.volumeID = pv.Spec.CSI.VolumeHandle
} else if c.volumeGroupReplicationContent != "" {
vgrc, err := getVolumeReplicationClient().getVolumeGroupReplicationContent(context.Background(), c.volumeGroupReplicationContent, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get VolumeGroupReplicationContent %q", c.volumeGroupReplicationContent)
}
if vgrc.Spec.VolumeGroupReplicationHandle == "" {
return errors.New("volume group ID is not set")
}
rep.groupID = vgrc.Spec.VolumeGroupReplicationHandle

if pv.Spec.CSI.VolumeHandle == "" {
return errors.New("volume ID is not set")
}
rep.volumeID = pv.Spec.CSI.VolumeHandle

return nil
}
Expand All @@ -81,6 +97,35 @@ type EnableVolumeReplication struct {
VolumeReplicationBase
}

func (v VolumeReplicationBase) setReplicationSource(req *proto.ReplicationSource) error {
if req == nil {
return errors.New("replication source is not set")
}
if v.volumeID == "" && v.groupID == "" {
return errors.New("volumeID or groupID is not set")
}
if v.volumeID != "" && v.groupID != "" {
return errors.New("only one of volumeID or groupID should be set")
}
if v.volumeID != "" {
req.Type = &proto.ReplicationSource_Volume{
Volume: &proto.ReplicationSource_VolumeSource{
VolumeId: v.volumeID,
},
}
}
if v.groupID != "" {
req = &proto.ReplicationSource{
Type: &proto.ReplicationSource_Volumegroup{
Volumegroup: &proto.ReplicationSource_VolumeGroupSource{
VolumeGroupId: v.groupID,
},
},
}
}
return nil
}

var _ = registerOperation("EnableVolumeReplication", &EnableVolumeReplication{})

func (rep *EnableVolumeReplication) Execute() error {
Expand All @@ -91,10 +136,12 @@ func (rep *EnableVolumeReplication) Execute() error {
req := &proto.EnableVolumeReplicationRequest{
SecretName: rep.secretName,
SecretNamespace: rep.secretNamespace,
VolumeId: rep.volumeID,
}

_, err := rs.EnableVolumeReplication(context.TODO(), req)
err := rep.setReplicationSource(req.ReplicationSource)
if err != nil {
return err
}
_, err = rs.EnableVolumeReplication(context.TODO(), req)
if err != nil {
return err
}
Expand All @@ -119,10 +166,13 @@ func (rep *DisableVolumeReplication) Execute() error {
req := &proto.DisableVolumeReplicationRequest{
SecretName: rep.secretName,
SecretNamespace: rep.secretNamespace,
VolumeId: rep.volumeID,
}
err := rep.setReplicationSource(req.ReplicationSource)
if err != nil {
return err
}

_, err := rs.DisableVolumeReplication(context.TODO(), req)
_, err = rs.DisableVolumeReplication(context.TODO(), req)
if err != nil {
return err
}
Expand All @@ -147,10 +197,12 @@ func (rep *PromoteVolume) Execute() error {
req := &proto.PromoteVolumeRequest{
SecretName: rep.secretName,
SecretNamespace: rep.secretNamespace,
VolumeId: rep.volumeID,
}

_, err := rs.PromoteVolume(context.TODO(), req)
err := rep.setReplicationSource(req.ReplicationSource)
if err != nil {
return err
}
_, err = rs.PromoteVolume(context.TODO(), req)
if err != nil {
return err
}
Expand All @@ -175,10 +227,12 @@ func (rep *DemoteVolume) Execute() error {
req := &proto.DemoteVolumeRequest{
SecretName: rep.secretName,
SecretNamespace: rep.secretNamespace,
VolumeId: rep.volumeID,
}

_, err := rs.DemoteVolume(context.TODO(), req)
err := rep.setReplicationSource(req.ReplicationSource)
if err != nil {
return err
}
_, err = rs.DemoteVolume(context.TODO(), req)
if err != nil {
return err
}
Expand All @@ -203,10 +257,12 @@ func (rep *ResyncVolume) Execute() error {
req := &proto.ResyncVolumeRequest{
SecretName: rep.secretName,
SecretNamespace: rep.secretNamespace,
VolumeId: rep.volumeID,
}

_, err := rs.ResyncVolume(context.TODO(), req)
err := rep.setReplicationSource(req.ReplicationSource)
if err != nil {
return err
}
_, err = rs.ResyncVolume(context.TODO(), req)
if err != nil {
return err
}
Expand All @@ -231,7 +287,13 @@ func (rep *GetVolumeReplicationInfo) Execute() error {
req := &proto.GetVolumeReplicationInfoRequest{
SecretName: rep.secretName,
SecretNamespace: rep.secretNamespace,
VolumeId: rep.volumeID,
ReplicationSource: &proto.ReplicationSource{
Type: &proto.ReplicationSource_Volume{
Volume: &proto.ReplicationSource_VolumeSource{
VolumeId: rep.volumeID,
},
},
},
}

res, err := rs.GetVolumeReplicationInfo(context.TODO(), req)
Expand Down
71 changes: 71 additions & 0 deletions cmd/csi-addons/replicationClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2024 The Ceph-CSI Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"

replicationv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/apis/replication.storage/v1alpha1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)

type replicationClient struct {
restClient *rest.RESTClient
}


func getVolumeReplicationClient() *replicationClient {
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
scheme, err := replicationv1alpha1.SchemeBuilder.Build()
if err != nil {
panic(err.Error())
}

crdConfig := *config
crdConfig.ContentConfig.GroupVersion = &replicationv1alpha1.GroupVersion
crdConfig.APIPath = "/apis"
crdConfig.NegotiatedSerializer = serializer.NewCodecFactory(scheme)
crdConfig.UserAgent = rest.DefaultKubernetesUserAgent()

restClient, err := rest.UnversionedRESTClientFor(&crdConfig)
if err != nil {
panic(err)
}

return &replicationClient{restClient: restClient}
}

func (r *replicationClient) getVolumeGroupReplicationContent(ctx context.Context, name string, opts metav1.GetOptions) (*replicationv1alpha1.VolumeGroupReplicationContent, error) {
result := replicationv1alpha1.VolumeGroupReplicationContent{}
err := r.restClient.
Get().
Namespace("").
Resource("volumegroupreplicationcontents").
Name(name).
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)

return &result, err
}
32 changes: 32 additions & 0 deletions controllers/replication.storage/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
const (
volumeReplicationFinalizer = "replication.storage.openshift.io"
pvcReplicationFinalizer = "replication.storage.openshift.io/pvc-protection"
vgrReplicationFinalizer = "replication.storage.openshift.io/vgr-protection"
)

// addFinalizerToVR adds the VR finalizer on the VolumeReplication instance.
Expand Down Expand Up @@ -94,3 +95,34 @@ func (r *VolumeReplicationReconciler) removeFinalizerFromPVC(logger logr.Logger,

return nil
}

// addFinalizerToVGR adds the VR finalizer on the VolumeGroupReplication.
func (r *VolumeReplicationReconciler) addFinalizerToVGR(logger logr.Logger, vgr *replicationv1alpha1.VolumeGroupReplication) error {
if !slices.Contains(vgr.ObjectMeta.Finalizers, vgrReplicationFinalizer) {
logger.Info("adding finalizer to VolumeGroupReplication object", "Finalizer", vgrReplicationFinalizer)
vgr.ObjectMeta.Finalizers = append(vgr.ObjectMeta.Finalizers, vgrReplicationFinalizer)
if err := r.Client.Update(context.TODO(), vgr); err != nil {
return fmt.Errorf("failed to add finalizer (%s) to VolumeGroupReplication resource"+
" (%s/%s) %w",
vgrReplicationFinalizer, vgr.Namespace, vgr.Name, err)
}
}

return nil
}

// removeFinalizerFromPVC removes the VR finalizer on VolumeGroupReplication.
func (r *VolumeReplicationReconciler) removeFinalizerFromVGR(logger logr.Logger, vgr *replicationv1alpha1.VolumeGroupReplication,
) error {
if slices.Contains(vgr.ObjectMeta.Finalizers, vgrReplicationFinalizer) {
logger.Info("removing finalizer from VolumeGroupReplication object", "Finalizer", vgrReplicationFinalizer)
vgr.ObjectMeta.Finalizers = util.RemoveFromSlice(vgr.ObjectMeta.Finalizers, vgrReplicationFinalizer)
if err := r.Client.Update(context.TODO(), vgr); err != nil {
return fmt.Errorf("failed to remove finalizer (%s) from VolumeGroupReplication resource"+
" (%s/%s), %w",
vgrReplicationFinalizer, vgr.Namespace, vgr.Name, err)
}
}

return nil
}
Loading

0 comments on commit f7ac9e8

Please sign in to comment.