diff --git a/pkg/resource/api.go b/pkg/resource/api.go index 66b87e6a4..f99f8e791 100644 --- a/pkg/resource/api.go +++ b/pkg/resource/api.go @@ -67,8 +67,9 @@ func (a *APIPatchingApplicator) WithLogger(l logging.Logger) *APIPatchingApplica // not exist, or patched if it does. If the object does exist, it will only be // patched if the passed object has the same or an empty resource version. func (a *APIPatchingApplicator) Apply(ctx context.Context, obj client.Object, ao ...ApplyOption) error { //nolint:gocyclo // the logic here is crucial and deserves to stay in one method + log := a.log.WithValues(logging.ForResource(obj)) + if obj.GetName() == "" && obj.GetGenerateName() != "" { - log := a.log.WithValues(logging.ForResource(obj)) log.Info("creating object") return a.client.Create(ctx, obj) } @@ -107,7 +108,6 @@ func (a *APIPatchingApplicator) Apply(ctx context.Context, obj client.Object, ao if err != nil { return errors.Wrapf(err, "failed to diff %s", HumanReadableReference(a.client, obj)) } - log := a.log.WithValues(logging.ForResource(obj)) log.WithValues("diff", string(patchBytes)).Info("patching object") return a.client.Patch(ctx, obj, client.RawPatch(patch.Type(), patchBytes)) @@ -132,7 +132,7 @@ func groupResource(c client.Client, o client.Object) (schema.GroupResource, erro // This only works with a desired object of type *unstructured.Unstructured. // // Deprecated: replace with Server Side Apply. -func AdditiveMergePatchApplyOption(ctx context.Context, current, desired runtime.Object) error { +func AdditiveMergePatchApplyOption(_ context.Context, current, desired runtime.Object) error { u, ok := desired.(*unstructured.Unstructured) if !ok { return errors.New("desired object is not an unstructured.Unstructured") @@ -181,8 +181,9 @@ func (a *APIUpdatingApplicator) WithLogger(l logging.Logger) *APIUpdatingApplica // Apply changes to the supplied object. The object will be created if it does // not exist, or updated if it does. func (a *APIUpdatingApplicator) Apply(ctx context.Context, obj client.Object, ao ...ApplyOption) error { + log := a.log.WithValues(logging.ForResource(obj)) + if obj.GetName() == "" && obj.GetGenerateName() != "" { - log := a.log.WithValues(logging.ForResource(obj)) log.Info("creating object") return a.client.Create(ctx, obj) } @@ -209,7 +210,9 @@ func (a *APIUpdatingApplicator) Apply(ctx context.Context, obj client.Object, ao if err != nil { return errors.Wrapf(err, "failed to diff %s", HumanReadableReference(a.client, obj)) } - log := a.log.WithValues(logging.ForResource(obj)) + if len(patchBytes) == 0 { + return nil + } log.WithValues("diff", string(patchBytes)).Info("updating object") return a.client.Update(ctx, obj)