Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Streamline finalizer patching #434

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 30 additions & 49 deletions reconcilers/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,59 +110,40 @@ func ClearFinalizer(ctx context.Context, resource client.Object, finalizer strin
return ensureFinalizer(ctx, resource, finalizer, false)
}

func ensureFinalizer(ctx context.Context, resource client.Object, finalizer string, add bool) error {
config := RetrieveOriginalConfigOrDie(ctx)
if config.IsEmpty() {
panic(fmt.Errorf("resource config must exist on the context. Check that the context from a ResourceReconciler"))
}
resourceType := RetrieveOriginalResourceType(ctx)
if resourceType == nil {
panic(fmt.Errorf("resource type must exist on the context. Check that the context from a ResourceReconciler"))
}

if finalizer == "" || controllerutil.ContainsFinalizer(resource, finalizer) == add {
func ensureFinalizer(ctx context.Context, current client.Object, finalizer string, add bool) error {
if finalizer == "" || controllerutil.ContainsFinalizer(current, finalizer) == add {
// nothing to do
return nil
}

// cast the current object back to the resource so scheme-aware, typed client can operate on it
cast := &CastResource[client.Object, client.Object]{
Reconciler: &SyncReconciler[client.Object]{
SyncDuringFinalization: true,
Sync: func(ctx context.Context, current client.Object) error {
log := logr.FromContextOrDiscard(ctx)

desired := current.DeepCopyObject().(client.Object)
if add {
log.Info("adding finalizer", "finalizer", finalizer)
controllerutil.AddFinalizer(desired, finalizer)
} else {
log.Info("removing finalizer", "finalizer", finalizer)
controllerutil.RemoveFinalizer(desired, finalizer)
}

patch := client.MergeFromWithOptions(current, client.MergeFromWithOptimisticLock{})
if err := config.Patch(ctx, desired, patch); err != nil {
if !errors.Is(err, ErrQuiet) {
log.Error(err, "unable to patch finalizers", "finalizer", finalizer)
config.Recorder.Eventf(current, corev1.EventTypeWarning, "FinalizerPatchFailed",
"Failed to patch finalizer %q: %s", finalizer, err)
}
return err
}
config.Recorder.Eventf(current, corev1.EventTypeNormal, "FinalizerPatched",
"Patched finalizer %q", finalizer)

// update current object with values from the api server after patching
current.SetFinalizers(desired.GetFinalizers())
current.SetResourceVersion(desired.GetResourceVersion())
current.SetGeneration(desired.GetGeneration())

return nil
},
},
config := RetrieveOriginalConfigOrDie(ctx)
log := logr.FromContextOrDiscard(ctx)

desired := current.DeepCopyObject().(client.Object)
if add {
log.Info("adding finalizer", "finalizer", finalizer)
controllerutil.AddFinalizer(desired, finalizer)
} else {
log.Info("removing finalizer", "finalizer", finalizer)
controllerutil.RemoveFinalizer(desired, finalizer)
}

_, err := cast.Reconcile(ctx, resource)
return err
patch := client.MergeFromWithOptions(current, client.MergeFromWithOptimisticLock{})
if err := config.Patch(ctx, desired, patch); err != nil {
if !errors.Is(err, ErrQuiet) {
log.Error(err, "unable to patch finalizers", "finalizer", finalizer)
config.Recorder.Eventf(current, corev1.EventTypeWarning, "FinalizerPatchFailed",
"Failed to patch finalizer %q: %s", finalizer, err)
}
return err
}
config.Recorder.Eventf(current, corev1.EventTypeNormal, "FinalizerPatched",
"Patched finalizer %q", finalizer)

// update current object with values from the api server after patching
current.SetFinalizers(desired.GetFinalizers())
current.SetResourceVersion(desired.GetResourceVersion())
current.SetGeneration(desired.GetGeneration())

return nil
}
Loading