Skip to content

Commit b1c2663

Browse files
committed
Implement options in clients
1 parent e2c92af commit b1c2663

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

pkg/client/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,18 @@ type client struct {
107107
func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateOptionFunc) error {
108108
_, ok := obj.(*unstructured.Unstructured)
109109
if ok {
110-
return c.unstructuredClient.Create(ctx, obj)
110+
return c.unstructuredClient.Create(ctx, obj, opts...)
111111
}
112-
return c.typedClient.Create(ctx, obj)
112+
return c.typedClient.Create(ctx, obj, opts...)
113113
}
114114

115115
// Update implements client.Client
116116
func (c *client) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
117117
_, ok := obj.(*unstructured.Unstructured)
118118
if ok {
119-
return c.unstructuredClient.Update(ctx, obj)
119+
return c.unstructuredClient.Update(ctx, obj, opts...)
120120
}
121-
return c.typedClient.Update(ctx, obj)
121+
return c.typedClient.Update(ctx, obj, opts...)
122122
}
123123

124124
// Delete implements client.Client

pkg/client/typed_client.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,39 @@ type typedClient struct {
3030
}
3131

3232
// Create implements client.Client
33-
func (c *typedClient) Create(ctx context.Context, obj runtime.Object) error {
33+
func (c *typedClient) Create(ctx context.Context, obj runtime.Object, opts ...CreateOptionFunc) error {
3434
o, err := c.cache.getObjMeta(obj)
3535
if err != nil {
3636
return err
3737
}
38+
39+
createOpts := &CreateOptions{}
40+
createOpts.ApplyOptions(opts)
3841
return o.Post().
3942
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
4043
Resource(o.resource()).
4144
Body(obj).
45+
VersionedParams(createOpts.AsCreateOptions(), c.paramCodec).
4246
Context(ctx).
4347
Do().
4448
Into(obj)
4549
}
4650

4751
// Update implements client.Client
48-
func (c *typedClient) Update(ctx context.Context, obj runtime.Object) error {
52+
func (c *typedClient) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
4953
o, err := c.cache.getObjMeta(obj)
5054
if err != nil {
5155
return err
5256
}
57+
58+
updateOpts := &UpdateOptions{}
59+
updateOpts.ApplyOptions(opts)
5360
return o.Put().
5461
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
5562
Resource(o.resource()).
5663
Name(o.GetName()).
5764
Body(obj).
65+
VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
5866
Context(ctx).
5967
Do().
6068
Into(obj)

pkg/client/unstructured_client.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ type unstructuredClient struct {
3737
}
3838

3939
// Create implements client.Client
40-
func (uc *unstructuredClient) Create(_ context.Context, obj runtime.Object) error {
40+
func (uc *unstructuredClient) Create(_ context.Context, obj runtime.Object, opts ...CreateOptionFunc) error {
4141
u, ok := obj.(*unstructured.Unstructured)
4242
if !ok {
4343
return fmt.Errorf("unstructured client did not understand object: %T", obj)
4444
}
45+
createOpts := CreateOptions{}
46+
createOpts.ApplyOptions(opts)
4547
r, err := uc.getResourceInterface(u.GroupVersionKind(), u.GetNamespace())
4648
if err != nil {
4749
return err
4850
}
49-
i, err := r.Create(u, metav1.CreateOptions{})
51+
i, err := r.Create(u, *createOpts.AsCreateOptions())
5052
if err != nil {
5153
return err
5254
}
@@ -55,16 +57,18 @@ func (uc *unstructuredClient) Create(_ context.Context, obj runtime.Object) erro
5557
}
5658

5759
// Update implements client.Client
58-
func (uc *unstructuredClient) Update(_ context.Context, obj runtime.Object) error {
60+
func (uc *unstructuredClient) Update(_ context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
5961
u, ok := obj.(*unstructured.Unstructured)
6062
if !ok {
6163
return fmt.Errorf("unstructured client did not understand object: %T", obj)
6264
}
65+
updateOpts := UpdateOptions{}
66+
updateOpts.ApplyOptions(opts)
6367
r, err := uc.getResourceInterface(u.GroupVersionKind(), u.GetNamespace())
6468
if err != nil {
6569
return err
6670
}
67-
i, err := r.Update(u, metav1.UpdateOptions{})
71+
i, err := r.Update(u, *updateOpts.AsUpdateOptions())
6872
if err != nil {
6973
return err
7074
}

0 commit comments

Comments
 (0)