Skip to content

Commit

Permalink
Handle delete before adding finalizer
Browse files Browse the repository at this point in the history
In Reconcile() method, move the object deletion above add finalizer.
Finalizers can't be set when an object is being deleted.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Jul 24, 2023
1 parent 8d9a181 commit 650516c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions internal/controller/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,20 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
}()

// Prune managed resources if the object is under deletion.
if !obj.ObjectMeta.DeletionTimestamp.IsZero() {
return r.finalize(ctx, obj)
}

// Add finalizer first if it doesn't exist to avoid the race condition
// between init and delete.
// Note: Finalizers in general can only be added when the deletionTimestamp
// is not set.
if !controllerutil.ContainsFinalizer(obj, kustomizev1.KustomizationFinalizer) {
controllerutil.AddFinalizer(obj, kustomizev1.KustomizationFinalizer)
return ctrl.Result{Requeue: true}, nil
}

// Prune managed resources if the object is under deletion.
if !obj.ObjectMeta.DeletionTimestamp.IsZero() {
return r.finalize(ctx, obj)
}

// Skip reconciliation if the object is suspended.
if obj.Spec.Suspend {
log.Info("Reconciliation is suspended for this object")
Expand Down

0 comments on commit 650516c

Please sign in to comment.