@@ -22,6 +22,7 @@ import (
2222"time"
2323
2424"github.com/google/go-cmp/cmp"
25+ "github.com/google/go-cmp/cmp/cmpopts"
2526fuzz "github.com/google/gofuzz"
2627corev1 "k8s.io/api/core/v1"
2728"k8s.io/apimachinery/pkg/api/meta"
@@ -38,6 +39,22 @@ func TestDefaultOpts(t *testing.T) {
3839t .Parallel ()
3940
4041pod := & corev1.Pod {}
42+
43+ compare := func (a , b any ) string {
44+ return cmp .Diff (a , b ,
45+ cmpopts .IgnoreUnexported (Options {}),
46+ cmpopts .IgnoreFields (Options {}, "HTTPClient" , "Scheme" , "Mapper" , "SyncPeriod" ),
47+ cmp .Comparer (func (a , b fields.Selector ) bool {
48+ if (a != nil ) != (b != nil ) {
49+ return false
50+ }
51+ if a == nil {
52+ return true
53+ }
54+ return a .String () == b .String ()
55+ }),
56+ )
57+ }
4158testCases := []struct {
4259name string
4360in Options
@@ -221,6 +238,120 @@ func TestDefaultOpts(t *testing.T) {
221238return cmp .Diff (expected , o .DefaultNamespaces )
222239},
223240},
241+ {
242+ name : "ByObject.Namespaces get selector from DefaultNamespaces before DefaultSelector" ,
243+ in : Options {
244+ ByObject : map [client.Object ]ByObject {
245+ pod : {Namespaces : map [string ]Config {"default" : {}}},
246+ },
247+ DefaultNamespaces : map [string ]Config {"default" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "namespace" })}},
248+ DefaultLabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "default" }),
249+ },
250+
251+ verification : func (o Options ) string {
252+ expected := Options {
253+ ByObject : map [client.Object ]ByObject {
254+ pod : {Namespaces : map [string ]Config {"default" : {
255+ LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "namespace" }),
256+ }}},
257+ },
258+ DefaultNamespaces : map [string ]Config {"default" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "namespace" })}},
259+ DefaultLabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "default" }),
260+ }
261+
262+ return compare (expected , o )
263+ },
264+ },
265+ {
266+ name : "Two namespaces in DefaultNamespaces with custom selection logic" ,
267+ in : Options {DefaultNamespaces : map [string ]Config {
268+ "kube-public" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-public" })},
269+ "kube-system" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-system" })},
270+ "" : {},
271+ }},
272+
273+ verification : func (o Options ) string {
274+ expected := Options {
275+ DefaultNamespaces : map [string ]Config {
276+ "kube-public" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-public" })},
277+ "kube-system" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-system" })},
278+ "" : {FieldSelector : fields .ParseSelectorOrDie ("metadata.namespace!=kube-public,metadata.namespace!=kube-system" )},
279+ },
280+ }
281+
282+ return compare (expected , o )
283+ },
284+ },
285+ {
286+ name : "Two namespaces in DefaultNamespaces with custom selection logic and namespace default has its own field selector" ,
287+ in : Options {DefaultNamespaces : map [string ]Config {
288+ "kube-public" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-public" })},
289+ "kube-system" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-system" })},
290+ "" : {FieldSelector : fields .ParseSelectorOrDie ("spec.nodeName=foo" )},
291+ }},
292+
293+ verification : func (o Options ) string {
294+ expected := Options {
295+ DefaultNamespaces : map [string ]Config {
296+ "kube-public" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-public" })},
297+ "kube-system" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-system" })},
298+ "" : {FieldSelector : fields .ParseSelectorOrDie (
299+ "metadata.namespace!=kube-public,metadata.namespace!=kube-system,spec.nodeName=foo" ,
300+ )},
301+ },
302+ }
303+
304+ return compare (expected , o )
305+ },
306+ },
307+ {
308+ name : "Two namespaces in ByObject.Namespaces with custom selection logic" ,
309+ in : Options {ByObject : map [client.Object ]ByObject {pod : {
310+ Namespaces : map [string ]Config {
311+ "kube-public" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-public" })},
312+ "kube-system" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-system" })},
313+ "" : {},
314+ },
315+ }}},
316+
317+ verification : func (o Options ) string {
318+ expected := Options {ByObject : map [client.Object ]ByObject {pod : {
319+ Namespaces : map [string ]Config {
320+ "kube-public" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-public" })},
321+ "kube-system" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-system" })},
322+ "" : {FieldSelector : fields .ParseSelectorOrDie (
323+ "metadata.namespace!=kube-public,metadata.namespace!=kube-system" ,
324+ )},
325+ },
326+ }}}
327+
328+ return compare (expected , o )
329+ },
330+ },
331+ {
332+ name : "Two namespaces in ByObject.Namespaces with custom selection logic and namespace default has its own field selector" ,
333+ in : Options {ByObject : map [client.Object ]ByObject {pod : {
334+ Namespaces : map [string ]Config {
335+ "kube-public" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-public" })},
336+ "kube-system" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-system" })},
337+ "" : {FieldSelector : fields .ParseSelectorOrDie ("spec.nodeName=foo" )},
338+ },
339+ }}},
340+
341+ verification : func (o Options ) string {
342+ expected := Options {ByObject : map [client.Object ]ByObject {pod : {
343+ Namespaces : map [string ]Config {
344+ "kube-public" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-public" })},
345+ "kube-system" : {LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "kube-system" })},
346+ "" : {FieldSelector : fields .ParseSelectorOrDie (
347+ "metadata.namespace!=kube-public,metadata.namespace!=kube-system,spec.nodeName=foo" ,
348+ )},
349+ },
350+ }}}
351+
352+ return compare (expected , o )
353+ },
354+ },
224355{
225356name : "DefaultNamespace label selector doesn't get defaulted when set" ,
226357in : Options {
@@ -235,6 +366,30 @@ func TestDefaultOpts(t *testing.T) {
235366return cmp .Diff (expected , o .DefaultNamespaces )
236367},
237368},
369+ {
370+ name : "Defaulted namespaces in ByObject contain ByObject's selector" ,
371+ in : Options {
372+ ByObject : map [client.Object ]ByObject {
373+ pod : {Label : labels .SelectorFromSet (map [string ]string {"from" : "pod" })},
374+ },
375+ DefaultNamespaces : map [string ]Config {"default" : {}},
376+ },
377+ verification : func (o Options ) string {
378+ expected := Options {
379+ ByObject : map [client.Object ]ByObject {
380+ pod : {
381+ Label : labels .SelectorFromSet (map [string ]string {"from" : "pod" }),
382+ Namespaces : map [string ]Config {"default" : {
383+ LabelSelector : labels .SelectorFromSet (map [string ]string {"from" : "pod" }),
384+ }},
385+ },
386+ },
387+
388+ DefaultNamespaces : map [string ]Config {"default" : {}},
389+ }
390+ return compare (expected , o )
391+ },
392+ },
238393}
239394
240395for _ , tc := range testCases {
0 commit comments