File tree 3 files changed +69
-7
lines changed
pkg/controllers/namespacesync
3 files changed +69
-7
lines changed Original file line number Diff line number Diff line change @@ -32,13 +32,6 @@ type Reconciler struct {
32
32
TargetNamespaceFilter func (ns * corev1.Namespace ) bool
33
33
}
34
34
35
- var NamespaceHasLabelKey = func (key string ) func (ns * corev1.Namespace ) bool {
36
- return func (ns * corev1.Namespace ) bool {
37
- _ , ok := ns .GetLabels ()[key ]
38
- return ok
39
- }
40
- }
41
-
42
35
func (r * Reconciler ) SetupWithManager (
43
36
ctx context.Context ,
44
37
mgr ctrl.Manager ,
Original file line number Diff line number Diff line change
1
+ package namespacesync
2
+
3
+ import corev1 "k8s.io/api/core/v1"
4
+
5
+ var NamespaceHasLabelKey = func (key string ) func (ns * corev1.Namespace ) bool {
6
+ return func (ns * corev1.Namespace ) bool {
7
+ _ , ok := ns .GetLabels ()[key ]
8
+ return ok
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ package namespacesync
2
+
3
+ import (
4
+ "testing"
5
+
6
+ corev1 "k8s.io/api/core/v1"
7
+ v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8
+ )
9
+
10
+ func TestNamespaceHasLabelKey (t * testing.T ) {
11
+ tests := []struct {
12
+ name string
13
+ key string
14
+ ns * corev1.Namespace
15
+ want bool
16
+ }{
17
+ {
18
+ name : "match a labeled namespace" ,
19
+ key : "testkey" ,
20
+ ns : & corev1.Namespace {
21
+ ObjectMeta : v1.ObjectMeta {
22
+ Name : "test" ,
23
+ Labels : map [string ]string {
24
+ "testkey" : "" ,
25
+ },
26
+ },
27
+ },
28
+ want : true ,
29
+ },
30
+ {
31
+ name : "do not match if label key is not found" ,
32
+ key : "testkey" ,
33
+ ns : & corev1.Namespace {
34
+ ObjectMeta : v1.ObjectMeta {
35
+ Name : "test" ,
36
+ },
37
+ },
38
+ want : false ,
39
+ },
40
+ {
41
+ name : "do not match if label key is empty string" ,
42
+ key : "" ,
43
+ ns : & corev1.Namespace {
44
+ ObjectMeta : v1.ObjectMeta {
45
+ Name : "test" ,
46
+ },
47
+ },
48
+ want : false ,
49
+ },
50
+ }
51
+ for _ , tt := range tests {
52
+ t .Run (tt .name , func (t * testing.T ) {
53
+ fn := NamespaceHasLabelKey (tt .key )
54
+ if got := fn (tt .ns ); got != tt .want {
55
+ t .Fatalf ("got %t, want %t" , got , tt .want )
56
+ }
57
+ })
58
+ }
59
+ }
You can’t perform that action at this time.
0 commit comments