This is a short blog to let you know that objects represented in unstructured.Unstructured will not be cached by the client returned from manager.Manager by default.
UPDATED on Jan. 9th, 2023
The trick described in this article is no longer needed for controller-runtime version 0.14.0 or later.
ref. github.com
This also means that in-memory indexing and retrieving of unstructured objects will not work by default.
To enable caching for unstructured objects, define a custom function to create a client like this:
func NewCachingClient(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 client.NewDelegatingClient(client.NewDelegatingClientInput{ CacheReader: cache, Client: c, UncachedObjects: uncachedObjects, // THIS IS THE MAGIC CacheUnstructured: true, }) }
and give it when creating manager.Manager as follows:
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ NewClient: NewCachingClient, ...
Read the rest of the article if you'd like to know more about what's happening under the hood.
Read more