diff --git a/controllers/replication.storage/pvc.go b/controllers/replication.storage/pvc.go index 59ccda47d..ba8bd8f23 100644 --- a/controllers/replication.storage/pvc.go +++ b/controllers/replication.storage/pvc.go @@ -91,3 +91,19 @@ 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 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")