From ef4a8643d19f1fe8b3b4060383dba25fa27131b7 Mon Sep 17 00:00:00 2001 From: Olivier Michallat Date: Fri, 13 Dec 2024 10:00:17 -0800 Subject: [PATCH] Add ReconcileObject variant that returns the object --- pkg/reconciliation/generic.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pkg/reconciliation/generic.go b/pkg/reconciliation/generic.go index a0bcbaafb..2f9f32f7d 100644 --- a/pkg/reconciliation/generic.go +++ b/pkg/reconciliation/generic.go @@ -21,6 +21,15 @@ type Reconcileable[T any] interface { // ReconcileObject ensures that desiredObject exists in the given state, either by creating it, or updating it if it // already exists. func ReconcileObject[U any, T Reconcileable[U]](ctx context.Context, kClient client.Client, requeueDelay time.Duration, desiredObject U) result.ReconcileResult { + recResult, _ := ReconcileAndGetObject[U, T](ctx, kClient, requeueDelay, desiredObject) + return recResult +} + +// ReconcileAndGetObject ensures that desiredObject exists in the given state, either by creating it, or updating it if +// it already exists. It returns the current state of the object on the server after the reconciliation. +func ReconcileAndGetObject[U any, T Reconcileable[U]]( + ctx context.Context, kClient client.Client, requeueDelay time.Duration, desiredObject U, +) (result.ReconcileResult, *U) { objectKey := types.NamespacedName{ Name: T(&desiredObject).GetName(), Namespace: T(&desiredObject).GetNamespace(), @@ -35,13 +44,13 @@ func ReconcileObject[U any, T Reconcileable[U]](ctx context.Context, kClient cli if errors.IsNotFound(err) { if err := kClient.Create(ctx, T(&desiredObject)); err != nil { if errors.IsAlreadyExists(err) { - return result.RequeueSoon(requeueDelay) + return result.RequeueSoon(requeueDelay), nil } - return result.Error(err) + return result.Error(err), nil } - return result.Continue() + return result.Continue(), &desiredObject } - return result.Error(err) + return result.Error(err), nil } if !annotations.CompareHashAnnotations(T(currentCm), T(&desiredObject)) { @@ -49,8 +58,9 @@ func ReconcileObject[U any, T Reconcileable[U]](ctx context.Context, kClient cli T(&desiredObject).DeepCopyInto(currentCm) T(currentCm).SetResourceVersion(resourceVersion) if err := kClient.Update(ctx, T(currentCm)); err != nil { - return result.Error(err) + return result.Error(err), nil } + return result.Continue(), currentCm } - return result.Continue() + return result.Continue(), currentCm }