From fa88ec2314fe6990632365d5af1bb320c3374019 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Sun, 24 Dec 2023 00:32:33 -0800 Subject: [PATCH] Avoid contacting kube apiserver is patch is empty Signed-off-by: Tamal Saha --- pkg/client/metadata_client.go | 10 +++++----- pkg/client/options.go | 13 +++++++++++++ pkg/client/typed_client.go | 10 +++++----- pkg/client/unstructured_client.go | 10 +++++----- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/pkg/client/metadata_client.go b/pkg/client/metadata_client.go index d0c6b8e13a..f73eeac0a3 100644 --- a/pkg/client/metadata_client.go +++ b/pkg/client/metadata_client.go @@ -98,14 +98,14 @@ func (mc *metadataClient) Patch(ctx context.Context, obj Object, patch Patch, op return err } + patchOpts := &PatchOptions{} + patchOpts.ApplyOptions(opts) + data, err := patch.Data(obj) - if err != nil { + if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") { return err } - patchOpts := &PatchOptions{} - patchOpts.ApplyOptions(opts) - res, err := resInt.Patch(ctx, metadata.Name, patch.Type(), data, *patchOpts.AsPatchOptions()) if err != nil { return err @@ -189,7 +189,7 @@ func (mc *metadataClient) PatchSubResource(ctx context.Context, obj Object, subR } data, err := patch.Data(body) - if err != nil { + if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") { return err } diff --git a/pkg/client/options.go b/pkg/client/options.go index 798506f486..5e522f1290 100644 --- a/pkg/client/options.go +++ b/pkg/client/options.go @@ -747,6 +747,9 @@ type PatchOptions struct { // Raw represents raw PatchOptions, as passed to the API server. Raw *metav1.PatchOptions + + // SendEmptyPatch is going to send empty patch calls to the API server. + SendEmptyPatch bool } // ApplyOptions applies the given patch options on these options, @@ -809,6 +812,16 @@ func (forceOwnership) ApplyToSubResourcePatch(opts *SubResourcePatchOptions) { opts.Force = &definitelyTrue } +// SendEmptyPatch sets the "sendEmptyPatch" option to true. +var SendEmptyPatch = sendEmptyPatch{} + +type sendEmptyPatch struct{} + +// ApplyToPatch applies this configuration to the given patch options. +func (sendEmptyPatch) ApplyToPatch(opts *PatchOptions) { + opts.SendEmptyPatch = true +} + // }}} // {{{ DeleteAllOf Options diff --git a/pkg/client/typed_client.go b/pkg/client/typed_client.go index 92afd9a9c2..1347882a7f 100644 --- a/pkg/client/typed_client.go +++ b/pkg/client/typed_client.go @@ -114,14 +114,14 @@ func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts . return err } + patchOpts := &PatchOptions{} + patchOpts.ApplyOptions(opts) + data, err := patch.Data(obj) - if err != nil { + if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") { return err } - patchOpts := &PatchOptions{} - patchOpts.ApplyOptions(opts) - return o.Patch(patch.Type()). NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()). Resource(o.resource()). @@ -263,7 +263,7 @@ func (c *typedClient) PatchSubResource(ctx context.Context, obj Object, subResou } data, err := patch.Data(body) - if err != nil { + if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") { return err } diff --git a/pkg/client/unstructured_client.go b/pkg/client/unstructured_client.go index 0d96951780..439ba25fd3 100644 --- a/pkg/client/unstructured_client.go +++ b/pkg/client/unstructured_client.go @@ -148,14 +148,14 @@ func (uc *unstructuredClient) Patch(ctx context.Context, obj Object, patch Patch return err } + patchOpts := &PatchOptions{} + patchOpts.ApplyOptions(opts) + data, err := patch.Data(obj) - if err != nil { + if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") { return err } - patchOpts := &PatchOptions{} - patchOpts.ApplyOptions(opts) - return o.Patch(patch.Type()). NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()). Resource(o.resource()). @@ -342,7 +342,7 @@ func (uc *unstructuredClient) PatchSubResource(ctx context.Context, obj Object, } data, err := patch.Data(body) - if err != nil { + if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") { return err }