@@ -11,6 +11,7 @@ import (
1111
1212 "k8s.io/apimachinery/pkg/api/meta"
1313 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+ "k8s.io/apimachinery/pkg/util/sets"
1415 "sigs.k8s.io/controller-runtime/pkg/client"
1516)
1617
@@ -20,18 +21,20 @@ import (
2021// List operations.
2122type namespaceSelectorClient struct {
2223 client.Client
23- namespaceSelector * metav1.LabelSelector
24+ namespaceSelector * metav1.LabelSelector
25+ includedNamespaces sets.Set [string ]
2426}
2527
2628// newNamespaceSelectorClient creates a new namespace-filtered client wrapper.
2729// If namespaceSelector is nil, the wrapper passes through all operations unchanged.
28- func newNamespaceSelectorClient (c client.Client , namespaceSelector * metav1.LabelSelector ) client.Client {
30+ func newNamespaceSelectorClient (c client.Client , namespaceSelector * metav1.LabelSelector , includedNamespaces ... string ) client.Client {
2931 if namespaceSelector == nil {
3032 return c
3133 }
3234 return & namespaceSelectorClient {
33- Client : c ,
34- namespaceSelector : namespaceSelector ,
35+ Client : c ,
36+ namespaceSelector : namespaceSelector ,
37+ includedNamespaces : sets .New (includedNamespaces ... ),
3538 }
3639}
3740
@@ -86,15 +89,21 @@ func (c *namespaceSelectorClient) filterByNamespaceLabels(ctx context.Context, l
8689 }
8790
8891 ns := obj .GetNamespace ()
89- matches , cached := namespaceMatches [ns ]
90- if ! cached {
91- var err error
92- matches , err = checkObjectNamespaceLabels (ctx , c .Client , c .namespaceSelector , obj )
93- if err != nil {
94- return fmt .Errorf ("failed to check namespace labels for object %s/%s: %w" ,
95- ns , obj .GetName (), err )
92+ // includedNamespaces are part of EG's own operating surface, e.g. the
93+ // controller namespace. They must bypass user namespace selectors.
94+ matches := c .includedNamespaces .Has (ns )
95+ if ! matches {
96+ cachedMatches , cached := namespaceMatches [ns ]
97+ matches = cachedMatches
98+ if ! cached {
99+ var err error
100+ matches , err = checkObjectNamespaceLabels (ctx , c .Client , c .namespaceSelector , obj )
101+ if err != nil {
102+ return fmt .Errorf ("failed to check namespace labels for object %s/%s: %w" ,
103+ ns , obj .GetName (), err )
104+ }
105+ namespaceMatches [ns ] = matches
96106 }
97- namespaceMatches [ns ] = matches
98107 }
99108
100109 if matches {
0 commit comments