Skip to content

Commit 8178d55

Browse files
ELENAGERraghavendra-talur
authored andcommitted
Add generic helper function ResourceIsDeleted
Signed-off-by: Elena Gershkovich <[email protected]>
1 parent b76fe12 commit 8178d55

10 files changed

+36
-34
lines changed

controllers/drcluster_controller.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (r *DRClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
321321

322322
u.initializeStatus()
323323

324-
if drClusterIsDeleted(drcluster) {
324+
if util.ResourceIsDeleted(drcluster) {
325325
return r.processDeletion(u)
326326
}
327327

@@ -387,11 +387,6 @@ func (r DRClusterReconciler) processCreateOrUpdate(u *drclusterInstance) (ctrl.R
387387
return ctrl.Result{Requeue: requeue || u.requeue}, reconcileError
388388
}
389389

390-
// Return true if dr cluster was marked for deletion.
391-
func drClusterIsDeleted(c *ramen.DRCluster) bool {
392-
return !c.GetDeletionTimestamp().IsZero()
393-
}
394-
395390
func (u *drclusterInstance) initializeStatus() {
396391
// Save a copy of the instance status to be used for the DRCluster status update comparison
397392
u.object.Status.DeepCopyInto(&u.savedInstanceStatus)
@@ -1005,7 +1000,7 @@ func getPeerFromPolicy(ctx context.Context, reconciler *DRClusterReconciler, log
10051000
continue
10061001
}
10071002

1008-
if !peerCluster.ObjectMeta.DeletionTimestamp.IsZero() {
1003+
if util.ResourceIsDeleted(peerCluster) {
10091004
log.Info(fmt.Sprintf("peer cluster %s of cluster %s is being deleted",
10101005
peerCluster.Name, drCluster.Name))
10111006
// for now continue. We just need to get one DRCluster with

controllers/drcluster_mmode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (u *drclusterInstance) pruneMModeMCV(
351351

352352
// Do not prune if it is part of survivors (or if already pruned)
353353
key := util.ClusterScopedResourceNameFromMCVName(inMModeMCV.GetName())
354-
if _, ok := survivors[key]; ok || !inMModeMCV.GetDeletionTimestamp().IsZero() {
354+
if _, ok := survivors[key]; ok || util.ResourceIsDeleted(inMModeMCV) {
355355
u.log.Info("Skipping pruning view", "name", inMModeMCV.GetName())
356356

357357
return nil

controllers/drplacementcontrol.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ func (d *DRPCInstance) vrgExistsAndPrimary(targetCluster string) bool {
12751275
return false
12761276
}
12771277

1278-
if !vrg.GetDeletionTimestamp().IsZero() {
1278+
if rmnutil.ResourceIsDeleted(vrg) {
12791279
return false
12801280
}
12811281

@@ -1804,7 +1804,7 @@ func (d *DRPCInstance) ensureVRGManifestWorkOnClusterDeleted(clusterName string)
18041804
return !done, fmt.Errorf("failed to retrieve ManifestWork (%w)", err)
18051805
}
18061806

1807-
if !mw.GetDeletionTimestamp().IsZero() {
1807+
if rmnutil.ResourceIsDeleted(mw) {
18081808
d.log.Info("Waiting for VRG MW to be fully deleted", "cluster", clusterName)
18091809
// As long as the Manifestwork still exist, then we are not done
18101810
return !done, nil

controllers/drplacementcontrol_controller.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func DRClusterUpdateOfInterest(oldDRCluster, newDRCluster *rmn.DRCluster) bool {
307307
}
308308

309309
// Exhausted all failover activation checks, the only interesting update is deleting a drcluster.
310-
return drClusterIsDeleted(newDRCluster)
310+
return rmnutil.ResourceIsDeleted(newDRCluster)
311311
}
312312

313313
// checkFailoverActivation checks if provided provisioner and storage instance is activated as per the
@@ -354,7 +354,7 @@ func (r *DRPlacementControlReconciler) FilterDRCluster(drcluster *rmn.DRCluster)
354354

355355
var err error
356356

357-
if drClusterIsDeleted(drcluster) {
357+
if rmnutil.ResourceIsDeleted(drcluster) {
358358
drpcCollections, err = DRPCsUsingDRCluster(r.Client, log, drcluster)
359359
} else {
360360
drpcCollections, err = DRPCsFailingOverToCluster(r.Client, log, drcluster.GetName())
@@ -703,7 +703,7 @@ func (r *DRPlacementControlReconciler) Reconcile(ctx context.Context, req ctrl.R
703703
ensureDRPCConditionsInited(&drpc.Status.Conditions, drpc.Generation, "Initialization")
704704

705705
placementObj, err := getPlacementOrPlacementRule(ctx, r.Client, drpc, logger)
706-
if err != nil && !(errors.IsNotFound(err) && !drpc.GetDeletionTimestamp().IsZero()) {
706+
if err != nil && !(errors.IsNotFound(err) && rmnutil.ResourceIsDeleted(drpc)) {
707707
r.recordFailure(ctx, drpc, placementObj, "Error", err.Error(), logger)
708708

709709
return ctrl.Result{}, err
@@ -942,8 +942,8 @@ func (r *DRPlacementControlReconciler) createDRPCMetricsInstance(
942942

943943
// isBeingDeleted returns true if either DRPC, user placement, or both are being deleted
944944
func isBeingDeleted(drpc *rmn.DRPlacementControl, usrPl client.Object) bool {
945-
return !drpc.GetDeletionTimestamp().IsZero() ||
946-
(usrPl != nil && !usrPl.GetDeletionTimestamp().IsZero())
945+
return rmnutil.ResourceIsDeleted(drpc) ||
946+
(usrPl != nil && rmnutil.ResourceIsDeleted(usrPl))
947947
}
948948

949949
func (r *DRPlacementControlReconciler) reconcileDRPCInstance(d *DRPCInstance, log logr.Logger) (ctrl.Result, error) {
@@ -994,7 +994,7 @@ func (r *DRPlacementControlReconciler) getAndEnsureValidDRPolicy(ctx context.Con
994994
return nil, fmt.Errorf("failed to get DRPolicy %w", err)
995995
}
996996

997-
if !drPolicy.ObjectMeta.DeletionTimestamp.IsZero() {
997+
if rmnutil.ResourceIsDeleted(drPolicy) {
998998
// If drpolicy is deleted then return
999999
// error to fail drpc reconciliation
10001000
return nil, fmt.Errorf("drPolicy '%s' referred by the DRPC is deleted, DRPC reconciliation would fail",
@@ -1392,7 +1392,7 @@ func getPlacement(ctx context.Context, k8sclient client.Client,
13921392
func (r *DRPlacementControlReconciler) annotateObject(ctx context.Context,
13931393
drpc *rmn.DRPlacementControl, obj client.Object, log logr.Logger,
13941394
) error {
1395-
if !obj.GetDeletionTimestamp().IsZero() {
1395+
if rmnutil.ResourceIsDeleted(obj) {
13961396
return nil
13971397
}
13981398

@@ -1565,7 +1565,7 @@ func getVRGsFromManagedClusters(
15651565

15661566
clustersQueriedSuccessfully++
15671567

1568-
if drClusterIsDeleted(drCluster) {
1568+
if rmnutil.ResourceIsDeleted(drCluster) {
15691569
log.Info("Skipping VRG on deleted drcluster", "drcluster", drCluster.Name, "vrg", vrg.Name)
15701570

15711571
continue
@@ -2145,7 +2145,7 @@ func AvailableS3Profiles(drClusters []rmn.DRCluster) []string {
21452145

21462146
for i := range drClusters {
21472147
drCluster := &drClusters[i]
2148-
if drClusterIsDeleted(drCluster) {
2148+
if rmnutil.ResourceIsDeleted(drCluster) {
21492149
continue
21502150
}
21512151

controllers/drpolicy_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (r *DRPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
9595

9696
secretsUtil := &util.SecretsUtil{Client: r.Client, APIReader: r.APIReader, Ctx: ctx, Log: log}
9797
// DRPolicy is marked for deletion
98-
if !drpolicy.ObjectMeta.DeletionTimestamp.IsZero() &&
98+
if util.ResourceIsDeleted(drpolicy) &&
9999
controllerutil.ContainsFinalizer(drpolicy, drPolicyFinalizerName) {
100100
return ctrl.Result{}, u.deleteDRPolicy(drclusters, secretsUtil, ramenConfig)
101101
}

controllers/util/objectmeta.go

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package util
55

66
import (
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
89
)
910

1011
func ObjectMetaEmbedded(objectMeta *metav1.ObjectMeta) metav1.ObjectMeta {
@@ -17,3 +18,8 @@ func ObjectMetaEmbedded(objectMeta *metav1.ObjectMeta) metav1.ObjectMeta {
1718
Finalizers: objectMeta.Finalizers,
1819
}
1920
}
21+
22+
// Return true if resource was marked for deletion.
23+
func ResourceIsDeleted(obj client.Object) bool {
24+
return !obj.GetDeletionTimestamp().IsZero()
25+
}

controllers/util/secrets_util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ func (sutil *SecretsUtil) ensureS3SecretResources(secretName, namespace string)
527527
return nil, sutil.deletePolicyResources(&secret, namespace)
528528
}
529529

530-
if secret.GetDeletionTimestamp().IsZero() {
530+
if !ResourceIsDeleted(&secret) {
531531
return &secret, nil
532532
}
533533

controllers/volsync/vshandler_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
volsyncv1alpha1 "github.com/backube/volsync/api/v1alpha1"
2727
ramendrv1alpha1 "github.com/ramendr/ramen/api/v1alpha1"
28+
"github.com/ramendr/ramen/controllers/util"
2829
"github.com/ramendr/ramen/controllers/volsync"
2930
)
3031

@@ -1060,7 +1061,7 @@ var _ = Describe("VolSync_Handler", func() {
10601061
Eventually(func() bool {
10611062
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(testPVC), testPVC)
10621063
if err == nil {
1063-
if !testPVC.GetDeletionTimestamp().IsZero() {
1064+
if util.ResourceIsDeleted(testPVC) {
10641065
// PVC protection finalizer is added automatically to PVC - but testenv
10651066
// doesn't have anything that will remove it for us - we're good as long
10661067
// as the pvc is marked for deletion
@@ -1373,7 +1374,7 @@ var _ = Describe("VolSync_Handler", func() {
13731374
Eventually(func() bool {
13741375
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(pvc), pvc)
13751376
if err == nil {
1376-
if !pvc.GetDeletionTimestamp().IsZero() {
1377+
if util.ResourceIsDeleted(pvc) {
13771378
// PVC protection finalizer is added automatically to PVC - but testenv
13781379
// doesn't have anything that will remove it for us - we're good as long
13791380
// as the pvc is marked for deletion

controllers/volumereplicationgroup_controller.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ func (v *VRGInstance) processVRG() ctrl.Result {
524524
v.log = v.log.WithName("vrginstance").WithValues("State", v.instance.Spec.ReplicationState)
525525
v.s3StoreAccessorsGet()
526526

527-
if !v.instance.GetDeletionTimestamp().IsZero() {
527+
if rmnutil.ResourceIsDeleted(v.instance) {
528528
v.log = v.log.WithValues("Finalize", true)
529529

530530
return v.processForDeletion()
@@ -546,7 +546,7 @@ func (v *VRGInstance) validateVRGState() error {
546546
if v.instance.Spec.ReplicationState != ramendrv1alpha1.Primary &&
547547
v.instance.Spec.ReplicationState != ramendrv1alpha1.Secondary {
548548
err := fmt.Errorf("invalid or unknown replication state detected (deleted %v, desired replicationState %v)",
549-
!v.instance.GetDeletionTimestamp().IsZero(),
549+
rmnutil.ResourceIsDeleted(v.instance),
550550
v.instance.Spec.ReplicationState)
551551

552552
v.log.Error(err, "Invalid request detected")
@@ -568,7 +568,7 @@ func (v *VRGInstance) validateVRGMode() error {
568568

569569
if !sync && !async {
570570
err := fmt.Errorf("neither of sync or async mode is enabled (deleted %v)",
571-
!v.instance.GetDeletionTimestamp().IsZero())
571+
rmnutil.ResourceIsDeleted(v.instance))
572572

573573
v.log.Error(err, "Invalid request detected")
574574

@@ -674,7 +674,7 @@ func (v *VRGInstance) updatePVCList() error {
674674
v.vrcUpdated = true
675675
}
676676

677-
if !v.instance.GetDeletionTimestamp().IsZero() {
677+
if rmnutil.ResourceIsDeleted(v.instance) {
678678
v.separatePVCsUsingVRGStatus(pvcList)
679679
v.log.Info(fmt.Sprintf("Separated PVCs (%d) into VolRepPVCs (%d) and VolSyncPVCs (%d)",
680680
len(pvcList.Items), len(v.volRepPVCs), len(v.volSyncPVCs)))

controllers/vrg_volrep.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (v *VRGInstance) isPVCReadyForSecondary(pvc *corev1.PersistentVolumeClaim,
168168
const ready bool = true
169169

170170
// If PVC is not being deleted, it is not ready for Secondary, unless action is failover
171-
if v.instance.Spec.Action != ramendrv1alpha1.VRGActionFailover && pvc.GetDeletionTimestamp().IsZero() {
171+
if v.instance.Spec.Action != ramendrv1alpha1.VRGActionFailover && !rmnutil.ResourceIsDeleted(pvc) {
172172
log.Info("VolumeReplication cannot become Secondary, as its PersistentVolumeClaim is not marked for deletion")
173173

174174
msg := "unable to transition to Secondary as PVC is not deleted"
@@ -364,7 +364,7 @@ func (v *VRGInstance) protectPVC(pvc *corev1.PersistentVolumeClaim, log logr.Log
364364
}
365365

366366
// Annotate that PVC protection is complete, skip if being deleted
367-
if pvc.GetDeletionTimestamp().IsZero() {
367+
if !rmnutil.ResourceIsDeleted(pvc) {
368368
if err := v.addProtectedAnnotationForPVC(pvc, log); err != nil {
369369
log.Info("Requeuing, as annotating PersistentVolumeClaim failed", "errorValue", err)
370370

@@ -402,7 +402,7 @@ func skipPVC(pvc *corev1.PersistentVolumeClaim, log logr.Logger) (bool, string)
402402

403403
func isPVCDeletedAndNotProtected(pvc *corev1.PersistentVolumeClaim, log logr.Logger) (bool, string) {
404404
// If PVC deleted but not yet protected with a finalizer, skip it!
405-
if !containsString(pvc.Finalizers, PvcVRFinalizerProtected) && !pvc.GetDeletionTimestamp().IsZero() {
405+
if !containsString(pvc.Finalizers, PvcVRFinalizerProtected) && rmnutil.ResourceIsDeleted(pvc) {
406406
log.Info("Skipping PersistentVolumeClaim, as it is marked for deletion and not yet protected")
407407

408408
msg := "Skipping pvc marked for deletion"
@@ -740,7 +740,7 @@ func (v *VRGInstance) reconcileVRsForDeletion() {
740740
func (v *VRGInstance) pvcUnprotectVolRepIfDeleted(
741741
pvc corev1.PersistentVolumeClaim, log logr.Logger,
742742
) (pvcDeleted bool) {
743-
pvcDeleted = !pvc.GetDeletionTimestamp().IsZero()
743+
pvcDeleted = rmnutil.ResourceIsDeleted(&pvc)
744744
if !pvcDeleted {
745745
return
746746
}
@@ -903,7 +903,7 @@ func (v *VRGInstance) reconcileMissingVR(pvc *corev1.PersistentVolumeClaim, log
903903

904904
err := v.reconciler.Get(v.ctx, vrNamespacedName, volRep)
905905
if err == nil {
906-
if !volRep.ObjectMeta.DeletionTimestamp.IsZero() {
906+
if rmnutil.ResourceIsDeleted(volRep) {
907907
log.Info("Requeuing due to processing a VR under deletion")
908908

909909
return !vrMissing, requeue
@@ -2067,7 +2067,7 @@ func (v *VRGInstance) validateExistingPV(pv *corev1.PersistentVolume) error {
20672067
pvcNamespacedName.String(), err)
20682068
}
20692069

2070-
if !pvc.DeletionTimestamp.IsZero() {
2070+
if rmnutil.ResourceIsDeleted(&pvc) {
20712071
return fmt.Errorf("existing bound PV %s claim %s deletion timestamp non-zero %v", existingPV.GetName(),
20722072
pvcNamespacedName.String(), pvc.DeletionTimestamp)
20732073
}
@@ -2122,7 +2122,7 @@ func (v *VRGInstance) validateExistingPVC(pvc *corev1.PersistentVolumeClaim) err
21222122
return fmt.Errorf("PVC %s exists and is not bound (phase: %s)", pvcNSName.String(), existingPVC.Status.Phase)
21232123
}
21242124

2125-
if !existingPVC.DeletionTimestamp.IsZero() {
2125+
if rmnutil.ResourceIsDeleted(existingPVC) {
21262126
return fmt.Errorf("existing bound PVC %s is being deleted", pvcNSName.String())
21272127
}
21282128

0 commit comments

Comments
 (0)