Skip to content

Commit f501516

Browse files
committed
Avoid contacting kube apiserver is patch is empty
Signed-off-by: Tamal Saha <[email protected]>
1 parent 0f7927c commit f501516

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

pkg/client/metadata_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ func (mc *metadataClient) Patch(ctx context.Context, obj Object, patch Patch, op
9898
return err
9999
}
100100

101+
patchOpts := &PatchOptions{}
102+
patchOpts.ApplyOptions(opts)
103+
101104
data, err := patch.Data(obj)
102-
if err != nil {
105+
if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") {
103106
return err
104107
}
105108

106-
patchOpts := &PatchOptions{}
107-
patchOpts.ApplyOptions(opts)
108-
109109
res, err := resInt.Patch(ctx, metadata.Name, patch.Type(), data, *patchOpts.AsPatchOptions())
110110
if err != nil {
111111
return err
@@ -189,7 +189,7 @@ func (mc *metadataClient) PatchSubResource(ctx context.Context, obj Object, subR
189189
}
190190

191191
data, err := patch.Data(body)
192-
if err != nil {
192+
if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") {
193193
return err
194194
}
195195

pkg/client/options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,9 @@ type PatchOptions struct {
842842

843843
// Raw represents raw PatchOptions, as passed to the API server.
844844
Raw *metav1.PatchOptions
845+
846+
// SendEmptyPatch is going to send empty patch calls to the API server.
847+
SendEmptyPatch bool
845848
}
846849

847850
// ApplyOptions applies the given patch options on these options,
@@ -908,6 +911,16 @@ func (forceOwnership) ApplyToSubResourcePatch(opts *SubResourcePatchOptions) {
908911
opts.Force = &definitelyTrue
909912
}
910913

914+
// SendEmptyPatch sets the "sendEmptyPatch" option to true.
915+
var SendEmptyPatch = sendEmptyPatch{}
916+
917+
type sendEmptyPatch struct{}
918+
919+
// ApplyToPatch applies this configuration to the given patch options.
920+
func (sendEmptyPatch) ApplyToPatch(opts *PatchOptions) {
921+
opts.SendEmptyPatch = true
922+
}
923+
911924
// }}}
912925

913926
// {{{ DeleteAllOf Options

pkg/client/typed_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts .
114114
return err
115115
}
116116

117+
patchOpts := &PatchOptions{}
118+
patchOpts.ApplyOptions(opts)
119+
117120
data, err := patch.Data(obj)
118-
if err != nil {
121+
if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") {
119122
return err
120123
}
121124

122-
patchOpts := &PatchOptions{}
123-
patchOpts.ApplyOptions(opts)
124-
125125
return o.Patch(patch.Type()).
126126
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
127127
Resource(o.resource()).
@@ -263,7 +263,7 @@ func (c *typedClient) PatchSubResource(ctx context.Context, obj Object, subResou
263263
}
264264

265265
data, err := patch.Data(body)
266-
if err != nil {
266+
if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") {
267267
return err
268268
}
269269

pkg/client/unstructured_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ func (uc *unstructuredClient) Patch(ctx context.Context, obj Object, patch Patch
148148
return err
149149
}
150150

151+
patchOpts := &PatchOptions{}
152+
patchOpts.ApplyOptions(opts)
153+
151154
data, err := patch.Data(obj)
152-
if err != nil {
155+
if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") {
153156
return err
154157
}
155158

156-
patchOpts := &PatchOptions{}
157-
patchOpts.ApplyOptions(opts)
158-
159159
return o.Patch(patch.Type()).
160160
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
161161
Resource(o.resource()).
@@ -342,7 +342,7 @@ func (uc *unstructuredClient) PatchSubResource(ctx context.Context, obj Object,
342342
}
343343

344344
data, err := patch.Data(body)
345-
if err != nil {
345+
if err != nil || (!patchOpts.SendEmptyPatch && string(data) == "{}") {
346346
return err
347347
}
348348

0 commit comments

Comments
 (0)