From 1b6aa122ff550d1b101f19a7a20677e2db385bb7 Mon Sep 17 00:00:00 2001 From: Wen Zhou Date: Fri, 26 Jul 2024 17:02:59 +0200 Subject: [PATCH] fix: when delete DSCI CR get current instance and remove finalizer on it (#1142) - after remove servicemesh and update condition, instance has been updated - use retryonconflict to make sure it is removed when something else might also update DSCI at the same time Signed-off-by: Wen Zhou --- .../dscinitialization_controller.go | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/controllers/dscinitialization/dscinitialization_controller.go b/controllers/dscinitialization/dscinitialization_controller.go index acf67a9462a..3d7f3c15d4c 100644 --- a/controllers/dscinitialization/dscinitialization_controller.go +++ b/controllers/dscinitialization/dscinitialization_controller.go @@ -34,6 +34,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" + "k8s.io/client-go/util/retry" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -136,11 +137,23 @@ func (r *DSCInitializationReconciler) Reconcile(ctx context.Context, req ctrl.Re if err := r.removeServiceMesh(ctx, instance); err != nil { return reconcile.Result{}, err } - if controllerutil.ContainsFinalizer(instance, finalizerName) { - controllerutil.RemoveFinalizer(instance, finalizerName) - if err := r.Update(ctx, instance); err != nil { - return ctrl.Result{}, err + + err := retry.RetryOnConflict(retry.DefaultRetry, func() error { + newInstance := &dsciv1.DSCInitialization{} + if err := r.Client.Get(ctx, client.ObjectKeyFromObject(instance), newInstance); err != nil { + return err + } + if controllerutil.ContainsFinalizer(newInstance, finalizerName) { + controllerutil.RemoveFinalizer(newInstance, finalizerName) + if err := r.Update(ctx, newInstance); err != nil { + return err + } } + return nil + }) + if err != nil { + r.Log.Error(err, "Failed to remove finalizer when deleting DSCInitialization instance") + return ctrl.Result{}, err } return ctrl.Result{}, nil