Skip to content

Commit 6f1c3d8

Browse files
authored
Merge pull request #2807 from k8s-infra-cherrypick-robot/cherry-pick-2805-to-release-0.17
[release-0.17] 🐛 Cache: Fix label defaulting of byObject when namespaces are configured
2 parents dd5cbad + d1d3267 commit 6f1c3d8

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

pkg/cache/cache.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,6 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
419419
}
420420
}
421421

422-
for namespace, cfg := range opts.DefaultNamespaces {
423-
cfg = defaultConfig(cfg, optionDefaultsToConfig(&opts))
424-
if namespace == metav1.NamespaceAll {
425-
cfg.FieldSelector = fields.AndSelectors(
426-
appendIfNotNil(
427-
namespaceAllSelector(maps.Keys(opts.DefaultNamespaces)),
428-
cfg.FieldSelector,
429-
)...,
430-
)
431-
}
432-
opts.DefaultNamespaces[namespace] = cfg
433-
}
434-
435422
for obj, byObject := range opts.ByObject {
436423
isNamespaced, err := apiutil.IsObjectNamespaced(obj, opts.Scheme, opts.Mapper)
437424
if err != nil {
@@ -485,6 +472,22 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
485472
opts.ByObject[obj] = byObject
486473
}
487474

475+
// Default namespaces after byObject has been defaulted, otherwise a namespace without selectors
476+
// will get the `Default` selectors, then get copied to byObject and then not get defaulted from
477+
// byObject, as it already has selectors.
478+
for namespace, cfg := range opts.DefaultNamespaces {
479+
cfg = defaultConfig(cfg, optionDefaultsToConfig(&opts))
480+
if namespace == metav1.NamespaceAll {
481+
cfg.FieldSelector = fields.AndSelectors(
482+
appendIfNotNil(
483+
namespaceAllSelector(maps.Keys(opts.DefaultNamespaces)),
484+
cfg.FieldSelector,
485+
)...,
486+
)
487+
}
488+
opts.DefaultNamespaces[namespace] = cfg
489+
}
490+
488491
// Default the resync period to 10 hours if unset
489492
if opts.SyncPeriod == nil {
490493
opts.SyncPeriod = &defaultSyncPeriod

pkg/cache/cache_test.go

Lines changed: 20 additions & 0 deletions
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)