Skip to content

Commit 007d240

Browse files
authored
Merge pull request #2054 from erikgb/feat/cache-unstructured-client
✨ Add cluster.NewClientFunc with options
2 parents ea26ea4 + cdbd4b7 commit 007d240

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

pkg/cluster/cluster.go

+28-10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ type Options struct {
112112
// NewClient is the func that creates the client to be used by the manager.
113113
// If not set this will create the default DelegatingClient that will
114114
// use the cache for reads and the client for writes.
115+
// NOTE: The default client will not cache Unstructured.
115116
NewClient NewClientFunc
116117

117118
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
@@ -255,16 +256,33 @@ func setOptionsDefaults(options Options) Options {
255256
// NewClientFunc allows a user to define how to create a client.
256257
type NewClientFunc func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error)
257258

258-
// DefaultNewClient creates the default caching client.
259+
// ClientOptions are the optional arguments for tuning the caching client.
260+
type ClientOptions struct {
261+
UncachedObjects []client.Object
262+
CacheUnstructured bool
263+
}
264+
265+
// DefaultNewClient creates the default caching client, that will never cache Unstructured.
259266
func DefaultNewClient(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
260-
c, err := client.New(config, options)
261-
if err != nil {
262-
return nil, err
263-
}
267+
return ClientBuilderWithOptions(ClientOptions{})(cache, config, options, uncachedObjects...)
268+
}
269+
270+
// ClientBuilderWithOptions returns a Client constructor that will build a client
271+
// honoring the options argument
272+
func ClientBuilderWithOptions(options ClientOptions) NewClientFunc {
273+
return func(cache cache.Cache, config *rest.Config, clientOpts client.Options, uncachedObjects ...client.Object) (client.Client, error) {
274+
options.UncachedObjects = append(options.UncachedObjects, uncachedObjects...)
264275

265-
return client.NewDelegatingClient(client.NewDelegatingClientInput{
266-
CacheReader: cache,
267-
Client: c,
268-
UncachedObjects: uncachedObjects,
269-
})
276+
c, err := client.New(config, clientOpts)
277+
if err != nil {
278+
return nil, err
279+
}
280+
281+
return client.NewDelegatingClient(client.NewDelegatingClientInput{
282+
CacheReader: cache,
283+
Client: c,
284+
UncachedObjects: options.UncachedObjects,
285+
CacheUnstructured: options.CacheUnstructured,
286+
})
287+
}
270288
}

0 commit comments

Comments
 (0)