From d9ecde9093c4b00fb4cd984cc7b11579b8842392 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 | 32 ++++++-------------------------- pkg/cache/internal/informers.go | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 30 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..6f6da55c6e 100644 --- a/pkg/cache/informer_cache.go +++ b/pkg/cache/informer_cache.go @@ -39,16 +39,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 +60,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 +78,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 +125,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 +140,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)