From f47fd9424ae6da9b9aaf2ae486b68edcafba091a Mon Sep 17 00:00:00 2001 From: Jon Huhn Date: Tue, 31 Oct 2023 19:51:35 -0500 Subject: [PATCH] eliminate short-circuiting in logic to add/remove finalizers --- controllers/azuremanagedcontrolplane_controller.go | 5 +++-- controllers/helpers.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/controllers/azuremanagedcontrolplane_controller.go b/controllers/azuremanagedcontrolplane_controller.go index b254e541910..1534dbe6d19 100644 --- a/controllers/azuremanagedcontrolplane_controller.go +++ b/controllers/azuremanagedcontrolplane_controller.go @@ -224,8 +224,9 @@ func (amcpr *AzureManagedControlPlaneReconciler) reconcileNormal(ctx context.Con log.Info("Reconciling AzureManagedControlPlane") // Remove deprecated Cluster finalizer if it exists, if the AzureManagedControlPlane doesn't have our finalizer, add it. - if controllerutil.RemoveFinalizer(scope.ControlPlane, infrav1.ClusterFinalizer) || - controllerutil.AddFinalizer(scope.ControlPlane, infrav1.ManagedClusterFinalizer) { + needsPatch := controllerutil.RemoveFinalizer(scope.ControlPlane, infrav1.ClusterFinalizer) + needsPatch = controllerutil.AddFinalizer(scope.ControlPlane, infrav1.ManagedClusterFinalizer) || needsPatch + if needsPatch { // Register the finalizer immediately to avoid orphaning Azure resources on delete if err := scope.PatchObject(ctx); err != nil { amcpr.Recorder.Eventf(scope.ControlPlane, corev1.EventTypeWarning, "AzureManagedControlPlane unavailable", "failed to patch resource: %s", err) diff --git a/controllers/helpers.go b/controllers/helpers.go index 7b4bd3a49e8..920483696f8 100644 --- a/controllers/helpers.go +++ b/controllers/helpers.go @@ -656,8 +656,9 @@ func EnsureClusterIdentity(ctx context.Context, c client.Client, object conditio } // Remove deprecated finalizer if it exists, Register the finalizer immediately to avoid orphaning Azure resources on delete. - if controllerutil.RemoveFinalizer(identity, deprecatedClusterIdentityFinalizer(finalizerPrefix, namespace, name)) || - controllerutil.AddFinalizer(identity, clusterIdentityFinalizer(finalizerPrefix, namespace, name)) { + needsPatch := controllerutil.RemoveFinalizer(identity, deprecatedClusterIdentityFinalizer(finalizerPrefix, namespace, name)) + needsPatch = controllerutil.AddFinalizer(identity, clusterIdentityFinalizer(finalizerPrefix, namespace, name)) || needsPatch + if needsPatch { // finalizers are added/removed then patch the object identityHelper, err := patch.NewHelper(identity, c) if err != nil {