Skip to content

Commit

Permalink
bah-mutate!
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakknutsen committed Sep 4, 2024
1 parent 5527d2b commit 4e62320
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 5 additions & 3 deletions controllers/routingctrl/reconcile_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ func (r *Controller) propagateHostsToWatchedCR(target *unstructured.Unstructured
}

func (r *Controller) ensureResourceHasFinalizer(ctx context.Context, target *unstructured.Unstructured) error {
if controllerutil.AddFinalizer(target, finalizerName) {
if err := unstruct.Patch(ctx, r.Client, target); err != nil {
if !controllerutil.ContainsFinalizer(target, finalizerName) {
if err := unstruct.PatchMutate(ctx, r.Client, target, func() error {
controllerutil.AddFinalizer(target, finalizerName)
return nil

Check failure on line 127 in controllers/routingctrl/reconcile_resources.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}); err != nil {
return fmt.Errorf("failed to patch finalizer to resource %s/%s: %w", target.GetNamespace(), target.GetName(), err)
}
}

return nil

Check failure on line 132 in controllers/routingctrl/reconcile_resources.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

Expand Down
20 changes: 18 additions & 2 deletions pkg/unstruct/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

func Apply(ctx context.Context, cli client.Client, objects []*unstructured.Unstructured, metaOptions ...metadata.Option) error {
Expand Down Expand Up @@ -63,6 +64,21 @@ func IsMarkedForDeletion(target *unstructured.Unstructured) bool {
return !target.GetDeletionTimestamp().IsZero()
}

// Patch updates the specified Kubernetes resource by applying changes from the provided target object.
// In case of conflicts, it will retry using default strategy.
func PatchMutate(ctx context.Context, cli client.Client, target *unstructured.Unstructured, mutate controllerutil.MutateFn) error {
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
_, err := controllerutil.CreateOrPatch(ctx, cli, target, mutate)
return err

Check failure on line 72 in pkg/unstruct/funcs.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
})

if err != nil {
return fmt.Errorf("failed to patchmutate resource metadata with retry: %w", err)
}

return nil
}

// Patch updates the specified Kubernetes resource by applying changes from the provided target object.
// In case of conflicts, it will retry using default strategy.
func Patch(ctx context.Context, cli client.Client, target *unstructured.Unstructured) error {
Expand All @@ -74,8 +90,8 @@ func Patch(ctx context.Context, cli client.Client, target *unstructured.Unstruct
return err //nolint:wrapcheck // Return unwrapped error for retry logic
}

patch := client.MergeFrom(currentRes)
if errPatch := cli.Patch(ctx, target, patch); errPatch != nil {
patch := client.MergeFrom(target)
if errPatch := cli.Patch(ctx, currentRes, patch); errPatch != nil {
return errPatch //nolint:wrapcheck // Return unwrapped error for retry logic
}

Expand Down

0 comments on commit 4e62320

Please sign in to comment.