From 55353dc8e0635784a1a7e7214b3242d600844a25 Mon Sep 17 00:00:00 2001 From: Max Smythe Date: Fri, 12 May 2023 18:09:18 -0700 Subject: [PATCH] Refactor functional parameters Signed-off-by: Max Smythe --- pkg/cache/cache.go | 8 ++++++-- pkg/cache/informer_cache.go | 33 ++++++------------------------ pkg/cache/internal/informers.go | 19 +++++++++++++++-- pkg/cache/multi_namespace_cache.go | 11 +++++----- 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index f01de43810..4d3c8c868e 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -41,6 +41,10 @@ import ( var ( log = logf.RuntimeLog.WithName("object-cache") defaultSyncPeriod = 10 * time.Hour + + // BlockUntilSynced determines whether a get request for an informer should block + // until the informer's cache has synced. + BlockUntilSynced = internal.BlockUntilSynced ) // Cache knows how to load Kubernetes objects, fetch informers to request @@ -60,11 +64,11 @@ type Cache interface { type Informers interface { // GetInformer fetches or constructs an informer for the given object that corresponds to a single // API kind and resource. - GetInformer(ctx context.Context, obj client.Object) (Informer, error) + GetInformer(ctx context.Context, obj client.Object, opts ...internal.InformerGetOption) (Informer, error) // GetInformerForKind is similar to GetInformer, except that it takes a group-version-kind, instead // of the underlying object. - GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error) + GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, opts ...internal.InformerGetOption) (Informer, error) // Start runs all the informers known to this cache until the context is closed. // It blocks. diff --git a/pkg/cache/informer_cache.go b/pkg/cache/informer_cache.go index d2cba56c3c..71a5d172a4 100644 --- a/pkg/cache/informer_cache.go +++ b/pkg/cache/informer_cache.go @@ -21,7 +21,6 @@ import ( "fmt" "strings" - apierrors "k8s.io/apimachinery/pkg/api/errors" apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -39,16 +38,6 @@ var ( _ Cache = &informerCache{} ) -type InformerGetOption func(*internal.GetOptions) - -// BlockUntilSynced determines whether a get request for an informer should block -// until the informer's cache has synced. -func BlockUntilSynced(shouldBlock bool) InformerGetOption { - return func(opts *internal.GetOptions) { - opts.DoNotBlockUntilSynced = !shouldBlock - } -} - // ErrCacheNotStarted is returned when trying to read from the cache that wasn't started. type ErrCacheNotStarted struct{} @@ -70,7 +59,7 @@ func (ic *informerCache) Get(ctx context.Context, key client.ObjectKey, out clie return err } - started, cache, err := ic.Informers.Get(ctx, gvk, out, &internal.GetOptions{}) + started, cache, err := ic.Informers.Get(ctx, gvk, out) if err != nil { return err } @@ -88,7 +77,7 @@ func (ic *informerCache) List(ctx context.Context, out client.ObjectList, opts . return err } - started, cache, err := ic.Informers.Get(ctx, *gvk, cacheTypeObj, &internal.GetOptions{}) + started, cache, err := ic.Informers.Get(ctx, *gvk, cacheTypeObj) if err != nil { return err } @@ -135,19 +124,14 @@ func (ic *informerCache) objectTypeForListObject(list client.ObjectList) (*schem } // GetInformerForKind returns the informer for the GroupVersionKind. -func (ic *informerCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, opts ...InformerGetOption) (Informer, error) { +func (ic *informerCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, opts ...internal.InformerGetOption) (Informer, error) { // Map the gvk to an object obj, err := ic.scheme.New(gvk) if err != nil { return nil, err } - cfg := &internal.GetOptions{} - for _, opt := range opts { - opt(cfg) - } - - _, i, err := ic.Informers.Get(ctx, gvk, obj, cfg) + _, i, err := ic.Informers.Get(ctx, gvk, obj, opts...) if err != nil { return nil, err } @@ -155,18 +139,13 @@ func (ic *informerCache) GetInformerForKind(ctx context.Context, gvk schema.Grou } // GetInformer returns the informer for the obj. -func (ic *informerCache) GetInformer(ctx context.Context, obj client.Object, opts ...InformerGetOption) (Informer, error) { +func (ic *informerCache) GetInformer(ctx context.Context, obj client.Object, opts ...internal.InformerGetOption) (Informer, error) { gvk, err := apiutil.GVKForObject(obj, ic.scheme) if err != nil { return nil, err } - cfg := &internal.GetOptions{} - for _, opt := range opts { - opt(cfg) - } - - _, i, err := ic.Informers.Get(ctx, gvk, obj, cfg) + _, i, err := ic.Informers.Get(ctx, gvk, obj, opts...) if err != nil { return nil, err } diff --git a/pkg/cache/internal/informers.go b/pkg/cache/internal/informers.go index 5e42f93848..c5ac83a0e6 100644 --- a/pkg/cache/internal/informers.go +++ b/pkg/cache/internal/informers.go @@ -115,6 +115,16 @@ type GetOptions struct { DoNotBlockUntilSynced bool } +type InformerGetOption func(*GetOptions) + +// BlockUntilSynced determines whether a get request for an informer should block +// until the informer's cache has synced. +func BlockUntilSynced(shouldBlock bool) InformerGetOption { + return func(opts *GetOptions) { + opts.DoNotBlockUntilSynced = !shouldBlock + } +} + // Informers create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs. // It uses a standard parameter codec constructed based on the given generated Scheme. type Informers struct { @@ -303,7 +313,7 @@ func (ip *Informers) get(gvk schema.GroupVersionKind, obj runtime.Object) (res * // Get will create a new Informer and add it to the map of specificInformersMap if none exists. Returns // the Informer from the map. -func (ip *Informers) Get(ctx context.Context, gvk schema.GroupVersionKind, obj runtime.Object, opts *GetOptions) (bool, *Cache, error) { +func (ip *Informers) Get(ctx context.Context, gvk schema.GroupVersionKind, obj runtime.Object, opts ...InformerGetOption) (bool, *Cache, error) { // Return the informer if it is found i, started, ok := ip.get(gvk, obj) if !ok { @@ -313,7 +323,12 @@ func (ip *Informers) Get(ctx context.Context, gvk schema.GroupVersionKind, obj r } } - if started && !i.Informer.HasSynced() && !opts.DoNotBlockUntilSynced { + cfg := &GetOptions{} + for _, opt := range opts { + opt(cfg) + } + + if started && !i.Informer.HasSynced() && !cfg.DoNotBlockUntilSynced { // Wait for it to sync before returning the Informer so that folks don't read from a stale cache. if !cache.WaitForCacheSync(ctx.Done(), i.Informer.HasSynced) { return started, nil, apierrors.NewTimeoutError(fmt.Sprintf("failed waiting for %T Informer to sync", obj), 0) diff --git a/pkg/cache/multi_namespace_cache.go b/pkg/cache/multi_namespace_cache.go index ac97beae94..b658cc3c7f 100644 --- a/pkg/cache/multi_namespace_cache.go +++ b/pkg/cache/multi_namespace_cache.go @@ -27,6 +27,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/rest" toolscache "k8s.io/client-go/tools/cache" + "sigs.k8s.io/controller-runtime/pkg/cache/internal" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" ) @@ -93,7 +94,7 @@ type multiNamespaceCache struct { var _ Cache = &multiNamespaceCache{} // Methods for multiNamespaceCache to conform to the Informers interface. -func (c *multiNamespaceCache) GetInformer(ctx context.Context, obj client.Object) (Informer, error) { +func (c *multiNamespaceCache) GetInformer(ctx context.Context, obj client.Object, opts ...internal.InformerGetOption) (Informer, error) { informers := map[string]Informer{} // If the object is clusterscoped, get the informer from clusterCache, @@ -103,7 +104,7 @@ func (c *multiNamespaceCache) GetInformer(ctx context.Context, obj client.Object return nil, err } if !isNamespaced { - clusterCacheInf, err := c.clusterCache.GetInformer(ctx, obj) + clusterCacheInf, err := c.clusterCache.GetInformer(ctx, obj, opts...) if err != nil { return nil, err } @@ -123,7 +124,7 @@ func (c *multiNamespaceCache) GetInformer(ctx context.Context, obj client.Object return &multiNamespaceInformer{namespaceToInformer: informers}, nil } -func (c *multiNamespaceCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error) { +func (c *multiNamespaceCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, opts ...internal.InformerGetOption) (Informer, error) { informers := map[string]Informer{} // If the object is clusterscoped, get the informer from clusterCache, @@ -133,7 +134,7 @@ func (c *multiNamespaceCache) GetInformerForKind(ctx context.Context, gvk schema return nil, err } if !isNamespaced { - clusterCacheInf, err := c.clusterCache.GetInformerForKind(ctx, gvk) + clusterCacheInf, err := c.clusterCache.GetInformerForKind(ctx, gvk, opts...) if err != nil { return nil, err } @@ -143,7 +144,7 @@ func (c *multiNamespaceCache) GetInformerForKind(ctx context.Context, gvk schema } for ns, cache := range c.namespaceToCache { - informer, err := cache.GetInformerForKind(ctx, gvk) + informer, err := cache.GetInformerForKind(ctx, gvk, opts...) if err != nil { return nil, err }