core: skip reconcile when adding the finalizers

When finalizers are added to the CRs, a follow-up reconcile
will be triggered due to the increased generation on the CR.
Therefore, abort the initial reconcile when adding the finalizer,
and allow the follow-up reconcile to complete the configuration.

Signed-off-by: Travis Nielsen <tnielsen@redhat.com>
This commit is contained in:
Travis Nielsen committed 2025-04-08 13:42:58 -06:00
1 parent 916c0ab50e
commit f7fb1bc0f2
25 files changed
+118 -44

No files matched your search

+8 -5
View File
@@ -52,25 +52,28 @@ func remove(list []string, s string) []string {
// AddFinalizerIfNotPresent adds a finalizer an object to avoid instant deletion
// of the object without finalizing it.
func AddFinalizerIfNotPresent(ctx context.Context, client client.Client, obj client.Object) error {
func AddFinalizerIfNotPresent(ctx context.Context, client client.Client, obj client.Object) (bool, error) {
objectFinalizer := buildFinalizerName(obj.GetObjectKind().GroupVersionKind().Kind)
accessor, err := meta.Accessor(obj)
if err != nil {
return errors.Wrap(err, "failed to get meta information of object")
return false, errors.Wrap(err, "failed to get meta information of object")
}
if !contains(accessor.GetFinalizers(), objectFinalizer) {
logger.Infof("adding finalizer %q on %q", objectFinalizer, accessor.GetName())
accessor.SetFinalizers(append(accessor.GetFinalizers(), objectFinalizer))
originalGeneration := obj.GetGeneration()
// Update CR with finalizer
if err := client.Update(ctx, obj); err != nil {
return errors.Wrapf(err, "failed to add finalizer %q on %q", objectFinalizer, accessor.GetName())
return false, errors.Wrapf(err, "failed to add finalizer %q on %q", objectFinalizer, accessor.GetName())
}
newGeneration := obj.GetGeneration()
logger.Debugf("when adding finalizer on %q, original generation %d, new generation %d", accessor.GetName(), originalGeneration, newGeneration)
return originalGeneration != newGeneration, nil
}
return nil
return false, nil
}
// RemoveFinalizer removes a finalizer from an object
@@ -46,9 +46,10 @@ func TestAddFinalizerIfNotPresent(t *testing.T) {
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
assert.Empty(t, fakeObject.Finalizers)
err := AddFinalizerIfNotPresent(context.TODO(), cl, fakeObject)
generationUpdated, err := AddFinalizerIfNotPresent(context.TODO(), cl, fakeObject)
assert.NoError(t, err)
assert.NotEmpty(t, fakeObject.Finalizers)
assert.False(t, generationUpdated)
}
func TestRemoveFinalizer(t *testing.T) {
+2 -1
View File
@@ -104,7 +104,8 @@ func WatchControllerPredicate[T client.Object](scheme *runtime.Scheme) predicate
logger.Debugf("resource %q: %q is going to be deleted", kind, nsName)
return true
} else if objOld.GetGeneration() != objNew.GetGeneration() {
logger.Debugf("skipping resource %q: %q, update event with unchanged spec", kind, nsName)
logger.Debugf("reconciling %s %q with changed generation", kind, nsName.String())
return true
}
return false
},