Skip to content

Commit

Permalink
replication: remove annotation from pvc
Browse files Browse the repository at this point in the history
As part of red-hat-storage#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 <madhupr007@gmail.com>
  • Loading branch information
Madhu-1 authored and openshift-cherrypick-robot committed Aug 30, 2022
1 parent 85fb1fd commit 985f6a5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions controllers/replication.storage/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions controllers/replication.storage/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 985f6a5

Please sign in to comment.