Skip to content

Commit

Permalink
Merge pull request #210 from umangachapagain/flatten-vrc
Browse files Browse the repository at this point in the history
create VRC with flattenMode=force
  • Loading branch information
openshift-merge-bot[bot] authored Jun 5, 2024
2 parents 65be851 + b501cd9 commit 570778d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
62 changes: 46 additions & 16 deletions controllers/drpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ import (
)

const (
DefaultMirroringMode = "snapshot"
MirroringModeKey = "mirroringMode"
SchedulingIntervalKey = "schedulingInterval"
ReplicationSecretNameKey = "replication.storage.openshift.io/replication-secret-name"
ReplicationSecretNamespaceKey = "replication.storage.openshift.io/replication-secret-namespace"
ReplicationIDKey = "replicationid"
RBDVolumeReplicationClassNameTemplate = "rbd-volumereplicationclass-%v"
RBDReplicationSecretName = "rook-csi-rbd-provisioner"
RamenLabelTemplate = "ramendr.openshift.io/%s"
RBDProvisionerTemplate = "%s.rbd.csi.ceph.com"
DefaultMirroringMode = "snapshot"
MirroringModeKey = "mirroringMode"
SchedulingIntervalKey = "schedulingInterval"
ReplicationSecretNameKey = "replication.storage.openshift.io/replication-secret-name"
ReplicationSecretNamespaceKey = "replication.storage.openshift.io/replication-secret-namespace"
ReplicationIDKey = "replicationid"
RBDVolumeReplicationClassNameTemplate = "rbd-volumereplicationclass-%v"
RBDReplicationSecretName = "rook-csi-rbd-provisioner"
RamenLabelTemplate = "ramendr.openshift.io/%s"
RBDProvisionerTemplate = "%s.rbd.csi.ceph.com"
RBDFlattenVolumeReplicationClassNameTemplate = "rbd-flatten-volumereplicationclass-%v"
RBDFlattenVolumeReplicationClassLabelKey = "replication.storage.openshift.io/flatten-mode"
RBDFlattenVolumeReplicationClassLabelValue = "force"
RBDVolumeReplicationClassDefaultAnnotation = "replication.storage.openshift.io/is-default-class"
)

type DRPolicyReconciler struct {
Expand Down Expand Up @@ -126,8 +130,9 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
return err
}

manifestWorkName := fmt.Sprintf("vrc-%v", utils.FnvHash(dp.Name)) // Two ManifestWork per DRPolicy

for _, pr := range mp.Spec.Items {
manifestWorkName := fmt.Sprintf("vrc-%v", utils.FnvHash(dp.Name)) // Two ManifestWork per DRPolicy
found := &workv1.ManifestWork{
ObjectMeta: metav1.ObjectMeta{
Name: manifestWorkName,
Expand All @@ -144,6 +149,7 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
klog.Error(err, "failed to get ManifestWork: %s", manifestWorkName)
return err
}

interval := dp.Spec.SchedulingInterval
params := make(map[string]string)
params[MirroringModeKey] = DefaultMirroringMode
Expand All @@ -161,6 +167,9 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
ObjectMeta: metav1.ObjectMeta{
Name: vrcName,
Labels: labels,
Annotations: map[string]string{
RBDVolumeReplicationClassDefaultAnnotation: "true",
},
},
Spec: replicationv1alpha1.VolumeReplicationClassSpec{
Parameters: params,
Expand All @@ -173,8 +182,30 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
return fmt.Errorf("failed to marshal %v to JSON, error %w", vrc, err)
}

manifest := workv1.Manifest{}
manifest.RawExtension = runtime.RawExtension{Raw: objJson}
manifestList := []workv1.Manifest{
{
RawExtension: runtime.RawExtension{
Raw: objJson,
},
},
}

if dp.Spec.ReplicationClassSelector.MatchLabels[RBDFlattenVolumeReplicationClassLabelKey] == RBDFlattenVolumeReplicationClassLabelValue {
vrcFlatten := vrc.DeepCopy()
vrcFlatten.Name = fmt.Sprintf(RBDFlattenVolumeReplicationClassNameTemplate, utils.FnvHash(interval))
vrcFlatten.Labels[RBDFlattenVolumeReplicationClassLabelKey] = RBDFlattenVolumeReplicationClassLabelValue
vrcFlatten.Annotations = map[string]string{}
vrcFlatten.Spec.Parameters["flattenMode"] = "force"
vrcFlattenJson, err := json.Marshal(vrcFlatten)
if err != nil {
return fmt.Errorf("failed to marshal %v to JSON, error %w", vrcFlatten, err)
}
manifestList = append(manifestList, workv1.Manifest{
RawExtension: runtime.RawExtension{
Raw: vrcFlattenJson,
},
})
}

mw := workv1.ManifestWork{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -193,9 +224,8 @@ func (r *DRPolicyReconciler) createOrUpdateManifestWorkForVRC(ctx context.Contex
_, err = controllerutil.CreateOrUpdate(ctx, r.HubClient, &mw, func() error {
mw.Spec = workv1.ManifestWorkSpec{
Workload: workv1.ManifestsTemplate{
Manifests: []workv1.Manifest{
manifest,
}},
Manifests: manifestList,
},
}
return nil
})
Expand Down
8 changes: 8 additions & 0 deletions controllers/drpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func TestDRPolicyReconcile(t *testing.T) {
Spec: ramenv1alpha1.DRPolicySpec{
SchedulingInterval: "1h",
DRClusters: []string{cName1, cName2},
ReplicationClassSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
RBDFlattenVolumeReplicationClassLabelKey: RBDFlattenVolumeReplicationClassLabelValue,
},
},
},
}

Expand Down Expand Up @@ -88,6 +93,9 @@ func TestDRPolicyReconcile(t *testing.T) {
if err != nil {
t.Errorf("Failed to get ManifestWork. Error: %s", err)
}
if len(found.Spec.Workload.Manifests) < 2 {
t.Errorf("Expected at least 2 VRC")
}
}
}

Expand Down

0 comments on commit 570778d

Please sign in to comment.