Skip to content

Commit 9320b5e

Browse files
BenamarMkShyamsundarR
authored andcommitted
Refactor pvcExists and deleteAnnotation based on code review
Signed-off-by: Benamar Mekhissi <[email protected]>
1 parent d82d155 commit 9320b5e

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

controllers/volsync/vshandler.go

+8-33
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,8 @@ func (v *VSHandler) PreparePVC(pvcName string, prepFinalSync, copyMethodDirect b
505505
return nil
506506
}
507507

508-
// This doesn't need to specifically be in VSHandler - could be useful for non-volsync scenarios?
509-
// Will look at annotations on the PVC, make sure the reconcile option from ACM is set to merge (or not exists)
510-
// and then will remove ACM annotations and also add VRG as the owner. This is to break the connection between
511-
// the appsub and the PVC itself. This way we can proceed to remove the app without the PVC being removed.
512-
// We need the PVC left behind for running the final sync or for CopyMethod Direct.
508+
// TakePVCOwnership adds do-not-delete annotation to indicate that ACM should not delete/cleanup this pvc
509+
// when the appsub is removed and adds VRG as owner so the PVC is garbage collected when the VRG is deleted.
513510
func (v *VSHandler) TakePVCOwnership(pvcName string) (bool, error) {
514511
l := v.log.WithValues("pvcName", pvcName)
515512

@@ -562,23 +559,6 @@ func (v *VSHandler) pvcExistsAndInUse(pvcName string, inUsePodMustBeReady bool)
562559
return util.IsPVAttachedToNode(v.ctx, v.client, v.log, pvc)
563560
}
564561

565-
func (v *VSHandler) pvcExists(pvcName string) (bool, *corev1.PersistentVolumeClaim, error) {
566-
pvc, err := v.getPVC(pvcName)
567-
if err != nil {
568-
if !kerrors.IsNotFound(err) {
569-
v.log.V(1).Info("failed to get PVC")
570-
571-
return false, pvc, err
572-
}
573-
574-
return false, pvc, nil
575-
}
576-
577-
v.log.V(1).Info("PVC found")
578-
579-
return true, pvc, nil
580-
}
581-
582562
func (v *VSHandler) getPVC(pvcName string) (*corev1.PersistentVolumeClaim, error) {
583563
pvc := &corev1.PersistentVolumeClaim{}
584564

@@ -588,7 +568,7 @@ func (v *VSHandler) getPVC(pvcName string) (*corev1.PersistentVolumeClaim, error
588568
Namespace: v.owner.GetNamespace(),
589569
}, pvc)
590570
if err != nil {
591-
return pvc, fmt.Errorf("%w", err)
571+
return nil, fmt.Errorf("%w", err)
592572
}
593573

594574
return pvc, nil
@@ -917,6 +897,7 @@ func (v *VSHandler) EnsurePVCfromRD(rdSpec ramendrv1alpha1.VolSyncReplicationDes
917897
return v.validateSnapshotAndEnsurePVC(rdSpec, *vsImageRef, failoverAction)
918898
}
919899

900+
//nolint:cyclop
920901
func (v *VSHandler) EnsurePVCforDirectCopy(ctx context.Context,
921902
rdSpec ramendrv1alpha1.VolSyncReplicationDestinationSpec,
922903
) error {
@@ -930,12 +911,12 @@ func (v *VSHandler) EnsurePVCforDirectCopy(ctx context.Context,
930911
return fmt.Errorf("capacity must be provided %v", rdSpec.ProtectedPVC)
931912
}
932913

933-
exists, pvc, err := v.pvcExists(rdSpec.ProtectedPVC.Name)
934-
if err != nil {
914+
pvc, err := v.getPVC(rdSpec.ProtectedPVC.Name)
915+
if err != nil && !kerrors.IsNotFound(err) {
935916
return err
936917
}
937918

938-
if exists {
919+
if pvc != nil {
939920
return v.removeOCMAnnotationsAndUpdate(pvc)
940921
}
941922

@@ -2110,17 +2091,11 @@ func (v *VSHandler) DisownVolSyncManagedPVC(pvc *corev1.PersistentVolumeClaim) e
21102091
// TODO: Remove just the VRG ownerReference instead of blindly removing all ownerreferences.
21112092
// For now, this is fine, given that the VRG is the sole owner of the PVC after DR is enabled.
21122093
pvc.ObjectMeta.OwnerReferences = nil
2113-
v.deleteAnnotation(pvc, ACMAppSubDoNotDeleteAnnotation)
2094+
delete(pvc.Annotations, ACMAppSubDoNotDeleteAnnotation)
21142095

21152096
return v.client.Update(v.ctx, pvc)
21162097
}
21172098

2118-
func (v *VSHandler) deleteAnnotation(obj client.Object, annotationName string) {
2119-
annotations := obj.GetAnnotations()
2120-
delete(annotations, annotationName)
2121-
obj.SetAnnotations(annotations)
2122-
}
2123-
21242099
func (v *VSHandler) addBackOCMAnnotationsAndUpdate(obj client.Object, annotations map[string]string) error {
21252100
updatedAnnotations := obj.GetAnnotations()
21262101

0 commit comments

Comments
 (0)