Skip to content

Commit

Permalink
create a cluser scoped cache separately
Browse files Browse the repository at this point in the history
Signed-off-by: varshaprasad96 <varshaprasad96@gmail.com>
  • Loading branch information
varshaprasad96 committed May 25, 2021
1 parent 50bfd90 commit 56f0e9b
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions pkg/cache/multi_namespace_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const globalCache = "_cluster-scope"
// MultiNamespacedCacheBuilder - Builder function to create a new multi-namespaced cache.
// This will scope the cache to a list of namespaces. Listing for all namespaces
// will list for all the namespaces that this knows about. By default this will create
// a global cache for cluster scoped resource (having empty namespace). Note that this is not intended
// a global cache for cluster scoped resource. Note that this is not intended
// to be used for excluding namespaces, this is better done via a Predicate. Also note that
// you may face performance issues when using this with a high number of namespaces.
func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc {
Expand All @@ -59,9 +59,6 @@ func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc {
return nil, fmt.Errorf("error creating global cache %v", err)
}

// add global cache to the cacheMap
caches[globalCache] = gCache

for _, ns := range namespaces {
opts.Namespace = ns
c, err := New(config, opts)
Expand All @@ -70,7 +67,7 @@ func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc {
}
caches[ns] = c
}
return &multiNamespaceCache{namespaceToCache: caches, Scheme: opts.Scheme, RESTMapper: opts.Mapper}, nil
return &multiNamespaceCache{namespaceToCache: caches, Scheme: opts.Scheme, RESTMapper: opts.Mapper, ClusterCache: gCache}, nil
}
}

Expand All @@ -82,6 +79,7 @@ type multiNamespaceCache struct {
namespaceToCache map[string]Cache
Scheme *runtime.Scheme
RESTMapper meta.RESTMapper
ClusterCache Cache
}

var _ Cache = &multiNamespaceCache{}
Expand All @@ -96,6 +94,12 @@ func (c *multiNamespaceCache) GetInformer(ctx context.Context, obj client.Object
}
informers[ns] = informer
}
clusterCacheInf, err := c.ClusterCache.GetInformer(ctx, obj)
if err != nil {
return nil, err
}
informers[globalCache] = clusterCacheInf

return &multiNamespaceInformer{namespaceToInformer: informers}, nil
}

Expand All @@ -108,10 +112,24 @@ func (c *multiNamespaceCache) GetInformerForKind(ctx context.Context, gvk schema
}
informers[ns] = informer
}
clusterCacheInf, err := c.ClusterCache.GetInformerForKind(ctx, gvk)
if err != nil {
return nil, err
}
informers[globalCache] = clusterCacheInf
return &multiNamespaceInformer{namespaceToInformer: informers}, nil
}

func (c *multiNamespaceCache) Start(ctx context.Context) error {
// start global cache
go func() {
err := c.ClusterCache.Start(ctx)
if err != nil {
log.Error(err, "cluster scoped cache failed to start")
}
}()

// start namespaced caches
for ns, cache := range c.namespaceToCache {
go func(ns string, cache Cache) {
err := cache.Start(ctx)
Expand All @@ -120,6 +138,7 @@ func (c *multiNamespaceCache) Start(ctx context.Context) error {
}
}(ns, cache)
}

<-ctx.Done()
return nil
}
Expand All @@ -131,6 +150,11 @@ func (c *multiNamespaceCache) WaitForCacheSync(ctx context.Context) bool {
synced = s
}
}

// check if cluster scoped cache has synced
if !c.ClusterCache.WaitForCacheSync(ctx) {
synced = false
}
return synced
}

Expand All @@ -140,6 +164,10 @@ func (c *multiNamespaceCache) IndexField(ctx context.Context, obj client.Object,
return err
}
}

if err := c.ClusterCache.IndexField(ctx, obj, field, extractValue); err != nil {
return fmt.Errorf("error adding index on object with cluster scoped cache %v", err)
}
return nil
}

Expand All @@ -151,8 +179,7 @@ func (c *multiNamespaceCache) Get(ctx context.Context, key client.ObjectKey, obj

if !isNamespaced {
// Look into the global cache to fetch the object
cache := c.namespaceToCache[globalCache]
return cache.Get(ctx, key, obj)
return c.ClusterCache.Get(ctx, key, obj)
}

cache, ok := c.namespaceToCache[key.Namespace]
Expand All @@ -174,8 +201,7 @@ func (c *multiNamespaceCache) List(ctx context.Context, list client.ObjectList,

if !isNamespaced {
// Look at the global cache to get the objects with the specified GVK
cache := c.namespaceToCache[globalCache]
return cache.List(ctx, list, opts...)
return c.ClusterCache.List(ctx, list, opts...)
}

if listOpts.Namespace != corev1.NamespaceAll {
Expand All @@ -199,10 +225,7 @@ func (c *multiNamespaceCache) List(ctx context.Context, list client.ObjectList,
limitSet := listOpts.Limit > 0

var resourceVersion string
for ns, cache := range c.namespaceToCache {
if ns == globalCache {
continue
}
for _, cache := range c.namespaceToCache {
listObj := list.DeepCopyObject().(client.ObjectList)
err = cache.List(ctx, listObj, &listOpts)
if err != nil {
Expand Down

0 comments on commit 56f0e9b

Please sign in to comment.