Skip to content

Commit d6feaae

Browse files
authored
Merge pull request #2808 from k8s-infra-cherrypick-robot/cherry-pick-2805-to-release-0.18
[release-0.18] 🐛 Cache: Fix label defaulting of byObject when namespaces are configured
2 parents ed81fa6 + 87cae4c commit d6feaae

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

pkg/cache/cache.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -434,19 +434,6 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
434434
}
435435
}
436436

437-
for namespace, cfg := range opts.DefaultNamespaces {
438-
cfg = defaultConfig(cfg, optionDefaultsToConfig(&opts))
439-
if namespace == metav1.NamespaceAll {
440-
cfg.FieldSelector = fields.AndSelectors(
441-
appendIfNotNil(
442-
namespaceAllSelector(maps.Keys(opts.DefaultNamespaces)),
443-
cfg.FieldSelector,
444-
)...,
445-
)
446-
}
447-
opts.DefaultNamespaces[namespace] = cfg
448-
}
449-
450437
for obj, byObject := range opts.ByObject {
451438
isNamespaced, err := apiutil.IsObjectNamespaced(obj, opts.Scheme, opts.Mapper)
452439
if err != nil {
@@ -500,6 +487,22 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
500487
opts.ByObject[obj] = byObject
501488
}
502489

490+
// Default namespaces after byObject has been defaulted, otherwise a namespace without selectors
491+
// will get the `Default` selectors, then get copied to byObject and then not get defaulted from
492+
// byObject, as it already has selectors.
493+
for namespace, cfg := range opts.DefaultNamespaces {
494+
cfg = defaultConfig(cfg, optionDefaultsToConfig(&opts))
495+
if namespace == metav1.NamespaceAll {
496+
cfg.FieldSelector = fields.AndSelectors(
497+
appendIfNotNil(
498+
namespaceAllSelector(maps.Keys(opts.DefaultNamespaces)),
499+
cfg.FieldSelector,
500+
)...,
501+
)
502+
}
503+
opts.DefaultNamespaces[namespace] = cfg
504+
}
505+
503506
// Default the resync period to 10 hours if unset
504507
if opts.SyncPeriod == nil {
505508
opts.SyncPeriod = &defaultSyncPeriod

pkg/cache/cache_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,26 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
16301630
}},
16311631
expectedPods: []string{"test-pod-4"},
16321632
}),
1633+
Entry("namespaces configured, type-level label selector matches everything, overrides global selector", selectorsTestCase{
1634+
options: cache.Options{
1635+
DefaultNamespaces: map[string]cache.Config{testNamespaceOne: {}},
1636+
ByObject: map[client.Object]cache.ByObject{
1637+
&corev1.Pod{}: {Label: labels.Everything()},
1638+
},
1639+
DefaultLabelSelector: labels.SelectorFromSet(map[string]string{"does-not": "match-anything"}),
1640+
},
1641+
expectedPods: []string{"test-pod-1", "test-pod-5"},
1642+
}),
1643+
Entry("namespaces configured, global selector is used", selectorsTestCase{
1644+
options: cache.Options{
1645+
DefaultNamespaces: map[string]cache.Config{testNamespaceTwo: {}},
1646+
ByObject: map[client.Object]cache.ByObject{
1647+
&corev1.Pod{}: {},
1648+
},
1649+
DefaultLabelSelector: labels.SelectorFromSet(map[string]string{"common-label": "common"}),
1650+
},
1651+
expectedPods: []string{"test-pod-3"},
1652+
}),
16331653
Entry("global label selector matches one pod", selectorsTestCase{
16341654
options: cache.Options{
16351655
DefaultLabelSelector: labels.SelectorFromSet(map[string]string{

0 commit comments

Comments
 (0)