From 6526b3a5f229ae821d73f05c356147dc568516f4 Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Thu, 14 Jan 2021 13:35:29 -0500 Subject: [PATCH] pkg/{cluster,manager}: enable configurable caching of unstructured objects --- pkg/cluster/client_builder.go | 19 +++++++++++++++---- pkg/cluster/cluster.go | 5 +++++ pkg/cluster/cluster_test.go | 4 ++++ pkg/manager/manager.go | 5 +++++ pkg/manager/manager_test.go | 4 ++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/cluster/client_builder.go b/pkg/cluster/client_builder.go index 791ce16061..eea2512324 100644 --- a/pkg/cluster/client_builder.go +++ b/pkg/cluster/client_builder.go @@ -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) } @@ -39,7 +43,8 @@ func NewClientBuilder() ClientBuilder { } type newClientBuilder struct { - uncached []client.Object + uncached []client.Object + cacheUnstructured bool } func (n *newClientBuilder) WithUncached(objs ...client.Object) ClientBuilder { @@ -47,6 +52,11 @@ func (n *newClientBuilder) WithUncached(objs ...client.Object) ClientBuilder { 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) @@ -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, }) } diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 2c7b22382d..a692ba9281 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -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 @@ -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 diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index c407869fa0..1bba840bdd 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -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 } diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 2f676ad7a0..d251b932cb 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -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 @@ -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 }) diff --git a/pkg/manager/manager_test.go b/pkg/manager/manager_test.go index be5c5c3c0a..f0e105c000 100644 --- a/pkg/manager/manager_test.go +++ b/pkg/manager/manager_test.go @@ -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 }