Skip to content

Commit

Permalink
Provide single cache option
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Jul 19, 2023
1 parent 29d383c commit e61ecb8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
24 changes: 2 additions & 22 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -222,7 +217,6 @@ type client struct {

cache Reader
uncachedGVKs map[schema.GroupVersionKind]struct{}
failOnUncached bool
cacheUnstructured bool
}

Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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")
Expand Down
18 changes: 2 additions & 16 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e61ecb8

Please sign in to comment.