Skip to content

Commit

Permalink
✨ Add NewClientFunc to create client caching Unstructured
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgb committed Nov 21, 2022
1 parent accd262 commit f0c054d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
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,28 @@ 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.
// DefaultNewClient creates the default caching client, that will not cache Unstructured.
func DefaultNewClient(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
return newClient(cache, config, options, false, uncachedObjects)
}

// CacheUnstructuredNewClient creates a caching client that will cache Unstructured.
func CacheUnstructuredNewClient(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
return newClient(cache, config, options, true, uncachedObjects)
}

var _ NewClientFunc = CacheUnstructuredNewClient

func newClient(cache cache.Cache, config *rest.Config, options client.Options, cacheUnstructured bool, uncachedObjects []client.Object) (client.Client, error) {
c, err := client.New(config, options)
if err != nil {
return nil, err
}

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

0 comments on commit f0c054d

Please sign in to comment.