@@ -105,8 +105,12 @@ func sortGatheredResources(list []*api.GatheredResource) {
105
105
func TestNewDataGathererWithClientAndDynamicInformer (t * testing.T ) {
106
106
ctx := context .Background ()
107
107
config := ConfigDynamic {
108
- IncludeNamespaces : []string {"a " },
108
+ ExcludeNamespaces : []string {"kube-system " },
109
109
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
+ },
110
114
}
111
115
cl := fake .NewSimpleDynamicClient (runtime .NewScheme ())
112
116
dg , err := config .newDataGathererWithClient (ctx , cl , nil )
@@ -121,7 +125,8 @@ func TestNewDataGathererWithClientAndDynamicInformer(t *testing.T) {
121
125
groupVersionResource : config .GroupVersionResource ,
122
126
// it's important that the namespaces are set as the IncludeNamespaces
123
127
// 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" ,
125
130
}
126
131
127
132
gatherer := dg .(* DataGathererDynamic )
@@ -150,6 +155,9 @@ func TestNewDataGathererWithClientAndDynamicInformer(t *testing.T) {
150
155
if gatherer .nativeSharedInformer != nil {
151
156
t .Errorf ("unexpected nativeSharedInformer value: %v. should be nil" , gatherer .nativeSharedInformer )
152
157
}
158
+ if ! reflect .DeepEqual (gatherer .fieldSelector , expected .fieldSelector ) {
159
+ t .Errorf ("expected %v, got %v" , expected .fieldSelector , gatherer .fieldSelector )
160
+ }
153
161
}
154
162
155
163
func TestNewDataGathererWithClientAndSharedIndexInformer (t * testing.T ) {
@@ -216,6 +224,8 @@ exclude-namespaces:
216
224
# from the config file
217
225
include-namespaces:
218
226
- default
227
+ field-selectors:
228
+ - type!=kubernetes.io/service-account-token
219
229
`
220
230
221
231
expectedGVR := schema.GroupVersionResource {
@@ -231,6 +241,10 @@ include-namespaces:
231
241
232
242
expectedIncludeNamespaces := []string {"default" }
233
243
244
+ expectedFieldSelectors := []string {
245
+ "type!=kubernetes.io/service-account-token" ,
246
+ }
247
+
234
248
cfg := ConfigDynamic {}
235
249
err := yaml .Unmarshal ([]byte (textCfg ), & cfg )
236
250
if err != nil {
@@ -251,6 +265,9 @@ include-namespaces:
251
265
if got , want := cfg .IncludeNamespaces , expectedIncludeNamespaces ; ! reflect .DeepEqual (got , want ) {
252
266
t .Errorf ("IncludeNamespaces does not match: got=%+v want=%+v" , got , want )
253
267
}
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
+ }
254
271
}
255
272
256
273
func TestConfigDynamicValidate (t * testing.T ) {
@@ -275,17 +292,42 @@ func TestConfigDynamicValidate(t *testing.T) {
275
292
},
276
293
ExpectedError : "cannot set excluded and included namespaces" ,
277
294
},
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
+ },
278
317
}
279
318
280
319
for _ , test := range tests {
281
320
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 ) {
283
325
t .Errorf ("expected %s, got %s" , test .ExpectedError , err .Error ())
284
326
}
285
327
}
286
328
}
287
329
288
- func TestGenerateFieldSelector (t * testing.T ) {
330
+ func TestGenerateExcludedNamespacesFieldSelector (t * testing.T ) {
289
331
tests := []struct {
290
332
ExcludeNamespaces []string
291
333
ExpectedFieldSelector string
@@ -300,19 +342,19 @@ func TestGenerateFieldSelector(t *testing.T) {
300
342
ExcludeNamespaces : []string {
301
343
"kube-system" ,
302
344
},
303
- ExpectedFieldSelector : "metadata.namespace!=kube-system, " ,
345
+ ExpectedFieldSelector : "metadata.namespace!=kube-system" ,
304
346
},
305
347
{
306
348
ExcludeNamespaces : []string {
307
349
"kube-system" ,
308
350
"my-namespace" ,
309
351
},
310
- ExpectedFieldSelector : "metadata.namespace!=my-namespace ,metadata.namespace!=kube-system, " ,
352
+ ExpectedFieldSelector : "metadata.namespace!=kube-system ,metadata.namespace!=my-namespace " ,
311
353
},
312
354
}
313
355
314
356
for _ , test := range tests {
315
- fieldSelector := generateFieldSelector (test .ExcludeNamespaces ).String ()
357
+ fieldSelector := generateExcludedNamespacesFieldSelector (test .ExcludeNamespaces ).String ()
316
358
if fieldSelector != test .ExpectedFieldSelector {
317
359
t .Errorf ("ExpectedFieldSelector does not match: got=%+v want=%+v" , fieldSelector , test .ExpectedFieldSelector )
318
360
}
0 commit comments