Skip to content

Commit f469197

Browse files
authored
Merge pull request #1917 from FillZpp/add-client-GetOptions
⚠ Add GetOptions as optional argument of client.Reader and all its implementation
2 parents 196828e + 6533c61 commit f469197

17 files changed

+105
-23
lines changed

pkg/cache/informer_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type informerCache struct {
5151
}
5252

5353
// Get implements Reader.
54-
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out client.Object) error {
54+
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out client.Object, opts ...client.GetOption) error {
5555
gvk, err := apiutil.GVKForObject(out, ip.Scheme)
5656
if err != nil {
5757
return err

pkg/cache/informertest/fake_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (c *FakeInformers) IndexField(ctx context.Context, obj client.Object, field
131131
}
132132

133133
// Get implements Cache.
134-
func (c *FakeInformers) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
134+
func (c *FakeInformers) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
135135
return nil
136136
}
137137

pkg/cache/internal/cache_reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type CacheReader struct {
5454
}
5555

5656
// Get checks the indexer for the object and writes a copy of it if found.
57-
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object) error {
57+
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object, opts ...client.GetOption) error {
5858
if c.scopeName == apimeta.RESTScopeNameRoot {
5959
key.Namespace = ""
6060
}

pkg/cache/multi_namespace_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (c *multiNamespaceCache) IndexField(ctx context.Context, obj client.Object,
200200
return nil
201201
}
202202

203-
func (c *multiNamespaceCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
203+
func (c *multiNamespaceCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
204204
isNamespaced, err := objectutil.IsAPINamespaced(obj, c.Scheme, c.RESTMapper)
205205
if err != nil {
206206
return err

pkg/client/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,16 @@ func (c *client) Patch(ctx context.Context, obj Object, patch Patch, opts ...Pat
241241
}
242242

243243
// Get implements client.Client.
244-
func (c *client) Get(ctx context.Context, key ObjectKey, obj Object) error {
244+
func (c *client) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
245245
switch obj.(type) {
246246
case *unstructured.Unstructured:
247-
return c.unstructuredClient.Get(ctx, key, obj)
247+
return c.unstructuredClient.Get(ctx, key, obj, opts...)
248248
case *metav1.PartialObjectMetadata:
249249
// Metadata only object should always preserve the GVK coming in from the caller.
250250
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
251-
return c.metadataClient.Get(ctx, key, obj)
251+
return c.metadataClient.Get(ctx, key, obj, opts...)
252252
default:
253-
return c.typedClient.Get(ctx, key, obj)
253+
return c.typedClient.Get(ctx, key, obj, opts...)
254254
}
255255
}
256256

pkg/client/client_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2913,6 +2913,24 @@ var _ = Describe("Client", func() {
29132913
})
29142914
})
29152915

2916+
Describe("GetOptions", func() {
2917+
It("should be convertable to metav1.GetOptions", func() {
2918+
o := (&client.GetOptions{}).ApplyOptions([]client.GetOption{
2919+
&client.GetOptions{Raw: &metav1.GetOptions{ResourceVersion: "RV0"}},
2920+
})
2921+
mo := o.AsGetOptions()
2922+
Expect(mo).NotTo(BeNil())
2923+
Expect(mo.ResourceVersion).To(Equal("RV0"))
2924+
})
2925+
2926+
It("should produce empty metav1.GetOptions if nil", func() {
2927+
var o *client.GetOptions
2928+
Expect(o.AsGetOptions()).To(Equal(&metav1.GetOptions{}))
2929+
o = &client.GetOptions{}
2930+
Expect(o.AsGetOptions()).To(Equal(&metav1.GetOptions{}))
2931+
})
2932+
})
2933+
29162934
Describe("ListOptions", func() {
29172935
It("should be convertable to metav1.ListOptions", func() {
29182936
lo := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
@@ -3389,7 +3407,7 @@ type fakeReader struct {
33893407
Called int
33903408
}
33913409

3392-
func (f *fakeReader) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
3410+
func (f *fakeReader) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
33933411
f.Called++
33943412
return nil
33953413
}

pkg/client/dryrun.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func (c *dryRunClient) Patch(ctx context.Context, obj Object, patch Patch, opts
7272
}
7373

7474
// Get implements client.Client.
75-
func (c *dryRunClient) Get(ctx context.Context, key ObjectKey, obj Object) error {
76-
return c.client.Get(ctx, key, obj)
75+
func (c *dryRunClient) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
76+
return c.client.Get(ctx, key, obj, opts...)
7777
}
7878

7979
// List implements client.Client.

pkg/client/fake/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
329329
return t.ObjectTracker.Update(gvr, obj, ns)
330330
}
331331

332-
func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
332+
func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
333333
gvr, err := getGVRFromObject(obj, c.scheme)
334334
if err != nil {
335335
return err

pkg/client/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Reader interface {
5050
// Get retrieves an obj for the given object key from the Kubernetes Cluster.
5151
// obj must be a struct pointer so that obj can be updated with the response
5252
// returned by the Server.
53-
Get(ctx context.Context, key ObjectKey, obj Object) error
53+
Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error
5454

5555
// List retrieves list of objects for a given namespace and list options. On a
5656
// successful call, Items field in the list will be populated with the

pkg/client/metadata_client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,23 @@ func (mc *metadataClient) Patch(ctx context.Context, obj Object, patch Patch, op
116116
}
117117

118118
// Get implements client.Client.
119-
func (mc *metadataClient) Get(ctx context.Context, key ObjectKey, obj Object) error {
119+
func (mc *metadataClient) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
120120
metadata, ok := obj.(*metav1.PartialObjectMetadata)
121121
if !ok {
122122
return fmt.Errorf("metadata client did not understand object: %T", obj)
123123
}
124124

125125
gvk := metadata.GroupVersionKind()
126126

127+
getOpts := GetOptions{}
128+
getOpts.ApplyOptions(opts)
129+
127130
resInt, err := mc.getResourceInterface(gvk, key.Namespace)
128131
if err != nil {
129132
return err
130133
}
131134

132-
res, err := resInt.Get(ctx, key.Name, metav1.GetOptions{})
135+
res, err := resInt.Get(ctx, key.Name, *getOpts.AsGetOptions())
133136
if err != nil {
134137
return err
135138
}

0 commit comments

Comments
 (0)