From e61ecb8752226bae574ccaab32f3f1b73592a94e Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Wed, 19 Jul 2023 12:35:06 -0700 Subject: [PATCH] Provide single cache option Signed-off-by: Vince Prignano --- pkg/client/client.go | 24 ++---------------------- pkg/client/client_test.go | 18 ++---------------- 2 files changed, 4 insertions(+), 38 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index a24df8ba2e..ed9b31dd92 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -31,7 +31,6 @@ import ( "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/metadata" "k8s.io/client-go/rest" - cacheerrors "sigs.k8s.io/controller-runtime/pkg/cache/errors" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/log" @@ -85,9 +84,6 @@ type CacheOptions struct { // read unstructured objects or lists from the cache. // If false, unstructured objects will always result in a live lookup. Unstructured bool - // FailOnUnknownResource determines how the client should behave when the client - // is asked to read an object from the cache, and ErrResourceNotCached is returned. - FailOnUnknownResource bool } // NewClientFunc allows a user to define how to create a client. @@ -194,7 +190,6 @@ func newClient(config *rest.Config, options Options) (*client, error) { // We want a cache if we're here. // Set the cache. c.cache = options.Cache.Reader - c.failOnUncached = options.Cache.FailOnUnknownResource // Load uncached GVKs. c.cacheUnstructured = options.Cache.Unstructured @@ -222,7 +217,6 @@ type client struct { cache Reader uncachedGVKs map[schema.GroupVersionKind]struct{} - failOnUncached bool cacheUnstructured bool } @@ -347,9 +341,7 @@ func (c *client) Get(ctx context.Context, key ObjectKey, obj Object, opts ...Get return err } else if !isUncached { // Attempt to get from the cache. - if err := c.cache.Get(ctx, key, obj, opts...); c.shouldReturnCacheErr(err) { - return err - } + return c.cache.Get(ctx, key, obj, opts...) } // Perform a live lookup. @@ -371,9 +363,7 @@ func (c *client) List(ctx context.Context, obj ObjectList, opts ...ListOption) e return err } else if !isUncached { // Attempt to get from the cache. - if err := c.cache.List(ctx, obj, opts...); c.shouldReturnCacheErr(err) { - return err - } + return c.cache.List(ctx, obj, opts...) } // Perform a live lookup. @@ -409,16 +399,6 @@ func (c *client) List(ctx context.Context, obj ObjectList, opts ...ListOption) e } } -// shouldReturnCacheErr determines if we should return the error we got from the cache. -func (c *client) shouldReturnCacheErr(err error) bool { - if !c.failOnUncached && errors.As(err, &cacheerrors.ErrResourceNotCached{}) { - // Ignore errors if we're not configured to fail on uncached resources. - return false - } - // Return in any other case. - return true -} - // Status implements client.StatusClient. func (c *client) Status() SubResourceWriter { return c.SubResource("status") diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 86799bd217..f52101d3c2 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -282,9 +282,9 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC Expect(cache.Called).To(Equal(2)) }) - It("should use the provided reader cache if provided, but fail when configured to on cache misses for get and list", func() { + It("should propagate cache unknown resources errors", func() { c := &fakeUncachedReader{} - cl, err := client.New(cfg, client.Options{Cache: &client.CacheOptions{Reader: c, FailOnUnknownResource: true}}) + cl, err := client.New(cfg, client.Options{Cache: &client.CacheOptions{Reader: c}}) Expect(err).NotTo(HaveOccurred()) Expect(cl).NotTo(BeNil()) Expect(errors.As(cl.Get(ctx, client.ObjectKey{Name: "test"}, &appsv1.Deployment{}), &errNotCached)).To(BeTrue()) @@ -3967,20 +3967,6 @@ func (f *fakeUncachedReader) List(ctx context.Context, list client.ObjectList, o return &cacheerrors.ErrResourceNotCached{} } -type fakeErrorReader struct { - Called int -} - -func (f *fakeErrorReader) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { - f.Called++ - return errors.New("sentinel") -} - -func (f *fakeErrorReader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { - f.Called++ - return errors.New("sentinel") -} - func ptr[T any](to T) *T { return &to }