Skip to content

Commit

Permalink
Avoid contacting kube apiserver is patch is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jun 3, 2024
1 parent be2f383 commit fa88ec2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
10 changes: 5 additions & 5 deletions pkg/client/metadata_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkg/client/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()).
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/client/unstructured_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()).
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit fa88ec2

Please sign in to comment.