Skip to content

Commit 36db6ad

Browse files
committed
Add PatchStatus, options to UpdateStatus
This adds .Status().Patch to the client for patching status, and makes sure that .Status().Update takes update options like normal update does.
1 parent f4eaa7b commit 36db6ad

File tree

6 files changed

+75
-11
lines changed

6 files changed

+75
-11
lines changed

pkg/client/client.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,19 @@ type statusWriter struct {
171171
var _ StatusWriter = &statusWriter{}
172172

173173
// Update implements client.StatusWriter
174-
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object) error {
174+
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
175175
_, ok := obj.(*unstructured.Unstructured)
176176
if ok {
177-
return sw.client.unstructuredClient.UpdateStatus(ctx, obj)
177+
return sw.client.unstructuredClient.UpdateStatus(ctx, obj, opts...)
178178
}
179-
return sw.client.typedClient.UpdateStatus(ctx, obj)
179+
return sw.client.typedClient.UpdateStatus(ctx, obj, opts...)
180+
}
181+
182+
// Patch implements client.Client
183+
func (sw *statusWriter) Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOptionFunc) error {
184+
_, ok := obj.(*unstructured.Unstructured)
185+
if ok {
186+
return sw.client.unstructuredClient.PatchStatus(ctx, obj, patch, opts...)
187+
}
188+
return sw.client.typedClient.PatchStatus(ctx, obj, patch, opts...)
180189
}

pkg/client/fake/client.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,14 @@ type fakeStatusWriter struct {
244244
client *fakeClient
245245
}
246246

247-
func (sw *fakeStatusWriter) Update(ctx context.Context, obj runtime.Object) error {
247+
func (sw *fakeStatusWriter) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOptionFunc) error {
248248
// TODO(droot): This results in full update of the obj (spec + status). Need
249249
// a way to update status field only.
250-
return sw.client.Update(ctx, obj)
250+
return sw.client.Update(ctx, obj, opts...)
251+
}
252+
253+
func (sw *fakeStatusWriter) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOptionFunc) error {
254+
// TODO(droot): This results in full update of the obj (spec + status). Need
255+
// a way to update status field only.
256+
return sw.client.Patch(ctx, obj, patch, opts...)
251257
}

pkg/client/fake/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ var _ = Describe("Fake client", func() {
167167
Namespace: "ns2",
168168
},
169169
}
170-
err := cl.Create(nil, newcm, client.CreateDryRunAll())
170+
err := cl.Create(nil, newcm, client.CreateDryRunAll)
171171
Expect(err).To(BeNil())
172172

173173
By("Getting the new configmap")
@@ -193,7 +193,7 @@ var _ = Describe("Fake client", func() {
193193
"test-key": "new-value",
194194
},
195195
}
196-
err := cl.Update(nil, newcm, client.UpdateDryRunAll())
196+
err := cl.Update(nil, newcm, client.UpdateDryRunAll)
197197
Expect(err).To(BeNil())
198198

199199
By("Getting the new configmap")

pkg/client/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type StatusWriter interface {
8989
// Update updates the fields corresponding to the status subresource for the
9090
// given obj. obj must be a struct pointer so that obj can be updated
9191
// with the content returned by the Server.
92-
Update(ctx context.Context, obj runtime.Object) error
92+
Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error
9393

9494
// Patch patches the given object's subresource. obj must be a struct
9595
// pointer so that obj can be updated with the content returned by the

pkg/client/typed_client.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (c *typedClient) List(ctx context.Context, obj runtime.Object, opts ...List
141141
}
142142

143143
// UpdateStatus used by StatusWriter to write status.
144-
func (c *typedClient) UpdateStatus(ctx context.Context, obj runtime.Object) error {
144+
func (c *typedClient) UpdateStatus(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
145145
o, err := c.cache.getObjMeta(obj)
146146
if err != nil {
147147
return err
@@ -156,6 +156,32 @@ func (c *typedClient) UpdateStatus(ctx context.Context, obj runtime.Object) erro
156156
Name(o.GetName()).
157157
SubResource("status").
158158
Body(obj).
159+
VersionedParams((&UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions(), c.paramCodec).
160+
Context(ctx).
161+
Do().
162+
Into(obj)
163+
}
164+
165+
// PatchStatus used by StatusWriter to write status.
166+
func (c *typedClient) PatchStatus(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOptionFunc) error {
167+
o, err := c.cache.getObjMeta(obj)
168+
if err != nil {
169+
return err
170+
}
171+
172+
data, err := patch.Data(obj)
173+
if err != nil {
174+
return err
175+
}
176+
177+
patchOpts := &PatchOptions{}
178+
return o.Patch(patch.Type()).
179+
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
180+
Resource(o.resource()).
181+
Name(o.GetName()).
182+
SubResource("status").
183+
Body(data).
184+
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).
159185
Context(ctx).
160186
Do().
161187
Into(obj)

pkg/client/unstructured_client.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (uc *unstructuredClient) List(_ context.Context, obj runtime.Object, opts .
160160
return nil
161161
}
162162

163-
func (uc *unstructuredClient) UpdateStatus(_ context.Context, obj runtime.Object) error {
163+
func (uc *unstructuredClient) UpdateStatus(_ context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
164164
u, ok := obj.(*unstructured.Unstructured)
165165
if !ok {
166166
return fmt.Errorf("unstructured client did not understand object: %T", obj)
@@ -169,7 +169,30 @@ func (uc *unstructuredClient) UpdateStatus(_ context.Context, obj runtime.Object
169169
if err != nil {
170170
return err
171171
}
172-
i, err := r.UpdateStatus(u, metav1.UpdateOptions{})
172+
i, err := r.UpdateStatus(u, *(&UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions())
173+
if err != nil {
174+
return err
175+
}
176+
u.Object = i.Object
177+
return nil
178+
}
179+
180+
func (uc *unstructuredClient) PatchStatus(_ context.Context, obj runtime.Object, patch Patch, opts ...PatchOptionFunc) error {
181+
u, ok := obj.(*unstructured.Unstructured)
182+
if !ok {
183+
return fmt.Errorf("unstructured client did not understand object: %T", obj)
184+
}
185+
r, err := uc.getResourceInterface(u.GroupVersionKind(), u.GetNamespace())
186+
if err != nil {
187+
return err
188+
}
189+
190+
data, err := patch.Data(obj)
191+
if err != nil {
192+
return err
193+
}
194+
195+
i, err := r.Patch(u.GetName(), patch.Type(), data, *(&PatchOptions{}).ApplyOptions(opts).AsPatchOptions(), "status")
173196
if err != nil {
174197
return err
175198
}

0 commit comments

Comments
 (0)