Skip to content

Commit

Permalink
pkg/{cluster,manager}: enable configurable caching of unstructured ob…
Browse files Browse the repository at this point in the history
…jects
  • Loading branch information
joelanford committed Jan 14, 2021
1 parent 818a2ce commit 6526b3a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/cluster/client_builder.go
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
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
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
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
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

0 comments on commit 6526b3a

Please sign in to comment.