forked from antrea-io/antrea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustergroup_test.go
341 lines (319 loc) · 11.3 KB
/
clustergroup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2021 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package e2e
import (
"fmt"
"testing"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
crdv1beta1 "antrea.io/antrea/pkg/apis/crd/v1beta1"
)
func testInvalidCGIPBlockWithPodSelector(t *testing.T) {
invalidErr := fmt.Errorf("clustergroup created with ipblock and podSelector")
cgName := "ipb-pod"
pSel := &metav1.LabelSelector{MatchLabels: map[string]string{"pod": "x"}}
cidr := "10.0.0.10/32"
ipb := []crdv1beta1.IPBlock{{CIDR: cidr}}
cg := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: cgName,
},
Spec: crdv1beta1.GroupSpec{
PodSelector: pSel,
IPBlocks: ipb,
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cg); err == nil {
// Above creation of CG must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
}
func testInvalidCGIPBlockWithNSSelector(t *testing.T) {
invalidErr := fmt.Errorf("clustergroup created with ipblock and namespaceSelector")
cgName := "ipb-ns"
nSel := &metav1.LabelSelector{MatchLabels: map[string]string{"ns": namespaces["y"]}}
cidr := "10.0.0.10/32"
ipb := []crdv1beta1.IPBlock{{CIDR: cidr}}
cg := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: cgName,
},
Spec: crdv1beta1.GroupSpec{
NamespaceSelector: nSel,
IPBlocks: ipb,
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cg); err == nil {
// Above creation of CG must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
}
func testInvalidCGServiceRefWithPodSelector(t *testing.T) {
invalidErr := fmt.Errorf("clustergroup created with serviceReference and podSelector")
cgName := "svcref-pod-selector"
pSel := &metav1.LabelSelector{MatchLabels: map[string]string{"pod": "x"}}
svcRef := &crdv1beta1.NamespacedName{
Namespace: namespaces["y"],
Name: "test-svc",
}
cg := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: cgName,
},
Spec: crdv1beta1.GroupSpec{
PodSelector: pSel,
ServiceReference: svcRef,
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cg); err == nil {
// Above creation of CG must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
}
func testInvalidCGServiceRefWithNSSelector(t *testing.T) {
invalidErr := fmt.Errorf("clustergroup created with serviceReference and namespaceSelector")
cgName := "svcref-ns-selector"
nSel := &metav1.LabelSelector{MatchLabels: map[string]string{"ns": namespaces["y"]}}
svcRef := &crdv1beta1.NamespacedName{
Namespace: namespaces["y"],
Name: "test-svc",
}
cg := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: cgName,
},
Spec: crdv1beta1.GroupSpec{
NamespaceSelector: nSel,
ServiceReference: svcRef,
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cg); err == nil {
// Above creation of CG must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
}
func testInvalidCGServiceRefWithIPBlock(t *testing.T) {
invalidErr := fmt.Errorf("clustergroup created with ipblock and namespaceSelector")
cgName := "ipb-svcref"
cidr := "10.0.0.10/32"
ipb := []crdv1beta1.IPBlock{{CIDR: cidr}}
svcRef := &crdv1beta1.NamespacedName{
Namespace: namespaces["y"],
Name: "test-svc",
}
cg := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: cgName,
},
Spec: crdv1beta1.GroupSpec{
ServiceReference: svcRef,
IPBlocks: ipb,
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cg); err == nil {
// Above creation of CG must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
}
var testChildCGName = "test-child-cg"
func createChildCGForTest(t *testing.T) {
cg := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: testChildCGName,
},
Spec: crdv1beta1.GroupSpec{
PodSelector: &metav1.LabelSelector{},
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cg); err != nil {
failOnError(err, t)
}
}
func cleanupChildCGForTest(t *testing.T) {
if err := k8sUtils.DeleteCG(testChildCGName); err != nil {
failOnError(err, t)
}
}
func testInvalidCGChildGroupWithPodSelector(t *testing.T) {
invalidErr := fmt.Errorf("clustergroup created with childGroups and podSelector")
cgName := "child-group-pod-selector"
pSel := &metav1.LabelSelector{MatchLabels: map[string]string{"pod": "x"}}
cg := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: cgName,
},
Spec: crdv1beta1.GroupSpec{
PodSelector: pSel,
ChildGroups: []crdv1beta1.ClusterGroupReference{crdv1beta1.ClusterGroupReference(testChildCGName)},
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cg); err == nil {
// Above creation of CG must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
}
func testInvalidCGChildGroupWithServiceReference(t *testing.T) {
invalidErr := fmt.Errorf("clustergroup created with childGroups and ServiceReference")
cgName := "child-group-svcref"
svcRef := &crdv1beta1.NamespacedName{
Namespace: namespaces["y"],
Name: "test-svc",
}
cg := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: cgName,
},
Spec: crdv1beta1.GroupSpec{
ServiceReference: svcRef,
ChildGroups: []crdv1beta1.ClusterGroupReference{crdv1beta1.ClusterGroupReference(testChildCGName)},
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cg); err == nil {
// Above creation of CG must fail as it is an invalid spec.
failOnError(invalidErr, t)
}
}
func testInvalidCGMaxNestedLevel(t *testing.T) {
invalidErr := fmt.Errorf("clustergroup created with childGroup which has childGroups itself")
cgName1, cgName2 := "cg-nested-1", "cg-nested-2"
cg1 := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{Name: cgName1},
Spec: crdv1beta1.GroupSpec{
ChildGroups: []crdv1beta1.ClusterGroupReference{crdv1beta1.ClusterGroupReference(testChildCGName)},
},
}
cg2 := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{Name: cgName2},
Spec: crdv1beta1.GroupSpec{
ChildGroups: []crdv1beta1.ClusterGroupReference{crdv1beta1.ClusterGroupReference(cgName1)},
},
}
// Try to create cg-nested-1 first and then cg-nested-2.
// The creation of cg-nested-2 should fail as it breaks the max nested level
if _, err := k8sUtils.CreateOrUpdateCG(cg1); err != nil {
// Above creation of CG must succeed as it is a valid spec.
failOnError(err, t)
}
if _, err := k8sUtils.CreateOrUpdateCG(cg2); err == nil {
// Above creation of CG must fail as cg-nested-2 cannot have cg-nested-1 as childGroup.
failOnError(invalidErr, t)
}
// cleanup cg-nested-1
if err := k8sUtils.DeleteCG(cgName1); err != nil {
failOnError(err, t)
}
// Try to create cg-nested-2 first and then cg-nested-1.
// The creation of cg-nested-1 should fail as it breaks the max nested level
if _, err := k8sUtils.CreateOrUpdateCG(cg2); err != nil {
// Above creation of CG must succeed as it is a valid spec.
failOnError(err, t)
}
if _, err := k8sUtils.CreateOrUpdateCG(cg1); err == nil {
// Above creation of CG must fail as cg-nested-2 cannot have cg-nested-1 as childGroup.
failOnError(invalidErr, t)
}
// cleanup cg-nested-2
if err := k8sUtils.DeleteCG(cgName2); err != nil {
failOnError(err, t)
}
}
func getRealizationStatus(cg *crdv1beta1.ClusterGroup) v1.ConditionStatus {
conds := cg.Status.Conditions
for _, cond := range conds {
if cond.Type == crdv1beta1.GroupMembersComputed && cond.Status == v1.ConditionTrue {
return v1.ConditionTrue
}
}
return v1.ConditionFalse
}
func testClusterGroupRealizationStatus(t *testing.T) {
invalidErr1 := fmt.Errorf("clustergroup with child groups should only be considered realized when all its child groups are realized")
invalidErr2 := fmt.Errorf("clustergroup with selectors or serviceRef should be realized once processed")
childCG1Returned, _ := k8sUtils.GetCG(testChildCGName)
// test-child-cg should be considered realized as soon as its synced.
if getRealizationStatus(childCG1Returned) != v1.ConditionTrue {
failOnError(invalidErr2, t)
}
cgParent := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{Name: "parent-cg"},
Spec: crdv1beta1.GroupSpec{
ChildGroups: []crdv1beta1.ClusterGroupReference{
crdv1beta1.ClusterGroupReference(testChildCGName),
crdv1beta1.ClusterGroupReference("child-cg-2"),
},
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cgParent); err != nil {
// Above creation of CG must succeed as it is a valid spec.
failOnError(err, t)
}
time.Sleep(networkPolicyDelay / 2)
cgParentReturned, _ := k8sUtils.GetCG("parent-cg")
// cgParent should not be considered realized yet since child-cg-2 is not yet created.
if getRealizationStatus(cgParentReturned) != v1.ConditionFalse {
failOnError(invalidErr1, t)
}
cgChild2 := &crdv1beta1.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "child-cg-2",
},
Spec: crdv1beta1.GroupSpec{
IPBlocks: []crdv1beta1.IPBlock{
{CIDR: "192.168.2.0/24"},
},
},
}
if _, err := k8sUtils.CreateOrUpdateCG(cgChild2); err != nil {
// Above creation of CG must succeed as it is a valid spec.
failOnError(err, t)
}
time.Sleep(networkPolicyDelay / 2)
childCG2Returned, _ := k8sUtils.GetCG("child-cg-2")
// child-cg-2 should be considered realized as soon as its synced.
if getRealizationStatus(childCG2Returned) != v1.ConditionTrue {
failOnError(invalidErr2, t)
}
cgParentReturned, _ = k8sUtils.GetCG("parent-cg")
// cgParent should now be considered realized.
if getRealizationStatus(cgParentReturned) != v1.ConditionTrue {
failOnError(invalidErr1, t)
}
}
func TestClusterGroup(t *testing.T) {
skipIfHasWindowsNodes(t)
skipIfAntreaPolicyDisabled(t)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
initialize(t, data)
t.Run("TestGroupClusterGroupValidate", func(t *testing.T) {
t.Run("Case=IPBlockWithPodSelectorDenied", func(t *testing.T) { testInvalidCGIPBlockWithPodSelector(t) })
t.Run("Case=IPBlockWithNamespaceSelectorDenied", func(t *testing.T) { testInvalidCGIPBlockWithNSSelector(t) })
t.Run("Case=ServiceRefWithPodSelectorDenied", func(t *testing.T) { testInvalidCGServiceRefWithPodSelector(t) })
t.Run("Case=ServiceRefWithNamespaceSelectorDenied", func(t *testing.T) { testInvalidCGServiceRefWithNSSelector(t) })
t.Run("Case=ServiceRefWithIPBlockDenied", func(t *testing.T) { testInvalidCGServiceRefWithIPBlock(t) })
})
t.Run("TestGroupClusterGroupValidateChildGroup", func(t *testing.T) {
createChildCGForTest(t)
t.Run("Case=ChildGroupWithPodSelectorDenied", func(t *testing.T) { testInvalidCGChildGroupWithPodSelector(t) })
t.Run("Case=ChildGroupWithPodServiceReferenceDenied", func(t *testing.T) { testInvalidCGChildGroupWithServiceReference(t) })
t.Run("Case=ChildGroupExceedMaxNestedLevel", func(t *testing.T) { testInvalidCGMaxNestedLevel(t) })
t.Run("Case=ClusterGroupRealizationStatusWithChildGroups", func(t *testing.T) { testClusterGroupRealizationStatus(t) })
cleanupChildCGForTest(t)
})
k8sUtils.Cleanup(namespaces) // clean up all cluster-scope resources, including CGs
}