@@ -105,8 +105,12 @@ func sortGatheredResources(list []*api.GatheredResource) {
105105func TestNewDataGathererWithClientAndDynamicInformer (t * testing.T ) {
106106 ctx := context .Background ()
107107 config := ConfigDynamic {
108- IncludeNamespaces : []string {"a " },
108+ ExcludeNamespaces : []string {"kube-system " },
109109 GroupVersionResource : schema.GroupVersionResource {Group : "foobar" , Version : "v1" , Resource : "foos" },
110+ FieldSelectors : []string {
111+ "type!=kubernetes.io/service-account-token" ,
112+ "type!=kubernetes.io/dockercfg" ,
113+ },
110114 }
111115 cl := fake .NewSimpleDynamicClient (runtime .NewScheme ())
112116 dg , err := config .newDataGathererWithClient (ctx , cl , nil )
@@ -121,7 +125,8 @@ func TestNewDataGathererWithClientAndDynamicInformer(t *testing.T) {
121125 groupVersionResource : config .GroupVersionResource ,
122126 // it's important that the namespaces are set as the IncludeNamespaces
123127 // during initialization
124- namespaces : config .IncludeNamespaces ,
128+ namespaces : config .IncludeNamespaces ,
129+ fieldSelector : "metadata.namespace!=kube-system,type!=kubernetes.io/service-account-token,type!=kubernetes.io/dockercfg" ,
125130 }
126131
127132 gatherer := dg .(* DataGathererDynamic )
@@ -150,6 +155,9 @@ func TestNewDataGathererWithClientAndDynamicInformer(t *testing.T) {
150155 if gatherer .nativeSharedInformer != nil {
151156 t .Errorf ("unexpected nativeSharedInformer value: %v. should be nil" , gatherer .nativeSharedInformer )
152157 }
158+ if ! reflect .DeepEqual (gatherer .fieldSelector , expected .fieldSelector ) {
159+ t .Errorf ("expected %v, got %v" , expected .fieldSelector , gatherer .fieldSelector )
160+ }
153161}
154162
155163func TestNewDataGathererWithClientAndSharedIndexInformer (t * testing.T ) {
@@ -216,6 +224,8 @@ exclude-namespaces:
216224# from the config file
217225include-namespaces:
218226- default
227+ field-selectors:
228+ - type!=kubernetes.io/service-account-token
219229`
220230
221231 expectedGVR := schema.GroupVersionResource {
@@ -231,6 +241,10 @@ include-namespaces:
231241
232242 expectedIncludeNamespaces := []string {"default" }
233243
244+ expectedFieldSelectors := []string {
245+ "type!=kubernetes.io/service-account-token" ,
246+ }
247+
234248 cfg := ConfigDynamic {}
235249 err := yaml .Unmarshal ([]byte (textCfg ), & cfg )
236250 if err != nil {
@@ -251,6 +265,9 @@ include-namespaces:
251265 if got , want := cfg .IncludeNamespaces , expectedIncludeNamespaces ; ! reflect .DeepEqual (got , want ) {
252266 t .Errorf ("IncludeNamespaces does not match: got=%+v want=%+v" , got , want )
253267 }
268+ if got , want := cfg .FieldSelectors , expectedFieldSelectors ; ! reflect .DeepEqual (got , want ) {
269+ t .Errorf ("FieldSelectors does not match: got=%+v want=%+v" , got , want )
270+ }
254271}
255272
256273func TestConfigDynamicValidate (t * testing.T ) {
@@ -275,17 +292,42 @@ func TestConfigDynamicValidate(t *testing.T) {
275292 },
276293 ExpectedError : "cannot set excluded and included namespaces" ,
277294 },
295+ {
296+ Config : ConfigDynamic {
297+ GroupVersionResource : schema.GroupVersionResource {
298+ Group : "" ,
299+ Version : "v1" ,
300+ Resource : "secrets" ,
301+ },
302+ FieldSelectors : []string {"" },
303+ },
304+ ExpectedError : "invalid field selector 0: must not be empty" ,
305+ },
306+ {
307+ Config : ConfigDynamic {
308+ GroupVersionResource : schema.GroupVersionResource {
309+ Group : "" ,
310+ Version : "v1" ,
311+ Resource : "secrets" ,
312+ },
313+ FieldSelectors : []string {"foo" },
314+ },
315+ ExpectedError : "invalid field selector 0: invalid selector: 'foo'; can't understand 'foo'" ,
316+ },
278317 }
279318
280319 for _ , test := range tests {
281320 err := test .Config .validate ()
282- if ! strings .Contains (err .Error (), test .ExpectedError ) {
321+ if err == nil && test .ExpectedError != "" {
322+ t .Errorf ("expected error: %q, got: nil" , test .ExpectedError )
323+ }
324+ if err != nil && ! strings .Contains (err .Error (), test .ExpectedError ) {
283325 t .Errorf ("expected %s, got %s" , test .ExpectedError , err .Error ())
284326 }
285327 }
286328}
287329
288- func TestGenerateFieldSelector (t * testing.T ) {
330+ func TestGenerateExcludedNamespacesFieldSelector (t * testing.T ) {
289331 tests := []struct {
290332 ExcludeNamespaces []string
291333 ExpectedFieldSelector string
@@ -300,19 +342,19 @@ func TestGenerateFieldSelector(t *testing.T) {
300342 ExcludeNamespaces : []string {
301343 "kube-system" ,
302344 },
303- ExpectedFieldSelector : "metadata.namespace!=kube-system, " ,
345+ ExpectedFieldSelector : "metadata.namespace!=kube-system" ,
304346 },
305347 {
306348 ExcludeNamespaces : []string {
307349 "kube-system" ,
308350 "my-namespace" ,
309351 },
310- ExpectedFieldSelector : "metadata.namespace!=my-namespace ,metadata.namespace!=kube-system, " ,
352+ ExpectedFieldSelector : "metadata.namespace!=kube-system ,metadata.namespace!=my-namespace " ,
311353 },
312354 }
313355
314356 for _ , test := range tests {
315- fieldSelector := generateFieldSelector (test .ExcludeNamespaces ).String ()
357+ fieldSelector := generateExcludedNamespacesFieldSelector (test .ExcludeNamespaces ).String ()
316358 if fieldSelector != test .ExpectedFieldSelector {
317359 t .Errorf ("ExpectedFieldSelector does not match: got=%+v want=%+v" , fieldSelector , test .ExpectedFieldSelector )
318360 }
0 commit comments