Skip to content

⚠ Add manager option to enable caching of unstructured objects #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions pkg/cluster/client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type ClientBuilder interface {
// for this client. This function can be called multiple times, it should append to an internal slice.
WithUncached(objs ...client.Object) ClientBuilder

// CacheUnstructured tells the client whether or not to cache unstructured objects and lists. By default,
// unstructured objects and list are not cached.
CacheUnstructured(v bool) ClientBuilder

// Build returns a new client.
Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error)
}
Expand All @@ -39,14 +43,20 @@ func NewClientBuilder() ClientBuilder {
}

type newClientBuilder struct {
uncached []client.Object
uncached []client.Object
cacheUnstructured bool
}

func (n *newClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
n.uncached = append(n.uncached, objs...)
return n
}

func (n *newClientBuilder) CacheUnstructured(v bool) ClientBuilder {
n.cacheUnstructured = v
return n
}

func (n *newClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
// Create the Client for Write operations.
c, err := client.New(config, options)
Expand All @@ -55,8 +65,9 @@ func (n *newClientBuilder) Build(cache cache.Cache, config *rest.Config, options
}

return client.NewDelegatingClient(client.NewDelegatingClientInput{
CacheReader: cache,
Client: c,
UncachedObjects: n.uncached,
CacheReader: cache,
Client: c,
UncachedObjects: n.uncached,
CacheUnstructured: n.cacheUnstructured,
})
}
5 changes: 5 additions & 0 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ type Options struct {
// for the given objects.
ClientDisableCacheFor []client.Object

// ClientCacheUnstructured tells the client that, if any cache is used, to use it
// for unstructured and unstructured list objects.
ClientCacheUnstructured bool

// DryRunClient specifies whether the client should be configured to enforce
// dryRun mode.
DryRunClient bool
Expand Down Expand Up @@ -175,6 +179,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {

writeObj, err := options.ClientBuilder.
WithUncached(options.ClientDisableCacheFor...).
CacheUnstructured(options.ClientCacheUnstructured).
Build(cache, config, clientOptions)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
return e
}

func (e *fakeClientBuilder) CacheUnstructured(_ bool) ClientBuilder {
return e
}

func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
return nil, e.err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ type Options struct {
// for the given objects.
ClientDisableCacheFor []client.Object

// ClientCacheUnstructured tells the client that, if any cache is used, to use it
// for unstructured and unstructured list objects.
ClientCacheUnstructured bool

// DryRunClient specifies whether the client should be configured to enforce
// dryRun mode.
DryRunClient bool
Expand Down Expand Up @@ -284,6 +288,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
clusterOptions.NewCache = options.NewCache
clusterOptions.ClientBuilder = options.ClientBuilder
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor
clusterOptions.ClientCacheUnstructured = options.ClientCacheUnstructured
clusterOptions.DryRunClient = options.DryRunClient
clusterOptions.EventBroadcaster = options.EventBroadcaster
})
Expand Down
4 changes: 4 additions & 0 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func (e *fakeClientBuilder) WithUncached(objs ...client.Object) ClientBuilder {
return e
}

func (e *fakeClientBuilder) CacheUnstructured(_ bool) ClientBuilder {
return e
}

func (e *fakeClientBuilder) Build(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
return nil, e.err
}
Expand Down