-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathe2e_test.go
More file actions
473 lines (383 loc) · 22.1 KB
/
e2e_test.go
File metadata and controls
473 lines (383 loc) · 22.1 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package cnp_domain
import (
"context"
"flag"
"fmt"
"testing"
"time"
netpolv1alpha2 "sigs.k8s.io/network-policy-api/apis/v1alpha2"
"github.com/onsi/ginkgo/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"k8s.io/kubernetes/test/e2e"
k8sframework "k8s.io/kubernetes/test/e2e/framework"
"k8s.io/kubernetes/test/e2e/framework/config"
"github.com/kubeovn/kube-ovn/test/e2e/framework"
)
var _ = framework.SerialDescribe("[group:cluster-network-policy]", func() {
f := framework.NewDefaultFramework("cluster-network-policy")
var cs clientset.Interface
var cnpClient *framework.CnpClient
var cnpName string
var cnpName2 string
var namespaceName string
var podName string
var podClient *framework.PodClient
var nsClient *framework.NamespaceClient
ginkgo.BeforeEach(func() {
cs = f.ClientSet
nsClient = f.NamespaceClient()
cnpClient = f.CnpClient()
cnpName = "cnp-" + framework.RandomSuffix()
namespaceName = "ns-" + framework.RandomSuffix()
podName = "pod-" + framework.RandomSuffix()
podClient = f.PodClientNS(namespaceName)
})
ginkgo.AfterEach(func() {
ginkgo.By("Cleaning up test resources")
ctx := context.Background()
if cnpName != "" {
ginkgo.By("Deleting ClusterNetworkPolicy " + cnpName)
_ = cnpClient.Delete(context.TODO(), cnpName, metav1.DeleteOptions{})
}
if cnpName2 != "" {
ginkgo.By("Deleting second ClusterNetworkPolicy " + cnpName2)
_ = cnpClient.Delete(context.TODO(), cnpName2, metav1.DeleteOptions{})
}
if podName != "" {
ginkgo.By("Deleting test pod " + podName)
err := cs.CoreV1().Pods(namespaceName).Delete(ctx, podName, metav1.DeleteOptions{})
if err != nil {
framework.Logf("Failed to delete pod %s: %v", podName, err)
}
}
if namespaceName != "" {
ginkgo.By("Deleting namespace " + namespaceName)
nsClient.Delete(namespaceName)
}
})
testNetworkConnectivityWithRetry := func(target string, shouldSucceed bool, description string, maxRetries int, retryInterval time.Duration) {
ginkgo.By(description)
ctx := context.Background()
cmd := fmt.Sprintf("curl -s --connect-timeout 5 --max-time 5 %s", target)
for i := range maxRetries {
stdout, stderr, err := framework.ExecShellInPod(ctx, f, namespaceName, podName, cmd)
success := (err == nil && len(stdout) > 0)
if shouldSucceed {
if success {
framework.Logf("Attempt %d: Successfully connected to %s", i+1, target)
return // Success, no need to retry
}
framework.Logf("Attempt %d: Failed to connect to %s - stdout: %s, stderr: %s, err: %v", i+1, target, stdout, stderr, err)
} else {
if !success {
framework.Logf("Attempt %d: Successfully blocked access to %s", i+1, target)
return // Blocked as expected, no need to retry
}
framework.Logf("Attempt %d: Unexpectedly connected to %s - stdout: %s", i+1, target, stdout)
}
// Wait between attempts if not the last attempt
if i < maxRetries-1 {
time.Sleep(retryInterval)
}
}
// If we reach here, the expected result was not achieved
if shouldSucceed {
ginkgo.Fail(fmt.Sprintf("Failed to connect to %s after %d attempts", target, maxRetries))
} else {
ginkgo.Fail(fmt.Sprintf("Unexpectedly connected to %s after %d attempts", target, maxRetries))
}
}
testNetworkConnectivity := func(target string, shouldSucceed bool, description string) {
testNetworkConnectivityWithRetry(target, shouldSucceed, description, 30, 3*time.Second)
}
// waitForDNSResolversReady waits for all DNSNameResolver CRs associated with a CNP
// to be created and have at least one resolved address. This ensures the OVN ACL
// address sets are populated before connectivity checks.
waitForDNSResolversReady := func(name string, expectedCount int) {
ginkgo.By(fmt.Sprintf("Waiting for %d DNSNameResolver(s) to be ready for CNP %s", expectedCount, name))
dnsClient := f.DNSNameResolverClient()
labelSelector := fmt.Sprintf("anp=%s", name)
err := wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, 60*time.Second, true, func(_ context.Context) (bool, error) {
resolverList := dnsClient.ListByLabel(labelSelector)
if len(resolverList.Items) < expectedCount {
return false, nil
}
for _, resolver := range resolverList.Items {
if len(resolver.Status.ResolvedNames) == 0 {
return false, nil
}
}
return true, nil
})
framework.ExpectNoError(err, "DNSNameResolvers for CNP %s failed to be ready within timeout", name)
}
framework.ConformanceIt("should create CNP with domainName deny rule and verify connectivity behavior", func() {
f.SkipVersionPriorTo(1, 15, "ClusterNetworkPolicy domainName support was introduced in v1.15")
ginkgo.By("Creating test namespace " + namespaceName)
labels := map[string]string{
"kubernetes.io/metadata.name": namespaceName,
}
ns := framework.MakeNamespace(namespaceName, labels, nil)
_ = nsClient.Create(ns)
ginkgo.By("Creating test pod " + podName + " in namespace " + namespaceName)
cmd := []string{"sleep", "infinity"}
pod := framework.MakePrivilegedPod(namespaceName, podName, nil, nil, f.KubeOVNImage, cmd, nil)
_ = podClient.CreateSync(pod)
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com before applying CNP (should succeed)")
ginkgo.By("Creating ClusterNetworkPolicy with domainName to deny baidu.com")
namespaceSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": namespaceName,
},
}
ports := []netpolv1alpha2.ClusterNetworkPolicyPort{
framework.MakeClusterNetworkPolicyPort(443, corev1.ProtocolTCP),
}
domainNames := []netpolv1alpha2.DomainName{"*.baidu.com."}
egressRule := framework.MakeClusterNetworkPolicyEgressRule("deny-baidu", netpolv1alpha2.ClusterNetworkPolicyRuleActionDeny, ports, domainNames)
cnp := framework.MakeClusterNetworkPolicy(cnpName, 55, namespaceSelector, []netpolv1alpha2.ClusterNetworkPolicyEgressRule{egressRule}, nil)
ginkgo.By("Creating ClusterNetworkPolicy in the cluster")
createdCNP, _ := cnpClient.Create(context.TODO(), cnp, metav1.CreateOptions{})
framework.Logf("Successfully created ClusterNetworkPolicy: %s", createdCNP.Name)
ginkgo.By("Verifying CNP structure is correct")
framework.ExpectEqual(len(cnp.Spec.Egress), 1)
cnpEgressRule := cnp.Spec.Egress[0]
framework.ExpectEqual(len(cnpEgressRule.To), 1)
peer := cnpEgressRule.To[0]
framework.ExpectEqual(len(peer.DomainNames), 1)
framework.ExpectEqual(string(peer.DomainNames[0]), "*.baidu.com.")
framework.ExpectEqual(cnp.Spec.Priority, int32(55))
framework.ExpectEqual(cnp.Spec.Subject.Namespaces.MatchLabels["kubernetes.io/metadata.name"], namespaceName)
waitForDNSResolversReady(cnpName, 1)
testNetworkConnectivity("https://www.baidu.com", false, "Testing connectivity to baidu.com after applying CNP (should be blocked)")
ginkgo.By("Deleting ClusterNetworkPolicy " + cnpName)
_ = cnpClient.Delete(context.TODO(), cnpName, metav1.DeleteOptions{})
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com after deleting CNP (should succeed again)")
})
framework.ConformanceIt("should create multiple CNPs with domainName rules and verify they work together", func() {
f.SkipVersionPriorTo(1, 15, "ClusterNetworkPolicy domainName support was introduced in v1.15")
cnpName2 = "cnp2-" + framework.RandomSuffix()
ginkgo.By("Creating test namespace " + namespaceName)
labels := map[string]string{
"kubernetes.io/metadata.name": namespaceName,
}
ns := framework.MakeNamespace(namespaceName, labels, nil)
_ = nsClient.Create(ns)
ginkgo.By("Creating test pod " + podName + " in namespace " + namespaceName)
cmd := []string{"sleep", "infinity"}
pod := framework.MakePrivilegedPod(namespaceName, podName, nil, nil, f.KubeOVNImage, cmd, nil)
_ = podClient.CreateSync(pod)
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com before applying cnps (should succeed)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com before applying cnps (should succeed)")
ginkgo.By("Creating first ClusterNetworkPolicy with domainName to deny baidu.com")
namespaceSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": namespaceName,
},
}
ports := []netpolv1alpha2.ClusterNetworkPolicyPort{
framework.MakeClusterNetworkPolicyPort(443, corev1.ProtocolTCP),
}
domainNames1 := []netpolv1alpha2.DomainName{"*.baidu.com."}
egressRule1 := framework.MakeClusterNetworkPolicyEgressRule("deny-baidu", netpolv1alpha2.ClusterNetworkPolicyRuleActionDeny, ports, domainNames1)
cnp1 := framework.MakeClusterNetworkPolicy(cnpName, 44, namespaceSelector, []netpolv1alpha2.ClusterNetworkPolicyEgressRule{egressRule1}, nil)
ginkgo.By("Creating first ClusterNetworkPolicy in the cluster")
createdcnp1, _ := cnpClient.Create(context.TODO(), cnp1, metav1.CreateOptions{})
framework.Logf("Successfully created first ClusterNetworkPolicy: %s", createdcnp1.Name)
ginkgo.By("Creating second ClusterNetworkPolicy with domainName to allow google.com")
domainNames2 := []netpolv1alpha2.DomainName{"*.google.com."}
egressRule2 := framework.MakeClusterNetworkPolicyEgressRule("allow-google", netpolv1alpha2.ClusterNetworkPolicyRuleActionAccept, ports, domainNames2)
cnp2 := framework.MakeClusterNetworkPolicy(cnpName2, 45, namespaceSelector, []netpolv1alpha2.ClusterNetworkPolicyEgressRule{egressRule2}, nil)
ginkgo.By("Creating second ClusterNetworkPolicy in the cluster")
createdCNP2, _ := cnpClient.Create(context.TODO(), cnp2, metav1.CreateOptions{})
framework.Logf("Successfully created second ClusterNetworkPolicy: %s", createdCNP2.Name)
ginkgo.By("Verifying both cnps structure is correct")
framework.ExpectEqual(len(cnp1.Spec.Egress), 1)
cnp1EgressRule := cnp1.Spec.Egress[0]
framework.ExpectEqual(len(cnp1EgressRule.To), 1)
peer1 := cnp1EgressRule.To[0]
framework.ExpectEqual(len(peer1.DomainNames), 1)
framework.ExpectEqual(string(peer1.DomainNames[0]), "*.baidu.com.")
framework.ExpectEqual(cnp1.Spec.Priority, int32(44))
framework.ExpectEqual(len(cnp2.Spec.Egress), 1)
cnp2EgressRule := cnp2.Spec.Egress[0]
framework.ExpectEqual(len(cnp2EgressRule.To), 1)
peer2 := cnp2EgressRule.To[0]
framework.ExpectEqual(len(peer2.DomainNames), 1)
framework.ExpectEqual(string(peer2.DomainNames[0]), "*.google.com.")
framework.ExpectEqual(cnp2.Spec.Priority, int32(45))
waitForDNSResolversReady(cnpName, 1)
waitForDNSResolversReady(cnpName2, 1)
testNetworkConnectivity("https://www.baidu.com", false, "Testing connectivity to baidu.com after applying both CNPs (should be blocked)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com after applying both CNPs (should be allowed)")
ginkgo.By("Deleting first ClusterNetworkPolicy " + cnpName)
_ = cnpClient.Delete(context.TODO(), cnpName, metav1.DeleteOptions{})
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com after deleting first CNP (should be allowed)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com after deleting first CNP (should still be allowed)")
ginkgo.By("Deleting second ClusterNetworkPolicy " + cnpName2)
_ = cnpClient.Delete(context.TODO(), cnpName2, metav1.DeleteOptions{})
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com after deleting both CNPs (should succeed)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com after deleting both CNPs (should succeed)")
})
framework.ConformanceIt("should dynamically add and remove domainName deny rules in a single CNP", func() {
f.SkipVersionPriorTo(1, 15, "ClusterNetworkPolicy domainName support was introduced in v1.15")
ginkgo.By("Creating test namespace " + namespaceName)
labels := map[string]string{
"kubernetes.io/metadata.name": namespaceName,
}
ns := framework.MakeNamespace(namespaceName, labels, nil)
_ = nsClient.Create(ns)
ginkgo.By("Creating test pod " + podName + " in namespace " + namespaceName)
cmd := []string{"sleep", "infinity"}
pod := framework.MakePrivilegedPod(namespaceName, podName, nil, nil, f.KubeOVNImage, cmd, nil)
_ = podClient.CreateSync(pod)
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com before applying cnp (should succeed)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com before applying cnp (should succeed)")
ginkgo.By("Creating ClusterNetworkPolicy without any domainName rules initially")
namespaceSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": namespaceName,
},
}
ports := []netpolv1alpha2.ClusterNetworkPolicyPort{
framework.MakeClusterNetworkPolicyPort(443, corev1.ProtocolTCP),
}
cnp := framework.MakeClusterNetworkPolicy(cnpName, 50, namespaceSelector, nil, nil)
createdCNP, _ := cnpClient.Create(context.TODO(), cnp, metav1.CreateOptions{})
framework.Logf("Successfully created ClusterNetworkPolicy: %s", createdCNP.Name)
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com after creating cnp without rules (should succeed)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com after creating cnp without rules (should succeed)")
ginkgo.By("Adding domainName deny rule for baidu.com to the existing CNP")
domainNames := []netpolv1alpha2.DomainName{"*.baidu.com."}
egressRule := framework.MakeClusterNetworkPolicyEgressRule("deny-baidu", netpolv1alpha2.ClusterNetworkPolicyRuleActionDeny, ports, domainNames)
createdCNP.Spec.Egress = []netpolv1alpha2.ClusterNetworkPolicyEgressRule{egressRule}
updatedCNP, _ := cnpClient.Update(context.TODO(), createdCNP, metav1.UpdateOptions{})
framework.Logf("Successfully updated ClusterNetworkPolicy with baidu.com deny rule: %s", updatedCNP.Name)
waitForDNSResolversReady(cnpName, 1)
testNetworkConnectivity("https://www.baidu.com", false, "Testing connectivity to baidu.com after adding deny rule (should be blocked)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com after adding baidu.com deny rule (should still succeed)")
ginkgo.By("Adding domainName deny rule for google.com to the existing CNP")
domainNames2 := []netpolv1alpha2.DomainName{"*.google.com."}
egressRule2 := framework.MakeClusterNetworkPolicyEgressRule("deny-google", netpolv1alpha2.ClusterNetworkPolicyRuleActionDeny, ports, domainNames2)
updatedCNP.Spec.Egress = append(updatedCNP.Spec.Egress, egressRule2)
updatedcnp2, _ := cnpClient.Update(context.TODO(), updatedCNP, metav1.UpdateOptions{})
framework.Logf("Successfully updated ClusterNetworkPolicy with both deny rules: %s", updatedcnp2.Name)
waitForDNSResolversReady(cnpName, 2)
testNetworkConnectivity("https://www.baidu.com", false, "Testing connectivity to baidu.com after adding both deny rules (should be blocked)")
testNetworkConnectivity("https://www.google.com", false, "Testing connectivity to google.com after adding both deny rules (should be blocked)")
ginkgo.By("Removing baidu.com deny rule from the cnp")
updatedcnp2.Spec.Egress = []netpolv1alpha2.ClusterNetworkPolicyEgressRule{egressRule2}
updatedcnp3, _ := cnpClient.Update(context.TODO(), updatedcnp2, metav1.UpdateOptions{})
framework.Logf("Successfully updated ClusterNetworkPolicy removing baidu.com deny rule: %s", updatedcnp3.Name)
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com after removing deny rule (should succeed)")
testNetworkConnectivity("https://www.google.com", false, "Testing connectivity to google.com after removing baidu.com deny rule (should still be blocked)")
ginkgo.By("Removing all domainName deny rules from the cnp")
updatedcnp3.Spec.Egress = nil
updatedcnp4, _ := cnpClient.Update(context.TODO(), updatedcnp3, metav1.UpdateOptions{})
framework.Logf("Successfully updated ClusterNetworkPolicy removing all deny rules: %s", updatedcnp4.Name)
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com after removing all deny rules (should succeed)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com after removing all deny rules (should succeed)")
})
framework.ConformanceIt("should create cnp with domainName and CIDR rules and verify they work together", func() {
f.SkipVersionPriorTo(1, 15, "ClusterNetworkPolicy domainName support was introduced in v1.15")
ginkgo.By("Creating test namespace " + namespaceName)
labels := map[string]string{
"kubernetes.io/metadata.name": namespaceName,
}
ns := framework.MakeNamespace(namespaceName, labels, nil)
_ = nsClient.Create(ns)
ginkgo.By("Creating test pod " + podName + " in namespace " + namespaceName)
cmd := []string{"sleep", "infinity"}
pod := framework.MakePrivilegedPod(namespaceName, podName, nil, nil, f.KubeOVNImage, cmd, nil)
_ = podClient.CreateSync(pod)
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to baidu.com before applying cnp (should succeed)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com before applying cnp (should succeed)")
testNetworkConnectivity("https://8.8.8.8", true, "Testing connectivity to 8.8.8.8 before applying cnp (should succeed)")
ginkgo.By("Creating ClusterNetworkPolicy with both domainName and CIDR rules")
namespaceSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": namespaceName,
},
}
ports := []netpolv1alpha2.ClusterNetworkPolicyPort{
framework.MakeClusterNetworkPolicyPort(443, corev1.ProtocolTCP),
}
domainNames := []netpolv1alpha2.DomainName{"*.baidu.com."}
egressRule1 := framework.MakeClusterNetworkPolicyEgressRule("deny-baidu", netpolv1alpha2.ClusterNetworkPolicyRuleActionDeny, ports, domainNames)
egressRule2 := netpolv1alpha2.ClusterNetworkPolicyEgressRule{
Name: "deny-google-dns",
Action: netpolv1alpha2.ClusterNetworkPolicyRuleActionDeny,
To: []netpolv1alpha2.ClusterNetworkPolicyEgressPeer{
{
Networks: []netpolv1alpha2.CIDR{"8.8.8.8/32"},
},
},
Ports: &ports,
}
cnp := framework.MakeClusterNetworkPolicy(cnpName, 80, namespaceSelector,
[]netpolv1alpha2.ClusterNetworkPolicyEgressRule{egressRule1, egressRule2}, nil)
ginkgo.By("Creating ClusterNetworkPolicy in the cluster")
createdcnp, _ := cnpClient.Create(context.TODO(), cnp, metav1.CreateOptions{})
framework.Logf("Successfully created ClusterNetworkPolicy: %s", createdcnp.Name)
ginkgo.By("Verifying cnp structure is correct")
framework.ExpectEqual(len(cnp.Spec.Egress), 2)
framework.ExpectEqual(cnp.Spec.Priority, int32(80))
waitForDNSResolversReady(cnpName, 1)
testNetworkConnectivity("https://www.baidu.com", false, "Testing connectivity to baidu.com after applying cnp (should be blocked)")
testNetworkConnectivity("https://www.google.com", true, "Testing connectivity to google.com after applying cnp (should be allowed)")
testNetworkConnectivity("https://8.8.8.8", false, "Testing connectivity to 8.8.8.8 after applying cnp (should be blocked by CIDR rule)")
})
framework.ConformanceIt("should create cnp with wildcard domainName rules and verify they work correctly", func() {
f.SkipVersionPriorTo(1, 15, "ClusterNetworkPolicy domainName support was introduced in v1.15")
ginkgo.By("Creating test namespace " + namespaceName)
labels := map[string]string{
"kubernetes.io/metadata.name": namespaceName,
}
ns := framework.MakeNamespace(namespaceName, labels, nil)
_ = nsClient.Create(ns)
ginkgo.By("Creating test pod " + podName + " in namespace " + namespaceName)
cmd := []string{"sleep", "infinity"}
pod := framework.MakePrivilegedPod(namespaceName, podName, nil, nil, f.KubeOVNImage, cmd, nil)
_ = podClient.CreateSync(pod)
testNetworkConnectivity("https://www.baidu.com", true, "Testing connectivity to www.baidu.com before applying cnp (should succeed)")
testNetworkConnectivity("https://api.baidu.com", true, "Testing connectivity to api.baidu.com before applying cnp (should succeed)")
testNetworkConnectivity("https://news.baidu.com", true, "Testing connectivity to news.baidu.com before applying cnp (should succeed)")
ginkgo.By("Creating ClusterNetworkPolicy with wildcard domainName rules")
namespaceSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": namespaceName,
},
}
ports := []netpolv1alpha2.ClusterNetworkPolicyPort{
framework.MakeClusterNetworkPolicyPort(443, corev1.ProtocolTCP),
}
domainNames1 := []netpolv1alpha2.DomainName{"*.baidu.com."}
egressRule1 := framework.MakeClusterNetworkPolicyEgressRule("deny-baidu-wildcard", netpolv1alpha2.ClusterNetworkPolicyRuleActionDeny, ports, domainNames1)
cnp := framework.MakeClusterNetworkPolicy(cnpName, 85, namespaceSelector,
[]netpolv1alpha2.ClusterNetworkPolicyEgressRule{egressRule1}, nil)
ginkgo.By("Creating ClusterNetworkPolicy in the cluster")
createdcnp, _ := cnpClient.Create(context.TODO(), cnp, metav1.CreateOptions{})
framework.Logf("Successfully created ClusterNetworkPolicy: %s", createdcnp.Name)
ginkgo.By("Verifying cnp structure is correct")
framework.ExpectEqual(len(cnp.Spec.Egress), 1)
framework.ExpectEqual(cnp.Spec.Priority, int32(85))
waitForDNSResolversReady(cnpName, 1)
testNetworkConnectivity("https://www.baidu.com", false, "Testing connectivity to www.baidu.com after applying cnp (should be blocked by wildcard)")
testNetworkConnectivity("https://api.baidu.com", false, "Testing connectivity to api.baidu.com after applying cnp (should be blocked by wildcard)")
testNetworkConnectivity("https://news.baidu.com", false, "Testing connectivity to news.baidu.com after applying cnp (should be blocked by wildcard)")
})
})
func init() {
klog.SetOutput(ginkgo.GinkgoWriter)
config.CopyFlags(config.Flags, flag.CommandLine)
k8sframework.RegisterCommonFlags(flag.CommandLine)
k8sframework.RegisterClusterFlags(flag.CommandLine)
}
func TestE2E(t *testing.T) {
k8sframework.AfterReadingAllFlags(&k8sframework.TestContext)
e2e.RunE2ETests(t)
}