Skip to content

Commit

Permalink
Add PatchStatus, options to UpdateStatus
Browse files Browse the repository at this point in the history
This adds .Status().Patch to the client for patching status, and makes sure
that .Status().Update takes update options like normal update does.
  • Loading branch information
DirectXMan12 committed May 17, 2019
1 parent 9c43b2a commit 21c61b5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
15 changes: 12 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,19 @@ type statusWriter struct {
var _ StatusWriter = &statusWriter{}

// Update implements client.StatusWriter
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object) error {
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return sw.client.unstructuredClient.UpdateStatus(ctx, obj)
return sw.client.unstructuredClient.UpdateStatus(ctx, obj, opts...)
}
return sw.client.typedClient.UpdateStatus(ctx, obj)
return sw.client.typedClient.UpdateStatus(ctx, obj, opts...)
}

// Patch implements client.Client
func (sw *statusWriter) Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOptionFunc) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return sw.client.unstructuredClient.PatchStatus(ctx, obj, patch, opts...)
}
return sw.client.typedClient.PatchStatus(ctx, obj, patch, opts...)
}
10 changes: 8 additions & 2 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,14 @@ type fakeStatusWriter struct {
client *fakeClient
}

func (sw *fakeStatusWriter) Update(ctx context.Context, obj runtime.Object) error {
func (sw *fakeStatusWriter) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOptionFunc) error {
// TODO(droot): This results in full update of the obj (spec + status). Need
// a way to update status field only.
return sw.client.Update(ctx, obj)
return sw.client.Update(ctx, obj, opts...)
}

func (sw *fakeStatusWriter) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOptionFunc) error {
// TODO(droot): This results in full update of the obj (spec + status). Need
// a way to update status field only.
return sw.client.Patch(ctx, obj, patch, opts...)
}
4 changes: 2 additions & 2 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ var _ = Describe("Fake client", func() {
Namespace: "ns2",
},
}
err := cl.Create(nil, newcm, client.CreateDryRunAll())
err := cl.Create(nil, newcm, client.CreateDryRunAll)
Expect(err).To(BeNil())

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

By("Getting the new configmap")
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type StatusWriter interface {
// Update updates the fields corresponding to the status subresource for the
// given obj. obj must be a struct pointer so that obj can be updated
// with the content returned by the Server.
Update(ctx context.Context, obj runtime.Object) error
Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error

// Patch patches the given object's subresource. obj must be a struct
// pointer so that obj can be updated with the content returned by the
Expand Down
28 changes: 27 additions & 1 deletion pkg/client/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (c *typedClient) List(ctx context.Context, obj runtime.Object, opts ...List
}

// UpdateStatus used by StatusWriter to write status.
func (c *typedClient) UpdateStatus(ctx context.Context, obj runtime.Object) error {
func (c *typedClient) UpdateStatus(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
o, err := c.cache.getObjMeta(obj)
if err != nil {
return err
Expand All @@ -156,6 +156,32 @@ func (c *typedClient) UpdateStatus(ctx context.Context, obj runtime.Object) erro
Name(o.GetName()).
SubResource("status").
Body(obj).
VersionedParams((&UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions(), c.paramCodec).
Context(ctx).
Do().
Into(obj)
}

// PatchStatus used by StatusWriter to write status.
func (c *typedClient) PatchStatus(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOptionFunc) error {
o, err := c.cache.getObjMeta(obj)
if err != nil {
return err
}

data, err := patch.Data(obj)
if err != nil {
return err
}

patchOpts := &PatchOptions{}
return o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
SubResource("status").
Body(data).
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).
Context(ctx).
Do().
Into(obj)
Expand Down
27 changes: 25 additions & 2 deletions pkg/client/unstructured_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (uc *unstructuredClient) List(_ context.Context, obj runtime.Object, opts .
return nil
}

func (uc *unstructuredClient) UpdateStatus(_ context.Context, obj runtime.Object) error {
func (uc *unstructuredClient) UpdateStatus(_ context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
Expand All @@ -169,7 +169,30 @@ func (uc *unstructuredClient) UpdateStatus(_ context.Context, obj runtime.Object
if err != nil {
return err
}
i, err := r.UpdateStatus(u, metav1.UpdateOptions{})
i, err := r.UpdateStatus(u, *(&UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions())
if err != nil {
return err
}
u.Object = i.Object
return nil
}

func (uc *unstructuredClient) PatchStatus(_ context.Context, obj runtime.Object, patch Patch, opts ...PatchOptionFunc) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
r, err := uc.getResourceInterface(u.GroupVersionKind(), u.GetNamespace())
if err != nil {
return err
}

data, err := patch.Data(obj)
if err != nil {
return err
}

i, err := r.Patch(u.GetName(), patch.Type(), data, *(&PatchOptions{}).ApplyOptions(opts).AsPatchOptions(), "status")
if err != nil {
return err
}
Expand Down

0 comments on commit 21c61b5

Please sign in to comment.