Skip to content

Commit 6526b3a

Browse files
committed
pkg/{cluster,manager}: enable configurable caching of unstructured objects
1 parent 818a2ce commit 6526b3a

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

pkg/cluster/client_builder.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ type ClientBuilder interface {
2929
// for this client. This function can be called multiple times, it should append to an internal slice.
3030
WithUncached(objs ...client.Object) ClientBuilder
3131

32+
// CacheUnstructured tells the client whether or not to cache unstructured objects and lists. By default,
33+
// unstructured objects and list are not cached.
34+
CacheUnstructured(v bool) ClientBuilder
35+
3236
// Build returns a new client.
3337
Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error)
3438
}
@@ -39,14 +43,20 @@ func NewClientBuilder() ClientBuilder {
3943
}
4044

4145
type newClientBuilder struct {
42-
uncached []client.Object
46+
uncached []client.Object
47+
cacheUnstructured bool
4348
}
4449

4550
func (n *newClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
4651
n.uncached = append(n.uncached, objs...)
4752
return n
4853
}
4954

55+
func (n *newClientBuilder) CacheUnstructured(v bool) ClientBuilder {
56+
n.cacheUnstructured = v
57+
return n
58+
}
59+
5060
func (n *newClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
5161
// Create the Client for Write operations.
5262
c, err := client.New(config, options)
@@ -55,8 +65,9 @@ func (n *newClientBuilder) Build(cache cache.Cache, config *rest.Config, options
5565
}
5666

5767
return client.NewDelegatingClient(client.NewDelegatingClientInput{
58-
CacheReader: cache,
59-
Client: c,
60-
UncachedObjects: n.uncached,
68+
CacheReader: cache,
69+
Client: c,
70+
UncachedObjects: n.uncached,
71+
CacheUnstructured: n.cacheUnstructured,
6172
})
6273
}

pkg/cluster/cluster.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ type Options struct {
117117
// for the given objects.
118118
ClientDisableCacheFor []client.Object
119119

120+
// ClientCacheUnstructured tells the client that, if any cache is used, to use it
121+
// for unstructured and unstructured list objects.
122+
ClientCacheUnstructured bool
123+
120124
// DryRunClient specifies whether the client should be configured to enforce
121125
// dryRun mode.
122126
DryRunClient bool
@@ -175,6 +179,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
175179

176180
writeObj, err := options.ClientBuilder.
177181
WithUncached(options.ClientDisableCacheFor...).
182+
CacheUnstructured(options.ClientCacheUnstructured).
178183
Build(cache, config, clientOptions)
179184
if err != nil {
180185
return nil, err

pkg/cluster/cluster_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
4343
return e
4444
}
4545

46+
func (e *fakeClientBuilder) CacheUnstructured(_ bool) ClientBuilder {
47+
return e
48+
}
49+
4650
func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
4751
return nil, e.err
4852
}

pkg/manager/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ type Options struct {
213213
// for the given objects.
214214
ClientDisableCacheFor []client.Object
215215

216+
// ClientCacheUnstructured tells the client that, if any cache is used, to use it
217+
// for unstructured and unstructured list objects.
218+
ClientCacheUnstructured bool
219+
216220
// DryRunClient specifies whether the client should be configured to enforce
217221
// dryRun mode.
218222
DryRunClient bool
@@ -284,6 +288,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
284288
clusterOptions.NewCache = options.NewCache
285289
clusterOptions.ClientBuilder = options.ClientBuilder
286290
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor
291+
clusterOptions.ClientCacheUnstructured = options.ClientCacheUnstructured
287292
clusterOptions.DryRunClient = options.DryRunClient
288293
clusterOptions.EventBroadcaster = options.EventBroadcaster
289294
})

pkg/manager/manager_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
6262
return e
6363
}
6464

65+
func (e *fakeClientBuilder) CacheUnstructured(_ bool) ClientBuilder {
66+
return e
67+
}
68+
6569
func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
6670
return nil, e.err
6771
}

0 commit comments

Comments
 (0)