diff --git a/internal/controller/volumereplicationgroup_controller.go b/internal/controller/volumereplicationgroup_controller.go index 0cd2719c0..e238cabab 100644 --- a/internal/controller/volumereplicationgroup_controller.go +++ b/internal/controller/volumereplicationgroup_controller.go @@ -504,6 +504,9 @@ const ( // StorageClass label StorageIDLabel = "ramendr.openshift.io/storageid" + // Consistency group label + ConsistencyGroupLabel = "ramendr.openshift.io/consistency-group" + // VolumeReplicationClass label VolumeReplicationIDLabel = "ramendr.openshift.io/replicationid" @@ -548,6 +551,10 @@ func (v *VRGInstance) processVRG() ctrl.Result { return v.invalid(err, "Failed to process list of PVCs to protect", true) } + if err := v.labelPVCsForCG(); err != nil { + return v.invalid(err, "Failed to label PVCs for consistency groups", true) + } + v.log = v.log.WithName("vrginstance").WithValues("State", v.instance.Spec.ReplicationState) v.s3StoreAccessorsGet() @@ -694,6 +701,54 @@ func (v *VRGInstance) updatePVCList() error { return v.separatePVCsUsingStorageClassProvisioner(pvcList) } +func (v *VRGInstance) labelPVCsForCG() error { + if v.instance.Spec.Async == nil { + return nil + } + + if !rmnutil.IsCGEnabled(v.instance.GetAnnotations()) { + return nil + } + + for idx := range v.volRepPVCs { + pvc := &v.volRepPVCs[idx] + + if err := v.addConsistencyGroupLabel(pvc); err != nil { + return fmt.Errorf("failed to label PVC %s/%s for consistency group (%w)", + pvc.GetNamespace(), pvc.GetName(), err) + } + } + + return nil +} + +func (v *VRGInstance) addConsistencyGroupLabel(pvc *corev1.PersistentVolumeClaim) error { + scName := pvc.Spec.StorageClassName + + if scName == nil || *scName == "" { + return fmt.Errorf("missing storage class name for PVC %s/%s", pvc.GetNamespace(), pvc.GetName()) + } + + storageClass := &storagev1.StorageClass{} + if err := v.reconciler.Get(v.ctx, types.NamespacedName{Name: *scName}, storageClass); err != nil { + v.log.Info(fmt.Sprintf("Failed to get the storageclass %s", *scName)) + + return fmt.Errorf("failed to get the storageclass with name %s (%w)", *scName, err) + } + + storageID, ok := storageClass.GetLabels()[StorageIDLabel] + if !ok { + v.log.Info("Missing storageID for PVC %s/%s", pvc.GetNamespace(), pvc.GetName()) + + return fmt.Errorf("missing storageID for PVC %s/%s", pvc.GetNamespace(), pvc.GetName()) + } + + // Add label for PVC, showing that this PVC is part of consistency group + return rmnutil.NewResourceUpdater(pvc). + AddLabel(ConsistencyGroupLabel, storageID). + Update(v.ctx, v.reconciler.Client) +} + func (v *VRGInstance) updateReplicationClassList() error { if v.vrcUpdated { return nil diff --git a/internal/controller/vrg_volsync.go b/internal/controller/vrg_volsync.go index c9c1f9a92..59023bc37 100644 --- a/internal/controller/vrg_volsync.go +++ b/internal/controller/vrg_volsync.go @@ -17,9 +17,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// ConsistencyGroupLabel (TODO) use ConsistencyGroupLabel in PR https://github.com/RamenDR/ramen/pull/1472/files -var ConsistencyGroupLabel = "ramendr.openshift.io/consistency-group" - //nolint:gocognit,funlen,cyclop func (v *VRGInstance) restorePVsAndPVCsForVolSync() (int, error) { v.log.Info("VolSync: Restoring VolSync PVs")