Skip to content

Commit 1088c68

Browse files
author
Shawn Hurley
committed
Adding delegating reader for get and list requests for unstructured objs
* By default the client should not create informers for get and list requsts of unstructured types.
1 parent e54e115 commit 1088c68

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

pkg/client/client_cache.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ func (c *clientCache) newResource(obj runtime.Object) (*resourceMeta, error) {
6666
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
6767
}
6868

69-
client, err := apiutil.RESTClientForGVK(gvk, c.config, c.codecs)
69+
_, isUnstructured := obj.(*unstructured.Unstructured)
70+
var client rest.Interface
71+
if isUnstructured {
72+
client, err = apiutil.RESTUnstructuredClientForGVK(gvk, c.config)
73+
} else {
74+
client, err = apiutil.RESTClientForGVK(gvk, c.config, c.codecs)
75+
}
7076
if err != nil {
7177
return nil, err
7278
}

pkg/client/split.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ limitations under the License.
1616

1717
package client
1818

19+
import (
20+
"context"
21+
22+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
)
25+
1926
// DelegatingClient forms an interface Client by composing separate
2027
// reader, writer and statusclient interfaces. This way, you can have an Client that
2128
// reads from a cache and writes to the API server.
@@ -24,3 +31,29 @@ type DelegatingClient struct {
2431
Writer
2532
StatusClient
2633
}
34+
35+
// DelegatingReader forms a interface Reader that will cause Get and List
36+
// requests for unstructured types to use the ClientReader while
37+
// requests for any other type of object with use the CacheReader.
38+
type DelegatingReader struct {
39+
CacheReader Reader
40+
ClientReader Reader
41+
}
42+
43+
// Get retrieves an obj for a given object key from the Kubernetes Cluster.
44+
func (d *DelegatingReader) Get(ctx context.Context, key ObjectKey, obj runtime.Object) error {
45+
_, isUnstructured := obj.(*unstructured.Unstructured)
46+
if isUnstructured {
47+
return d.ClientReader.Get(ctx, key, obj)
48+
}
49+
return d.CacheReader.Get(ctx, key, obj)
50+
}
51+
52+
// List retrieves list of objects for a given namespace and list options.
53+
func (d *DelegatingReader) List(ctx context.Context, opts *ListOptions, list runtime.Object) error {
54+
_, isUnstructured := list.(*unstructured.UnstructuredList)
55+
if isUnstructured {
56+
return d.ClientReader.List(ctx, opts, list)
57+
}
58+
return d.CacheReader.List(ctx, opts, list)
59+
}

pkg/manager/manager.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,19 @@ func New(config *rest.Config, options Options) (Manager, error) {
166166
}
167167

168168
return &controllerManager{
169-
config: config,
170-
scheme: options.Scheme,
171-
errChan: make(chan error),
172-
cache: cache,
173-
fieldIndexes: cache,
174-
client: client.DelegatingClient{Reader: cache, Writer: writeObj, StatusClient: writeObj},
169+
config: config,
170+
scheme: options.Scheme,
171+
errChan: make(chan error),
172+
cache: cache,
173+
fieldIndexes: cache,
174+
client: client.DelegatingClient{
175+
Reader: &client.DelegatingReader{
176+
CacheReader: cache,
177+
ClientReader: writeObj,
178+
},
179+
Writer: writeObj,
180+
StatusClient: writeObj,
181+
},
175182
recorderProvider: recorderProvider,
176183
resourceLock: resourceLock,
177184
}, nil

0 commit comments

Comments
 (0)