Skip to content

Commit b015843

Browse files
joelanfordk8s-infra-cherrypick-robot
authored and
k8s-infra-cherrypick-robot
committed
fix: cache should list out of global cache when present and necessary
When the cache options are configured with DefaultNamespaces which include an entry with `cache.AllNamespaces`, listing from the cache should fallback to the global cache if there are no namespace-specific caches that match the namespace from the list options. Signed-off-by: Joe Lanford <[email protected]>
1 parent 52b1791 commit b015843

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

pkg/cache/cache_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ var _ = Describe("Informer Cache with ReaderFailOnMissingInformer", func() {
130130
var _ = Describe("Multi-Namespace Informer Cache", func() {
131131
CacheTest(cache.New, cache.Options{
132132
DefaultNamespaces: map[string]cache.Config{
133-
testNamespaceOne: {},
134-
testNamespaceTwo: {},
135-
"default": {},
133+
cache.AllNamespaces: {FieldSelector: fields.OneTermEqualSelector("metadata.namespace", testNamespaceOne)},
134+
testNamespaceTwo: {},
135+
"default": {},
136136
},
137137
})
138138
NonBlockingGetTest(cache.New, cache.Options{
139139
DefaultNamespaces: map[string]cache.Config{
140-
testNamespaceOne: {},
141-
testNamespaceTwo: {},
142-
"default": {},
140+
cache.AllNamespaces: {FieldSelector: fields.OneTermEqualSelector("metadata.namespace", testNamespaceOne)},
141+
testNamespaceTwo: {},
142+
"default": {},
143143
},
144144
})
145145
})

pkg/cache/multi_namespace_cache.go

+3
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ func (c *multiNamespaceCache) List(ctx context.Context, list client.ObjectList,
262262
if listOpts.Namespace != corev1.NamespaceAll {
263263
cache, ok := c.namespaceToCache[listOpts.Namespace]
264264
if !ok {
265+
if global, hasGlobal := c.namespaceToCache[AllNamespaces]; hasGlobal {
266+
return global.List(ctx, list, opts...)
267+
}
265268
return fmt.Errorf("unable to list: %v because of unknown namespace for the cache", listOpts.Namespace)
266269
}
267270
return cache.List(ctx, list, opts...)

0 commit comments

Comments
 (0)