Skip to content

Commit

Permalink
Add ReconcileObject variant that returns the object
Browse files Browse the repository at this point in the history
  • Loading branch information
olim7t committed Dec 13, 2024
1 parent 36a47f9 commit ef4a864
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions pkg/reconciliation/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -35,22 +44,23 @@ 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)) {
resourceVersion := T(currentCm).GetResourceVersion()
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
}

0 comments on commit ef4a864

Please sign in to comment.