Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-0.18] 🐛 Cache: Fix label defaulting of byObject when namespaces are configured #2808

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,19 +434,6 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
}
}

for namespace, cfg := range opts.DefaultNamespaces {
cfg = defaultConfig(cfg, optionDefaultsToConfig(&opts))
if namespace == metav1.NamespaceAll {
cfg.FieldSelector = fields.AndSelectors(
appendIfNotNil(
namespaceAllSelector(maps.Keys(opts.DefaultNamespaces)),
cfg.FieldSelector,
)...,
)
}
opts.DefaultNamespaces[namespace] = cfg
}

for obj, byObject := range opts.ByObject {
isNamespaced, err := apiutil.IsObjectNamespaced(obj, opts.Scheme, opts.Mapper)
if err != nil {
Expand Down Expand Up @@ -500,6 +487,22 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
opts.ByObject[obj] = byObject
}

// Default namespaces after byObject has been defaulted, otherwise a namespace without selectors
// will get the `Default` selectors, then get copied to byObject and then not get defaulted from
// byObject, as it already has selectors.
for namespace, cfg := range opts.DefaultNamespaces {
cfg = defaultConfig(cfg, optionDefaultsToConfig(&opts))
if namespace == metav1.NamespaceAll {
cfg.FieldSelector = fields.AndSelectors(
appendIfNotNil(
namespaceAllSelector(maps.Keys(opts.DefaultNamespaces)),
cfg.FieldSelector,
)...,
)
}
opts.DefaultNamespaces[namespace] = cfg
}

// Default the resync period to 10 hours if unset
if opts.SyncPeriod == nil {
opts.SyncPeriod = &defaultSyncPeriod
Expand Down
20 changes: 20 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,26 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
}},
expectedPods: []string{"test-pod-4"},
}),
Entry("namespaces configured, type-level label selector matches everything, overrides global selector", selectorsTestCase{
options: cache.Options{
DefaultNamespaces: map[string]cache.Config{testNamespaceOne: {}},
ByObject: map[client.Object]cache.ByObject{
&corev1.Pod{}: {Label: labels.Everything()},
},
DefaultLabelSelector: labels.SelectorFromSet(map[string]string{"does-not": "match-anything"}),
},
expectedPods: []string{"test-pod-1", "test-pod-5"},
}),
Entry("namespaces configured, global selector is used", selectorsTestCase{
options: cache.Options{
DefaultNamespaces: map[string]cache.Config{testNamespaceTwo: {}},
ByObject: map[client.Object]cache.ByObject{
&corev1.Pod{}: {},
},
DefaultLabelSelector: labels.SelectorFromSet(map[string]string{"common-label": "common"}),
},
expectedPods: []string{"test-pod-3"},
}),
Entry("global label selector matches one pod", selectorsTestCase{
options: cache.Options{
DefaultLabelSelector: labels.SelectorFromSet(map[string]string{
Expand Down