8
8
9
9
. "github.com/onsi/ginkgo/v2"
10
10
. "github.com/onsi/gomega"
11
- "k8s.io/apimachinery/pkg/api/meta"
12
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
12
"sigs.k8s.io/controller-runtime/pkg/client"
14
13
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -21,101 +20,134 @@ import (
21
20
var _ = Describe ("Kuadrant Gateway controller" , func () {
22
21
var (
23
22
testNamespace string
24
- gwName = "toystore-gw"
23
+ gwAName = "gw-a"
24
+ gwBName = "gw-b"
25
25
)
26
26
27
27
beforeEachCallback := func () {
28
28
CreateNamespace (& testNamespace )
29
29
30
- ApplyKuadrantCR (testNamespace )
31
30
}
32
31
33
32
BeforeEach (beforeEachCallback )
34
33
AfterEach (DeleteNamespaceCallback (& testNamespace ))
35
34
36
- Context ("Gateway created after Kuadrant instance" , func () {
37
- It ("gateway should have required annotation" , func () {
38
- gateway := testBuildBasicGateway (gwName , testNamespace )
39
- err := k8sClient .Create (context .Background (), gateway )
35
+ Context ("two gateways created after Kuadrant instance" , func () {
36
+ It ("gateways should have required annotation" , func () {
37
+ ApplyKuadrantCR (testNamespace )
38
+
39
+ gwA := testBuildBasicGateway (gwAName , testNamespace )
40
+ err := k8sClient .Create (context .Background (), gwA )
41
+ Expect (err ).ToNot (HaveOccurred ())
42
+ Eventually (testGatewayIsReady (gwA ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
43
+
44
+ gwB := testBuildBasicGateway (gwBName , testNamespace )
45
+ err = k8sClient .Create (context .Background (), gwB )
46
+ Expect (err ).ToNot (HaveOccurred ())
47
+ Eventually (testGatewayIsReady (gwB ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
48
+
49
+ // Check gwA is annotated with kuadrant annotation
50
+ Eventually (testIsGatewayKuadrantManaged (gwA , testNamespace ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
51
+
52
+ // Check gwB is annotated with kuadrant annotation
53
+ Eventually (testIsGatewayKuadrantManaged (gwB , testNamespace ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
54
+ })
55
+ })
56
+
57
+ Context ("two gateways created before Kuadrant instance" , func () {
58
+ It ("gateways should have required annotation" , func () {
59
+ gwA := testBuildBasicGateway (gwAName , testNamespace )
60
+ err := k8sClient .Create (context .Background (), gwA )
61
+ Expect (err ).ToNot (HaveOccurred ())
62
+ Eventually (testGatewayIsReady (gwA ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
63
+
64
+ gwB := testBuildBasicGateway (gwBName , testNamespace )
65
+ err = k8sClient .Create (context .Background (), gwB )
66
+ Expect (err ).ToNot (HaveOccurred ())
67
+ Eventually (testGatewayIsReady (gwB ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
68
+
69
+ ApplyKuadrantCR (testNamespace )
70
+
71
+ // Check gwA is annotated with kuadrant annotation
72
+ Eventually (testIsGatewayKuadrantManaged (gwA , testNamespace ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
73
+
74
+ // Check gwB is annotated with kuadrant annotation
75
+ Eventually (testIsGatewayKuadrantManaged (gwB , testNamespace ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
76
+ })
77
+ })
78
+
79
+ Context ("when Kuadrant instance is deleted" , func () {
80
+ It ("gateways should not have kuadrant annotation" , func () {
81
+ kuadrantName := "sample"
82
+ ApplyKuadrantCRWithName (testNamespace , kuadrantName )
83
+
84
+ gwA := testBuildBasicGateway (gwAName , testNamespace )
85
+ err := k8sClient .Create (context .Background (), gwA )
86
+ Expect (err ).ToNot (HaveOccurred ())
87
+ Eventually (testGatewayIsReady (gwA ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
88
+
89
+ gwB := testBuildBasicGateway (gwBName , testNamespace )
90
+ err = k8sClient .Create (context .Background (), gwB )
40
91
Expect (err ).ToNot (HaveOccurred ())
92
+ Eventually (testGatewayIsReady (gwB ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
93
+
94
+ // Check gwA is annotated with kuadrant annotation
95
+ Eventually (testIsGatewayKuadrantManaged (gwA , testNamespace ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
96
+
97
+ // Check gwB is annotated with kuadrant annotation
98
+ Eventually (testIsGatewayKuadrantManaged (gwB , testNamespace ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
99
+
100
+ kObj := & kuadrantv1beta1.Kuadrant {ObjectMeta : metav1.ObjectMeta {Name : kuadrantName , Namespace : testNamespace }}
101
+ err = testClient ().Delete (context .Background (), kObj )
41
102
103
+ // Check gwA is not annotated with kuadrant annotation
42
104
Eventually (func () bool {
43
105
existingGateway := & gatewayapiv1.Gateway {}
44
- err := k8sClient .Get (context .Background (), client .ObjectKeyFromObject (gateway ), existingGateway )
106
+ err := k8sClient .Get (context .Background (), client .ObjectKeyFromObject (gwA ), existingGateway )
45
107
if err != nil {
46
- logf .Log .V (1 ).Info ("[WARN] Getting gateway failed" , "error" , err )
47
- return false
48
- }
49
-
50
- if meta .IsStatusConditionFalse (existingGateway .Status .Conditions , string (gatewayapiv1 .GatewayConditionProgrammed )) {
51
- logf .Log .V (1 ).Info ("[WARN] Gateway not ready" )
108
+ logf .Log .Info ("[WARN] Getting gateway failed" , "error" , err )
52
109
return false
53
110
}
54
-
55
- return true
111
+ _ , isSet := existingGateway . GetAnnotations ()[ kuadrant . KuadrantNamespaceAnnotation ]
112
+ return ! isSet
56
113
}, 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
57
114
58
- // Check gateway is annotated with kuadrant annotation
115
+ // Check gwB is not annotated with kuadrant annotation
59
116
Eventually (func () bool {
60
117
existingGateway := & gatewayapiv1.Gateway {}
61
- err := k8sClient .Get (context .Background (), client .ObjectKeyFromObject (gateway ), existingGateway )
118
+ err := k8sClient .Get (context .Background (), client .ObjectKeyFromObject (gwB ), existingGateway )
62
119
if err != nil {
63
- logf .Log .V ( 1 ). Info ("[WARN] Getting gateway failed" , "error" , err )
120
+ logf .Log .Info ("[WARN] Getting gateway failed" , "error" , err )
64
121
return false
65
122
}
66
- return kuadrant .IsKuadrantManaged (existingGateway )
123
+ _ , isSet := existingGateway .GetAnnotations ()[kuadrant .KuadrantNamespaceAnnotation ]
124
+ return ! isSet
67
125
}, 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
68
126
})
69
127
})
70
128
71
129
Context ("Two kuadrant instances" , func () {
72
- var secondNamespace string
130
+ var (
131
+ secondNamespace string
132
+ kuadrantAName string = "kuadrant-a"
133
+ kuadrantBName string = "kuadrant-b"
134
+ )
73
135
74
136
BeforeEach (func () {
75
- CreateNamespace (& secondNamespace )
76
- newKuadrantName := "second"
77
- newKuadrant := & kuadrantv1beta1.Kuadrant {
78
- TypeMeta : metav1.TypeMeta {APIVersion : "v1beta1" , Kind : "Kuadrant" },
79
- ObjectMeta : metav1.ObjectMeta {Name : newKuadrantName , Namespace : secondNamespace },
80
- }
81
- err := testClient ().Create (context .Background (), newKuadrant )
82
- Expect (err ).ToNot (HaveOccurred ())
137
+ ApplyKuadrantCRWithName (testNamespace , kuadrantAName )
83
138
84
- Eventually (func () bool {
85
- kuadrant := & kuadrantv1beta1.Kuadrant {}
86
- err := k8sClient .Get (context .Background (), client.ObjectKey {Name : newKuadrantName , Namespace : secondNamespace }, kuadrant )
87
- if err != nil {
88
- return false
89
- }
90
- if ! meta .IsStatusConditionTrue (kuadrant .Status .Conditions , "Ready" ) {
91
- return false
92
- }
93
- return true
94
- }, time .Minute , 5 * time .Second ).Should (BeTrue ())
139
+ CreateNamespace (& secondNamespace )
140
+ ApplyKuadrantCRWithName (secondNamespace , kuadrantBName )
95
141
})
96
142
97
143
AfterEach (DeleteNamespaceCallback (& secondNamespace ))
98
144
99
145
It ("new gateway should not be annotated" , func () {
100
- gateway := testBuildBasicGateway (gwName , testNamespace )
146
+ gateway := testBuildBasicGateway ("gw-a" , testNamespace )
101
147
err := k8sClient .Create (context .Background (), gateway )
102
148
Expect (err ).ToNot (HaveOccurred ())
103
149
104
- Eventually (func () bool {
105
- existingGateway := & gatewayapiv1.Gateway {}
106
- err := k8sClient .Get (context .Background (), client .ObjectKeyFromObject (gateway ), existingGateway )
107
- if err != nil {
108
- logf .Log .V (1 ).Info ("[WARN] Getting gateway failed" , "error" , err )
109
- return false
110
- }
111
-
112
- if meta .IsStatusConditionFalse (existingGateway .Status .Conditions , string (gatewayapiv1 .GatewayConditionProgrammed )) {
113
- logf .Log .V (1 ).Info ("[WARN] Gateway not ready" )
114
- return false
115
- }
116
-
117
- return true
118
- }, 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
150
+ Eventually (testGatewayIsReady (gateway ), 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
119
151
120
152
// Check gateway is not annotated with kuadrant annotation
121
153
Eventually (func () bool {
@@ -125,8 +157,22 @@ var _ = Describe("Kuadrant Gateway controller", func() {
125
157
logf .Log .V (1 ).Info ("[WARN] Getting gateway failed" , "error" , err )
126
158
return false
127
159
}
128
- return ! kuadrant .IsKuadrantManaged (existingGateway )
160
+ _ , isSet := existingGateway .GetAnnotations ()[kuadrant .KuadrantNamespaceAnnotation ]
161
+ return ! isSet
129
162
}, 15 * time .Second , 5 * time .Second ).Should (BeTrue ())
130
163
})
131
164
})
132
165
})
166
+
167
+ func testIsGatewayKuadrantManaged (gw * gatewayapiv1.Gateway , ns string ) func () bool {
168
+ return func () bool {
169
+ existingGateway := & gatewayapiv1.Gateway {}
170
+ err := k8sClient .Get (context .Background (), client .ObjectKeyFromObject (gw ), existingGateway )
171
+ if err != nil {
172
+ logf .Log .Info ("[WARN] Getting gateway failed" , "error" , err )
173
+ return false
174
+ }
175
+ val , isSet := existingGateway .GetAnnotations ()[kuadrant .KuadrantNamespaceAnnotation ]
176
+ return isSet && val == ns
177
+ }
178
+ }
0 commit comments