Skip to content

Commit

Permalink
✨ Allow configuring a default cache selector
Browse files Browse the repository at this point in the history
It is already possible to configure cache selectors per gvk, but it is
not possible to default this selector for all types. This change adds
that.
  • Loading branch information
alvaroaleman committed Nov 15, 2021
1 parent c73b143 commit 30ce6b2
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 4 deletions.
19 changes: 19 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -90,8 +91,22 @@ type Informer interface {
type ObjectSelector internal.Selector

// SelectorsByObject associate a client.Object's GVK to a field/label selector.
// Use SelectorsByObjectDefaultKey as key to specify a default selector that
// will be used for all types that do not have a selector configured.
type SelectorsByObject map[client.Object]ObjectSelector

// SelectorsByObjectDefaultKey can be used in SelectorsByObject to configure a default
// selector for all kinds that do not have a specific selector set up.
type SelectorsByObjectDefaultKey struct{ metav1.Object }

// GetObjectKind implements runtime.Object.
func (s *SelectorsByObjectDefaultKey) GetObjectKind() schema.ObjectKind {
return schema.EmptyObjectKind
}

// DeepCopyObject implements runtime.Object.
func (s *SelectorsByObjectDefaultKey) DeepCopyObject() runtime.Object { return s }

// Options are the optional arguments for creating a new InformersMap object.
type Options struct {
// Scheme is the scheme to use for mapping objects to GroupVersionKinds
Expand Down Expand Up @@ -197,6 +212,10 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
func convertToSelectorsByGVK(selectorsByObject SelectorsByObject, scheme *runtime.Scheme) (internal.SelectorsByGVK, error) {
selectorsByGVK := internal.SelectorsByGVK{}
for object, selector := range selectorsByObject {
if _, isDefault := object.(*SelectorsByObjectDefaultKey); isDefault {
selectorsByGVK[internal.DefaultSelectorKey] = internal.Selector(selector)
continue
}
gvk, err := apiutil.GVKForObject(object, scheme)
if err != nil {
return nil, err
Expand Down
98 changes: 98 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"reflect"
"sort"
"strconv"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
Expand Down Expand Up @@ -73,6 +74,33 @@ func createPodWithLabels(name, namespace string, restartPolicy corev1.RestartPol
return pod
}

func createSvc(name, namespace string, cl client.Client) client.Object {
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{{Port: 1}},
},
}
err := cl.Create(context.Background(), svc)
Expect(err).NotTo(HaveOccurred())
return svc
}

func createSA(name, namespace string, cl client.Client) client.Object {
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
err := cl.Create(context.Background(), sa)
Expect(err).NotTo(HaveOccurred())
return sa
}

func createPod(name, namespace string, restartPolicy corev1.RestartPolicy) client.Object {
return createPodWithLabels(name, namespace, restartPolicy, nil)
}
Expand All @@ -93,6 +121,76 @@ var _ = Describe("Multi-Namespace Informer Cache", func() {
var _ = Describe("Informer Cache without DeepCopy", func() {
CacheTest(cache.New, cache.Options{UnsafeDisableDeepCopyByObject: cache.DisableDeepCopyByObject{cache.ObjectAll{}: true}})
})
var _ = Describe("Cache with selectors", func() {
defer GinkgoRecover()
var (
informerCache cache.Cache
informerCacheCtx context.Context
informerCacheCancel context.CancelFunc
)

BeforeEach(func() {
informerCacheCtx, informerCacheCancel = context.WithCancel(context.Background())
Expect(cfg).NotTo(BeNil())
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
err = ensureNamespace(testNamespaceOne, cl)
Expect(err).NotTo(HaveOccurred())
err = ensureNamespace(testNamespaceTwo, cl)
Expect(err).NotTo(HaveOccurred())
for idx, namespace := range []string{testNamespaceOne, testNamespaceTwo} {
_ = createSA("test-sa-"+strconv.Itoa(idx), namespace, cl)
_ = createSvc("test-svc-"+strconv.Itoa(idx), namespace, cl)
}

opts := cache.Options{
SelectorsByObject: cache.SelectorsByObject{
&corev1.ServiceAccount{}: {Field: fields.OneTermEqualSelector("metadata.namespace", testNamespaceOne)},
&cache.SelectorsByObjectDefaultKey{}: cache.ObjectSelector{Field: fields.OneTermEqualSelector("metadata.namespace", testNamespaceTwo)},
},
}

By("creating the informer cache")
informerCache, err = cache.New(cfg, opts)
Expect(err).NotTo(HaveOccurred())
By("running the cache and waiting for it to sync")
// pass as an arg so that we don't race between close and re-assign
go func(ctx context.Context) {
defer GinkgoRecover()
Expect(informerCache.Start(ctx)).To(Succeed())
}(informerCacheCtx)
Expect(informerCache.WaitForCacheSync(informerCacheCtx)).To(BeTrue())
})

AfterEach(func() {
ctx := context.Background()
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
for idx, namespace := range []string{testNamespaceOne, testNamespaceTwo} {
err = cl.Delete(ctx, &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: "test-sa-" + strconv.Itoa(idx)}})
Expect(err).NotTo(HaveOccurred())
err = cl.Delete(ctx, &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: "test-svc-" + strconv.Itoa(idx)}})
Expect(err).NotTo(HaveOccurred())
}
informerCacheCancel()
})

It("Should list serviceaccounts and find exactly one in namespace "+testNamespaceOne, func() {
var sas corev1.ServiceAccountList
err := informerCache.List(informerCacheCtx, &sas)
Expect(err).NotTo(HaveOccurred())
Expect(len(sas.Items)).To(Equal(1))
Expect(sas.Items[0].Namespace).To(Equal(testNamespaceOne))
})

It("Should list services and find exactly one in namespace "+testNamespaceTwo, func() {
var svcs corev1.ServiceList
err := informerCache.List(informerCacheCtx, &svcs)
Expect(err).NotTo(HaveOccurred())
Expect(len(svcs.Items)).To(Equal(1))
Expect(svcs.Items[0].Namespace).To(Equal(testNamespaceTwo))
})
})

func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (cache.Cache, error), opts cache.Options) {
Describe("Cache test", func() {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cache/internal/informers_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,19 @@ func createStructuredListWatch(gvk schema.GroupVersionKind, ip *specificInformer
// Create a new ListWatch for the obj
return &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
ip.selectors[gvk].ApplyToList(&opts)
ip.selectors.forGVK(gvk).ApplyToList(&opts)
res := listObj.DeepCopyObject()
namespace := restrictNamespaceBySelector(ip.namespace, ip.selectors[gvk])
namespace := restrictNamespaceBySelector(ip.namespace, ip.selectors.forGVK(gvk))
isNamespaceScoped := namespace != "" && mapping.Scope.Name() != meta.RESTScopeNameRoot
err := client.Get().NamespaceIfScoped(namespace, isNamespaceScoped).Resource(mapping.Resource.Resource).VersionedParams(&opts, ip.paramCodec).Do(ctx).Into(res)
return res, err
},
// Setup the watch function
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
ip.selectors[gvk].ApplyToList(&opts)
ip.selectors.forGVK(gvk).ApplyToList(&opts)
// Watch needs to be set to true separately
opts.Watch = true
namespace := restrictNamespaceBySelector(ip.namespace, ip.selectors[gvk])
namespace := restrictNamespaceBySelector(ip.namespace, ip.selectors.forGVK(gvk))
isNamespaceScoped := namespace != "" && mapping.Scope.Name() != meta.RESTScopeNameRoot
return client.Get().NamespaceIfScoped(namespace, isNamespaceScoped).Resource(mapping.Resource.Resource).VersionedParams(&opts, ip.paramCodec).Watch(ctx)
},
Expand Down
13 changes: 13 additions & 0 deletions pkg/cache/internal/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)

var DefaultSelectorKey = schema.GroupVersionKind{}

// SelectorsByGVK associate a GroupVersionKind to a field/label selector.
type SelectorsByGVK map[schema.GroupVersionKind]Selector

func (s SelectorsByGVK) forGVK(gvk schema.GroupVersionKind) Selector {
if specific, found := s[gvk]; found {
return specific
}
if defaultSelector, found := s[DefaultSelectorKey]; found {
return defaultSelector
}

return Selector{}
}

// Selector specify the label/field selector to fill in ListOptions.
type Selector struct {
Label labels.Selector
Expand Down

0 comments on commit 30ce6b2

Please sign in to comment.