Skip to content

Commit

Permalink
✨ Add cluster.NewClientFunc with options
Browse files Browse the repository at this point in the history
Motivated by the ability to create a client caching Unstructured when configuring manager.
  • Loading branch information
erikgb committed Nov 25, 2022
1 parent accd262 commit c7c2185
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions pkg/cluster/cluster.go
Expand Up @@ -112,6 +112,7 @@ type Options struct {
// NewClient is the func that creates the client to be used by the manager.
// If not set this will create the default DelegatingClient that will
// use the cache for reads and the client for writes.
// NOTE: The default client will not cache Unstructured.
NewClient NewClientFunc

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

// DefaultNewClient creates the default caching client.
type ClientOptions struct {
UncachedObjects []client.Object
CacheUnstructured bool
}

// DefaultNewClient creates the default caching client, that will never cache Unstructured.
func DefaultNewClient(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
c, err := client.New(config, options)
if err != nil {
return nil, err
}
return ClientBuilderWithOptions(ClientOptions{})(cache, config, options, uncachedObjects...)
}

return client.NewDelegatingClient(client.NewDelegatingClientInput{
CacheReader: cache,
Client: c,
UncachedObjects: uncachedObjects,
})
func ClientBuilderWithOptions(options ClientOptions) NewClientFunc {
return func(cache cache.Cache, config *rest.Config, clientOpts client.Options, uncachedObjects ...client.Object) (client.Client, error) {
c, err := client.New(config, clientOpts)
if err != nil {
return nil, err
}

if uncachedObjects != nil {
options.UncachedObjects = append(options.UncachedObjects, uncachedObjects...)
}

return client.NewDelegatingClient(client.NewDelegatingClientInput{
CacheReader: cache,
Client: c,
UncachedObjects: options.UncachedObjects,
CacheUnstructured: options.CacheUnstructured,
})
}
}

0 comments on commit c7c2185

Please sign in to comment.