From 60cfbf5b5bea2f1248793e9b2239a3beb3c7cb2e Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Thu, 25 Aug 2022 14:53:58 +0530 Subject: [PATCH] replication: remove annotation from pvc As part of #213 support was added to have one VR per PVC, once the VR is deleted we need to remove the annotation so new VR can be created for the same PVC. This PR adds the missing piece. Signed-off-by: Madhu Rajanna --- controllers/replication.storage/pvc.go | 15 ++++++++++++++ controllers/replication.storage/pvc_test.go | 20 +++++++++++++++++++ .../volumereplication_controller.go | 7 +++++++ 3 files changed, 42 insertions(+) diff --git a/controllers/replication.storage/pvc.go b/controllers/replication.storage/pvc.go index 59ccda47d..f466e2465 100644 --- a/controllers/replication.storage/pvc.go +++ b/controllers/replication.storage/pvc.go @@ -91,3 +91,18 @@ func (r *VolumeReplicationReconciler) annotatePVCWithOwner(ctx context.Context, return nil } + +// removeOwnerFromPVCAnnotation removes the VolumeReplication owner from the PVC annotations. +func (r *VolumeReplicationReconciler) removeOwnerFromPVCAnnotation(ctx context.Context, logger logr.Logger, pvc *corev1.PersistentVolumeClaim) error { + if _, ok := pvc.ObjectMeta.Annotations[replicationv1alpha1.VolumeReplicationNameAnnotation]; ok { + logger.Info("removing owner annotation from PersistentVolumeClaim object", "Annotation", replicationv1alpha1.VolumeReplicationNameAnnotation) + delete(pvc.ObjectMeta.Annotations, replicationv1alpha1.VolumeReplicationNameAnnotation) + if err := r.Client.Update(ctx, pvc); err != nil { + return fmt.Errorf("failed to remove annotation %q from PersistentVolumeClaim "+ + "%q %w", + replicationv1alpha1.VolumeReplicationNameAnnotation, pvc.Name, err) + } + } + + return nil +} diff --git a/controllers/replication.storage/pvc_test.go b/controllers/replication.storage/pvc_test.go index 84437819c..416185372 100644 --- a/controllers/replication.storage/pvc_test.go +++ b/controllers/replication.storage/pvc_test.go @@ -236,5 +236,25 @@ func TestVolumeReplicationReconciler_annotatePVCWithOwner(t *testing.T) { } else { assert.NoError(t, err) } + + err = reconciler.removeOwnerFromPVCAnnotation(context.TODO(), log.FromContext(context.TODO()), testPVC) + assert.NoError(t, err) + + // try calling delete again, it should not fail + err = reconciler.removeOwnerFromPVCAnnotation(context.TODO(), log.FromContext(context.TODO()), testPVC) + assert.NoError(t, err) + + } + + // try removeOwnerFromPVCAnnotation for empty map + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pvc-name", + Namespace: mockNamespace, + }, } + volumeReplication := &replicationv1alpha1.VolumeReplication{} + reconciler := createFakeVolumeReplicationReconciler(t, pvc, volumeReplication) + err := reconciler.removeOwnerFromPVCAnnotation(context.TODO(), log.FromContext(context.TODO()), pvc) + assert.NoError(t, err) } diff --git a/controllers/replication.storage/volumereplication_controller.go b/controllers/replication.storage/volumereplication_controller.go index cfbf0dbd3..d27e3363d 100644 --- a/controllers/replication.storage/volumereplication_controller.go +++ b/controllers/replication.storage/volumereplication_controller.go @@ -219,6 +219,13 @@ func (r *VolumeReplicationReconciler) Reconcile(ctx context.Context, req ctrl.Re return ctrl.Result{}, err } + + if err = r.removeOwnerFromPVCAnnotation(ctx, logger, pvc); err != nil { + logger.Error(err, "Failed to remove VolumeReplication annotation from PersistentVolumeClaim") + + return reconcile.Result{}, err + } + if err = r.removeFinalizerFromPVC(logger, pvc); err != nil { logger.Error(err, "Failed to remove PersistentVolumeClaim finalizer")