forked from antrea-io/antrea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_test.go
1230 lines (1095 loc) · 52.2 KB
/
proxy_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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 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 (
"context"
"encoding/hex"
"fmt"
"net"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"antrea.io/antrea/pkg/agent/config"
"antrea.io/antrea/pkg/agent/types"
"antrea.io/antrea/pkg/features"
)
type expectTableFlows struct {
tableName string
flows []string
}
// TestProxy is the top-level test which contains all subtests for
// Proxy related test cases so they can share setup, teardown.
func TestProxy(t *testing.T) {
skipIfHasWindowsNodes(t)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
t.Run("testProxyServiceSessionAffinityCase", func(t *testing.T) {
testProxyServiceSessionAffinityCase(t, data)
})
t.Run("testProxyEndpointLifeCycleCase", func(t *testing.T) {
testProxyEndpointLifeCycleCase(t, data)
})
t.Run("testProxyServiceLifeCycleCase", func(t *testing.T) {
testProxyServiceLifeCycleCase(t, data)
})
}
func testProxyServiceSessionAffinityCase(t *testing.T, data *TestData) {
if len(clusterInfo.podV4NetworkCIDR) != 0 {
ipFamily := corev1.IPv4Protocol
testProxyServiceSessionAffinity(&ipFamily, []string{"169.254.169.1", "169.254.169.2"}, data, t)
}
if len(clusterInfo.podV6NetworkCIDR) != 0 {
ipFamily := corev1.IPv6Protocol
testProxyServiceSessionAffinity(&ipFamily, []string{"fd75::aabb:ccdd:ef00", "fd75::aabb:ccdd:ef01"}, data, t)
}
}
func skipIfKubeProxyEnabled(t *testing.T, data *TestData) {
_, err := data.clientset.AppsV1().DaemonSets(kubeNamespace).Get(context.TODO(), "kube-proxy", metav1.GetOptions{})
if err == nil {
t.Skipf("Skipping test because kube-proxy is running")
}
}
func probeFromNode(node string, url string, data *TestData) error {
_, _, _, err := data.RunCommandOnNode(node, fmt.Sprintf("curl --connect-timeout 1 --retry 5 --retry-connrefused %s", url))
return err
}
func probeHealthFromNode(node string, baseUrl string, data *TestData) (string, string, error) {
url := fmt.Sprintf("%s/%s", baseUrl, "healthz")
_, stdout, stderr, err := data.RunCommandOnNode(node, fmt.Sprintf("curl --connect-timeout 1 --retry 5 --retry-connrefused %s", url))
return stdout, stderr, err
}
func probeHostnameFromNode(node string, baseUrl string, data *TestData) (string, error) {
url := fmt.Sprintf("%s/%s", baseUrl, "hostname")
_, hostname, _, err := data.RunCommandOnNode(node, fmt.Sprintf("curl --connect-timeout 1 --retry 5 --retry-connrefused %s", url))
return hostname, err
}
func probeClientIPFromNode(node string, baseUrl string, data *TestData) (string, error) {
url := fmt.Sprintf("%s/%s", baseUrl, "clientip")
_, hostPort, _, err := data.RunCommandOnNode(node, fmt.Sprintf("curl --connect-timeout 1 --retry 5 --retry-connrefused %s", url))
if err != nil {
return "", err
}
host, _, err := net.SplitHostPort(hostPort)
return host, err
}
func probeFromPod(data *TestData, pod, container string, url string) error {
_, _, err := data.runWgetCommandFromTestPodWithRetry(pod, data.testNamespace, container, url, 5)
return err
}
func probeHostnameFromPod(data *TestData, pod, container string, baseUrl string) (string, error) {
url := fmt.Sprintf("%s/%s", baseUrl, "hostname")
hostname, _, err := data.runWgetCommandFromTestPodWithRetry(pod, data.testNamespace, container, url, 5)
return hostname, err
}
func probeClientIPFromPod(data *TestData, pod, container string, baseUrl string) (string, error) {
url := fmt.Sprintf("%s/%s", baseUrl, "clientip")
hostPort, _, err := data.runWgetCommandFromTestPodWithRetry(pod, data.testNamespace, container, url, 5)
if err != nil {
return "", err
}
host, _, err := net.SplitHostPort(hostPort)
return host, err
}
func reverseStrs(strs []string) []string {
var res []string
for i := len(strs) - 1; i >= 0; i-- {
res = append(res, strs[i])
}
return res
}
func TestProxyLoadBalancerServiceIPv4(t *testing.T) {
skipIfNotIPv4Cluster(t)
testProxyLoadBalancerService(t, false)
}
func TestProxyLoadBalancerServiceIPv6(t *testing.T) {
skipIfNotIPv6Cluster(t)
testProxyLoadBalancerService(t, true)
}
func testProxyLoadBalancerService(t *testing.T, isIPv6 bool) {
skipIfHasWindowsNodes(t)
skipIfNumNodesLessThan(t, 2)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
skipIfProxyAllDisabled(t, data)
// Create a busybox Pod on every Node. The busybox Pod is used as a client.
nodes := []string{nodeName(0), nodeName(1)}
var busyboxes, busyboxIPs []string
for idx, node := range nodes {
podName, ips, _ := createAndWaitForPod(t, data, data.createBusyboxPodOnNode, fmt.Sprintf("busybox-%d-", idx), node, data.testNamespace, false)
busyboxes = append(busyboxes, podName)
if !isIPv6 {
busyboxIPs = append(busyboxIPs, ips.IPv4.String())
} else {
busyboxIPs = append(busyboxIPs, ips.IPv6.String())
}
}
clusterIngressIP := []string{"169.254.169.1"}
localIngressIP := []string{"169.254.169.2"}
ipProtocol := corev1.IPv4Protocol
if isIPv6 {
ipProtocol = corev1.IPv6Protocol
clusterIngressIP = []string{"fd75::aabb:ccdd:ef00"}
localIngressIP = []string{"fd75::aabb:ccdd:ef01"}
}
// Create two LoadBalancer Services. The externalTrafficPolicy of one Service is Cluster, and the externalTrafficPolicy
// of another one is Local.
_, err = data.createAgnhostLoadBalancerService("agnhost-cluster", true, false, clusterIngressIP, &ipProtocol, nil)
require.NoError(t, err)
svc, err := data.createAgnhostLoadBalancerService("agnhost-local", true, true, localIngressIP, &ipProtocol, nil)
require.NoError(t, err)
// For the 'Local' externalTrafficPolicy, setup the health checks.
healthPort := fmt.Sprint(svc.Spec.HealthCheckNodePort)
require.NotEqual(t, "", healthPort, "HealthCheckNodePort port number should not be empty")
nodeIPs := []string{controlPlaneNodeIPv4(), workerNodeIPv4(1)}
var healthUrls []string
for _, nodeIP := range nodeIPs {
healthUrls = append(healthUrls, net.JoinHostPort(nodeIP, healthPort))
}
healthOutputTmpl := `{
"service": {
"namespace": "%s",
"name": "agnhost-local"
},
"localEndpoints": 1
}`
healthExpected := fmt.Sprintf(healthOutputTmpl, data.testNamespace)
port := "8080"
clusterUrl := net.JoinHostPort(clusterIngressIP[0], port)
localUrl := net.JoinHostPort(localIngressIP[0], port)
// Create agnhost Pods which are not on host network.
agnhosts := []string{"agnhost-0", "agnhost-1"}
for idx, node := range nodes {
createAgnhostPod(t, data, agnhosts[idx], node, false)
}
t.Run("Non-HostNetwork Endpoints", func(t *testing.T) {
loadBalancerTestCases(t, data, clusterUrl, localUrl, healthExpected, nodes, healthUrls, busyboxes, busyboxIPs, agnhosts)
})
// Delete agnhost Pods which are not on host network and create new agnhost Pods which are on host network.
hostAgnhosts := []string{"agnhost-host-0", "agnhost-host-1"}
for idx, node := range nodes {
require.NoError(t, data.DeletePod(data.testNamespace, agnhosts[idx]))
createAgnhostPod(t, data, hostAgnhosts[idx], node, true)
}
t.Run("HostNetwork Endpoints", func(t *testing.T) {
loadBalancerTestCases(t, data, clusterUrl, localUrl, healthExpected, nodes, healthUrls, busyboxes, busyboxIPs, nodes)
})
}
func loadBalancerTestCases(t *testing.T, data *TestData, clusterUrl, localUrl, healthExpected string, nodes, healthUrls, pods, podIPs, hostnames []string) {
t.Run("ExternalTrafficPolicy:Cluster/Client:Node", func(t *testing.T) {
testLoadBalancerClusterFromNode(t, data, nodes, clusterUrl)
})
t.Run("ExternalTrafficPolicy:Cluster/Client:Pod", func(t *testing.T) {
testLoadBalancerClusterFromPod(t, data, pods, clusterUrl)
})
t.Run("ExternalTrafficPolicy:Local/Client:Node", func(t *testing.T) {
testLoadBalancerLocalFromNode(t, data, nodes, healthUrls, healthExpected, localUrl)
})
t.Run("ExternalTrafficPolicy:Local/Client:Pod", func(t *testing.T) {
testLoadBalancerLocalFromPod(t, data, pods, localUrl)
})
}
func testLoadBalancerClusterFromNode(t *testing.T, data *TestData, nodes []string, url string) {
skipIfKubeProxyEnabled(t, data)
for _, node := range nodes {
require.NoError(t, probeFromNode(node, url, data), "Service LoadBalancer whose externalTrafficPolicy is Cluster should be able to be connected from Node")
}
}
func testLoadBalancerClusterFromPod(t *testing.T, data *TestData, pods []string, url string) {
for _, pod := range pods {
require.NoError(t, probeFromPod(data, pod, busyboxContainerName, url), "Service LoadBalancer whose externalTrafficPolicy is Cluster should be able to be connected from Pod")
}
}
func testLoadBalancerLocalFromNode(t *testing.T, data *TestData, nodes, healthUrls []string, healthExpected, url string) {
skipIfKubeProxyEnabled(t, data)
for _, node := range nodes {
require.NoError(t, probeFromNode(node, url, data), "Service LoadBalancer whose externalTrafficPolicy is Local should be able to be connected from Node")
for _, healthUrl := range healthUrls {
healthOutput, _, err := probeHealthFromNode(node, healthUrl, data)
require.NoError(t, err, "Service LoadBalancer whose externalTrafficPolicy is Local should have a response for healthcheck")
require.Equal(t, healthOutput, healthExpected)
}
}
}
func testLoadBalancerLocalFromPod(t *testing.T, data *TestData, pods []string, url string) {
errMsg := "Service NodePort whose externalTrafficPolicy is Local should be able to be connected from Pod"
for _, pod := range pods {
require.NoError(t, probeFromPod(data, pod, busyboxContainerName, url), errMsg)
}
}
func TestProxyNodePortServiceIPv4(t *testing.T) {
skipIfNotIPv4Cluster(t)
testProxyNodePortService(t, false)
}
func TestProxyNodePortServiceIPv6(t *testing.T) {
skipIfNotIPv6Cluster(t)
testProxyNodePortService(t, true)
}
func testProxyNodePortService(t *testing.T, isIPv6 bool) {
skipIfHasWindowsNodes(t)
skipIfNumNodesLessThan(t, 2)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
skipIfProxyAllDisabled(t, data)
nodes := []string{nodeName(0), nodeName(1)}
nodeIPs := []string{controlPlaneNodeIPv4(), workerNodeIPv4(1)}
ipProtocol := corev1.IPv4Protocol
if isIPv6 {
nodeIPs = []string{controlPlaneNodeIPv6(), workerNodeIPv6(1)}
ipProtocol = corev1.IPv6Protocol
}
// Create a busybox Pod on every Node. The busybox Pod is used as a client.
var busyboxes, busyboxIPs []string
for idx, node := range nodes {
podName, ips, _ := createAndWaitForPod(t, data, data.createBusyboxPodOnNode, fmt.Sprintf("busybox-%d-", idx), node, data.testNamespace, false)
busyboxes = append(busyboxes, podName)
if !isIPv6 {
busyboxIPs = append(busyboxIPs, ips.IPv4.String())
} else {
busyboxIPs = append(busyboxIPs, ips.IPv6.String())
}
}
// Create two NodePort Services. The externalTrafficPolicy of one Service is Cluster, and the externalTrafficPolicy
// of another one is Local.
var portCluster, portLocal string
nodePortSvc, err := data.createAgnhostNodePortService("agnhost-cluster", true, false, &ipProtocol)
require.NoError(t, err)
for _, port := range nodePortSvc.Spec.Ports {
if port.NodePort != 0 {
portCluster = fmt.Sprint(port.NodePort)
break
}
}
require.NotEqual(t, "", portCluster, "NodePort port number should not be empty")
nodePortSvc, err = data.createAgnhostNodePortService("agnhost-local", true, true, &ipProtocol)
require.NoError(t, err)
for _, port := range nodePortSvc.Spec.Ports {
if port.NodePort != 0 {
portLocal = fmt.Sprint(port.NodePort)
break
}
}
require.NotEqual(t, "", portLocal, "NodePort port number should not be empty")
// Create agnhost Pods which are not on host network.
agnhosts := []string{"agnhost-0", "agnhost-1"}
for idx, node := range nodes {
createAgnhostPod(t, data, agnhosts[idx], node, false)
}
t.Run("Non-HostNetwork Endpoints", func(t *testing.T) {
nodePortTestCases(t, data, portCluster, portLocal, nodes, nodeIPs, busyboxes, busyboxIPs, agnhosts, false)
})
// Delete agnhost Pods which are not on host network and create new agnhost Pods which are on host network.
hostAgnhosts := []string{"agnhost-host-0", "agnhost-host-1"}
for idx, node := range nodes {
require.NoError(t, data.DeletePod(data.testNamespace, agnhosts[idx]))
createAgnhostPod(t, data, hostAgnhosts[idx], node, true)
}
t.Run("HostNetwork Endpoints", func(t *testing.T) {
nodePortTestCases(t, data, portCluster, portLocal, nodes, nodeIPs, busyboxes, busyboxIPs, nodes, true)
})
}
func nodePortTestCases(t *testing.T, data *TestData, portStrCluster, portStrLocal string, nodes, nodeIPs, pods, podIPs, hostnames []string, hostNetwork bool) {
var clusterUrls, localUrls []string
for _, nodeIP := range nodeIPs {
clusterUrls = append(clusterUrls, net.JoinHostPort(nodeIP, portStrCluster))
localUrls = append(localUrls, net.JoinHostPort(nodeIP, portStrLocal))
}
t.Run("ExternalTrafficPolicy:Cluster/Client:Remote", func(t *testing.T) {
testNodePortClusterFromRemote(t, data, nodes, reverseStrs(clusterUrls))
})
t.Run("ExternalTrafficPolicy:Cluster/Client:Node", func(t *testing.T) {
testNodePortClusterFromNode(t, data, nodes, clusterUrls)
})
t.Run("ExternalTrafficPolicy:Cluster/Client:Pod", func(t *testing.T) {
testNodePortClusterFromPod(t, data, pods, clusterUrls)
})
t.Run("ExternalTrafficPolicy:Local/Client:Remote", func(t *testing.T) {
if hostNetwork {
t.Skipf("Skip this test as Endpoint is on host network")
}
testNodePortLocalFromRemote(t, data, nodes, reverseStrs(localUrls), nodeIPs, reverseStrs(hostnames))
})
t.Run("ExternalTrafficPolicy:Local/Client:Node", func(t *testing.T) {
testNodePortLocalFromNode(t, data, nodes, localUrls)
})
t.Run("ExternalTrafficPolicy:Local/Client:Pod", func(t *testing.T) {
testNodePortLocalFromPod(t, data, pods, localUrls)
})
}
func TestNodePortAndEgressWithTheSameBackendPod(t *testing.T) {
skipIfHasWindowsNodes(t)
skipIfNotIPv4Cluster(t)
skipIfNumNodesLessThan(t, 2)
skipIfAntreaIPAMTest(t)
skipIfEgressDisabled(t)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
skipIfProxyAllDisabled(t, data)
skipIfEncapModeIsNot(t, data, config.TrafficEncapModeEncap) // Egress works for encap mode only.
// Create a NodePort Service.
nodePortIP := controlPlaneNodeIPv4()
ipProtocol := corev1.IPv4Protocol
var portStr string
nodePortSvc, err := data.createNginxNodePortService("test-nodeport-svc", data.testNamespace, true, false, &ipProtocol)
require.NoError(t, err)
for _, port := range nodePortSvc.Spec.Ports {
if port.NodePort != 0 {
portStr = fmt.Sprint(port.NodePort)
break
}
}
testNodePortURL := net.JoinHostPort(nodePortIP, portStr)
// Create an Egress whose external IP is on worker Node.
egressNodeIP := workerNodeIPv4(1)
egress := data.createEgress(t, "test-egress", nil, map[string]string{"app": "nginx"}, "", egressNodeIP, nil)
defer data.crdClient.CrdV1beta1().Egresses().Delete(context.TODO(), egress.Name, metav1.DeleteOptions{})
// Create the backend Pod on control plane Node.
backendPodName := "test-nodeport-egress-backend-pod"
require.NoError(t, data.createNginxPodOnNode(backendPodName, data.testNamespace, controlPlaneNodeName(), false))
defer deletePodWrapper(t, data, data.testNamespace, backendPodName)
if err := data.podWaitForRunning(defaultTimeout, backendPodName, data.testNamespace); err != nil {
t.Fatalf("Error when waiting for Pod '%s' to be in the Running state", backendPodName)
}
// Create another netns to fake an external network on the host network Pod.
testPod := "test-client"
cmd, testNetns := getCommandInFakeExternalNetwork("sleep 3600", 24, "1.1.1.1", "1.1.1.254")
if err := NewPodBuilder(testPod, data.testNamespace, agnhostImage).OnNode(controlPlaneNodeName()).WithCommand([]string{"sh", "-c", cmd}).InHostNetwork().Privileged().Create(data); err != nil {
t.Fatalf("Failed to create client Pod: %v", err)
}
defer deletePodWrapper(t, data, data.testNamespace, testPod)
if err := data.podWaitForRunning(defaultTimeout, testPod, data.testNamespace); err != nil {
t.Fatalf("Error when waiting for Pod '%s' to be in the Running state", testPod)
}
// Connect to NodePort on control plane Node in the fake external network.
cmd = fmt.Sprintf("ip netns exec %s curl --connect-timeout 1 --retry 5 --retry-connrefused %s", testNetns, testNodePortURL)
_, _, err = data.RunCommandFromPod(data.testNamespace, testPod, agnhostContainerName, []string{"sh", "-c", cmd})
require.NoError(t, err, "Service NodePort should be able to be connected from external network when Egress is enabled")
}
func createAgnhostPod(t *testing.T, data *TestData, podName string, node string, hostNetwork bool) {
args := []string{"netexec", "--http-port=8080"}
ports := []corev1.ContainerPort{
{
Name: "http",
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
}
require.NoError(t, NewPodBuilder(podName, data.testNamespace, agnhostImage).OnNode(node).WithArgs(args).WithPorts(ports).WithHostNetwork(hostNetwork).Create(data))
_, err := data.podWaitForIPs(defaultTimeout, podName, data.testNamespace)
require.NoError(t, err)
require.NoError(t, data.podWaitForRunning(defaultTimeout, podName, data.testNamespace))
}
func testNodePortClusterFromRemote(t *testing.T, data *TestData, nodes, urls []string) {
skipIfKubeProxyEnabled(t, data)
for idx, node := range nodes {
require.NoError(t, probeFromNode(node, urls[idx], data), "Service NodePort whose externalTrafficPolicy is Cluster should be able to be connected from remote Node")
}
}
func testNodePortClusterFromNode(t *testing.T, data *TestData, nodes, urls []string) {
skipIfKubeProxyEnabled(t, data)
for idx, node := range nodes {
require.NoError(t, probeFromNode(node, urls[idx], data), "Service NodePort whose externalTrafficPolicy is Cluster should be able to be connected from Node")
}
}
func testNodePortClusterFromPod(t *testing.T, data *TestData, pods, urls []string) {
for _, url := range urls {
for _, pod := range pods {
require.NoError(t, probeFromPod(data, pod, busyboxContainerName, url), "Service NodePort whose externalTrafficPolicy is Cluster should be able to be connected from Pod")
}
}
}
func testNodePortLocalFromRemote(t *testing.T, data *TestData, nodes, urls, expectedClientIPs, expectedHostnames []string) {
skipIfKubeProxyEnabled(t, data)
errMsg := "Service NodePort whose externalTrafficPolicy is Local should be able to be connected from remote Node"
for idx, node := range nodes {
hostname, err := probeHostnameFromNode(node, urls[idx], data)
require.NoError(t, err, errMsg)
require.Equal(t, expectedHostnames[idx], hostname)
clientIP, err := probeClientIPFromNode(node, urls[idx], data)
require.NoError(t, err, errMsg)
require.Equal(t, expectedClientIPs[idx], clientIP)
}
}
func testNodePortLocalFromNode(t *testing.T, data *TestData, nodes, urls []string) {
skipIfKubeProxyEnabled(t, data)
for idx, node := range nodes {
require.NoError(t, probeFromNode(node, urls[idx], data), "Service NodePort whose externalTrafficPolicy is Local should be able to be connected from Node")
}
}
func testNodePortLocalFromPod(t *testing.T, data *TestData, pods, urls []string) {
for idx, pod := range pods {
require.NoError(t, probeFromPod(data, pod, busyboxContainerName, urls[idx]), "There should be no errors when accessing to Service NodePort whose externalTrafficPolicy is Local from Pod")
}
}
func TestProxyServiceSessionAffinity(t *testing.T) {
skipIfHasWindowsNodes(t)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
if len(clusterInfo.podV4NetworkCIDR) != 0 {
ipFamily := corev1.IPv4Protocol
testProxyServiceSessionAffinity(&ipFamily, []string{"169.254.169.1", "169.254.169.2"}, data, t)
}
if len(clusterInfo.podV6NetworkCIDR) != 0 {
ipFamily := corev1.IPv6Protocol
testProxyServiceSessionAffinity(&ipFamily, []string{"fd75::aabb:ccdd:ef00", "fd75::aabb:ccdd:ef01"}, data, t)
}
}
func TestProxyExternalTrafficPolicyIPv4(t *testing.T) {
skipIfNotIPv4Cluster(t)
testProxyExternalTrafficPolicy(t, false)
}
func TestProxyExternalTrafficPolicyIPv6(t *testing.T) {
skipIfNotIPv6Cluster(t)
testProxyExternalTrafficPolicy(t, true)
}
func testProxyExternalTrafficPolicy(t *testing.T, isIPv6 bool) {
skipIfHasWindowsNodes(t)
skipIfNumNodesLessThan(t, 2)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
skipIfProxyAllDisabled(t, data)
svcName := fmt.Sprintf("nodeport-external-traffic-policy-test-ipv6-%v", isIPv6)
nodes := []string{nodeName(0), nodeName(1)}
nodeIPs := []string{controlPlaneNodeIPv4(), workerNodeIPv4(1)}
ipProtocol := corev1.IPv4Protocol
if isIPv6 {
nodeIPs = []string{controlPlaneNodeIPv6(), workerNodeIPv6(1)}
ipProtocol = corev1.IPv6Protocol
}
// Create agnhost Pods which are not on host network.
var podNames []string
for idx, node := range nodes {
podName := fmt.Sprintf("agnhost-%d-ipv6-%v", idx, isIPv6)
createAgnhostPod(t, data, podName, node, false)
podNames = append(podNames, podName)
}
// Create a NodePort Service whose externalTrafficPolicy is Cluster and backend Pods are created above.
var portStr string
nodePortSvc, err := data.createAgnhostNodePortService(svcName, false, false, &ipProtocol)
require.NoError(t, err)
for _, port := range nodePortSvc.Spec.Ports {
if port.NodePort != 0 {
portStr = fmt.Sprint(port.NodePort)
break
}
}
require.NotEqual(t, "", portStr, "NodePort port number should not be empty")
// Get test NodePort URLs.
var urls []string
for _, nodeIP := range nodeIPs {
urls = append(urls, net.JoinHostPort(nodeIP, portStr))
}
// Hold on to make sure that the Service is realized, then test the NodePort on each Node.
time.Sleep(2 * time.Second)
testNodePortClusterFromRemote(t, data, nodes, reverseStrs(urls))
// Update the NodePort Service's externalTrafficPolicy from Cluster to Local.
_, err = data.updateServiceExternalTrafficPolicy(svcName, true)
require.NoError(t, err)
// Hold on to make sure that the update of Service is realized, then test the NodePort on each Node.
time.Sleep(2 * time.Second)
testNodePortLocalFromRemote(t, data, nodes, reverseStrs(urls), nodeIPs, reverseStrs(podNames))
}
func testProxyServiceSessionAffinity(ipFamily *corev1.IPFamily, ingressIPs []string, data *TestData, t *testing.T) {
nodeName := nodeName(1)
nginx := randName("nginx-")
require.NoError(t, data.createNginxPodOnNode(nginx, data.testNamespace, nodeName, false))
nginxIP, err := data.podWaitForIPs(defaultTimeout, nginx, data.testNamespace)
defer data.DeletePodAndWait(defaultTimeout, nginx, data.testNamespace)
require.NoError(t, err)
require.NoError(t, data.podWaitForRunning(defaultTimeout, nginx, data.testNamespace))
svc, err := data.createNginxClusterIPService(nginx, data.testNamespace, true, ipFamily)
defer data.deleteServiceAndWait(defaultTimeout, nginx, data.testNamespace)
require.NoError(t, err)
_, err = data.createNginxLoadBalancerService(true, ingressIPs, ipFamily)
defer data.deleteServiceAndWait(defaultTimeout, nginxLBService, data.testNamespace)
require.NoError(t, err)
busyboxPod := randName("busybox-")
require.NoError(t, data.createBusyboxPodOnNode(busyboxPod, data.testNamespace, nodeName, false))
defer data.DeletePodAndWait(defaultTimeout, busyboxPod, data.testNamespace)
require.NoError(t, data.podWaitForRunning(defaultTimeout, busyboxPod, data.testNamespace))
stdout, stderr, err := data.runWgetCommandOnBusyboxWithRetry(busyboxPod, data.testNamespace, svc.Spec.ClusterIP, 5)
require.NoError(t, err, fmt.Sprintf("ipFamily: %v\nstdout: %s\nstderr: %s\n", *ipFamily, stdout, stderr))
for _, ingressIP := range ingressIPs {
stdout, stderr, err := data.runWgetCommandOnBusyboxWithRetry(busyboxPod, data.testNamespace, ingressIP, 5)
require.NoError(t, err, fmt.Sprintf("ipFamily: %v\nstdout: %s\nstderr: %s\n", *ipFamily, stdout, stderr))
}
// Hold on to make sure that the Service is realized.
time.Sleep(3 * time.Second)
agentName, err := data.getAntreaPodOnNode(nodeName)
require.NoError(t, err)
tableSessionAffinityName := "SessionAffinity"
tableSessionAffinityOutput, _, err := data.RunCommandFromPod(metav1.NamespaceSystem, agentName, "antrea-agent", []string{"ovs-ofctl", "dump-flows", defaultBridgeName, fmt.Sprintf("table=%s", tableSessionAffinityName)})
require.NoError(t, err)
if *ipFamily == corev1.IPv4Protocol {
require.Contains(t, tableSessionAffinityOutput, fmt.Sprintf("nw_dst=%s,tp_dst=80", svc.Spec.ClusterIP))
require.Contains(t, tableSessionAffinityOutput, fmt.Sprintf("load:0x%s->NXM_NX_REG3[]", strings.TrimLeft(hex.EncodeToString(nginxIP.IPv4.To4()), "0")))
for _, ingressIP := range ingressIPs {
require.Contains(t, tableSessionAffinityOutput, fmt.Sprintf("nw_dst=%s,tp_dst=80", ingressIP))
}
} else {
require.Contains(t, tableSessionAffinityOutput, fmt.Sprintf("ipv6_dst=%s,tp_dst=80", svc.Spec.ClusterIP))
require.Contains(t, tableSessionAffinityOutput, fmt.Sprintf("load:0x%s->NXM_NX_XXREG3[0..63]", strings.TrimLeft(hex.EncodeToString([]byte(*nginxIP.IPv6)[8:16]), "0")))
require.Contains(t, tableSessionAffinityOutput, fmt.Sprintf("load:0x%s->NXM_NX_XXREG3[64..127]", strings.TrimLeft(hex.EncodeToString([]byte(*nginxIP.IPv6)[0:8]), "0")))
for _, ingressIP := range ingressIPs {
require.Contains(t, tableSessionAffinityOutput, fmt.Sprintf("ipv6_dst=%s,tp_dst=80", ingressIP))
}
}
}
func TestProxyHairpinIPv4(t *testing.T) {
skipIfHasWindowsNodes(t)
skipIfNotIPv4Cluster(t)
testProxyHairpin(t, false)
}
func TestProxyHairpinIPv6(t *testing.T) {
skipIfHasWindowsNodes(t)
skipIfNotIPv6Cluster(t)
testProxyHairpin(t, true)
}
func testProxyHairpin(t *testing.T, isIPv6 bool) {
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
node := nodeName(1)
workerNodeIP := workerNodeIPv4(1)
controllerNodeIP := controlPlaneNodeIPv4()
ipProtocol := corev1.IPv4Protocol
lbClusterIngressIP := []string{"192.168.240.1"}
lbLocalIngressIP := []string{"192.168.240.2"}
if isIPv6 {
workerNodeIP = workerNodeIPv6(1)
controllerNodeIP = controlPlaneNodeIPv6()
ipProtocol = corev1.IPv6Protocol
lbClusterIngressIP = []string{"fd75::aabb:ccdd:ef00"}
lbLocalIngressIP = []string{"fd75::aabb:ccdd:ef01"}
}
// Create a ClusterIP Service.
serviceClusterIP := fmt.Sprintf("clusterip-%v", isIPv6)
clusterIPSvc, err := data.createAgnhostClusterIPService(serviceClusterIP, true, &ipProtocol)
defer data.deleteServiceAndWait(defaultTimeout, serviceClusterIP, data.testNamespace)
require.NoError(t, err)
// Create two NodePort Services. The externalTrafficPolicy of one Service is Cluster, and the externalTrafficPolicy
// of another one is Local.
var nodePortCluster, nodePortLocal string
serviceNodePortCluster := fmt.Sprintf("nodeport-cluster-%v", isIPv6)
serviceNodePortLocal := fmt.Sprintf("nodeport-local-%v", isIPv6)
nodePortSvc, err := data.createAgnhostNodePortService(serviceNodePortCluster, true, false, &ipProtocol)
defer data.deleteServiceAndWait(defaultTimeout, serviceNodePortCluster, data.testNamespace)
require.NoError(t, err)
for _, port := range nodePortSvc.Spec.Ports {
if port.NodePort != 0 {
nodePortCluster = fmt.Sprint(port.NodePort)
break
}
}
require.NotEqual(t, "", nodePortCluster, "NodePort port number should not be empty")
nodePortSvc, err = data.createAgnhostNodePortService(serviceNodePortLocal, true, true, &ipProtocol)
require.NoError(t, err)
defer data.deleteServiceAndWait(defaultTimeout, serviceNodePortLocal, data.testNamespace)
for _, port := range nodePortSvc.Spec.Ports {
if port.NodePort != 0 {
nodePortLocal = fmt.Sprint(port.NodePort)
break
}
}
require.NotEqual(t, "", nodePortLocal, "NodePort port number should not be empty")
// Create two LoadBalancer Services. The externalTrafficPolicy of one Service is Cluster, and the externalTrafficPolicy
// of another one is Local.
serviceLBCluster := fmt.Sprintf("lb-cluster-%v", isIPv6)
serviceLBLocal := fmt.Sprintf("lb-local-%v", isIPv6)
_, err = data.createAgnhostLoadBalancerService(serviceLBCluster, true, false, lbClusterIngressIP, &ipProtocol, nil)
require.NoError(t, err)
_, err = data.createAgnhostLoadBalancerService(serviceLBLocal, true, true, lbLocalIngressIP, &ipProtocol, nil)
require.NoError(t, err)
// These are test urls.
port := "8080"
clusterIPUrl := net.JoinHostPort(clusterIPSvc.Spec.ClusterIP, port)
workerNodePortClusterUrl := net.JoinHostPort(workerNodeIP, nodePortCluster)
workerNodePortLocalUrl := net.JoinHostPort(workerNodeIP, nodePortLocal)
controllerNodePortClusterUrl := net.JoinHostPort(controllerNodeIP, nodePortCluster)
lbClusterUrl := net.JoinHostPort(lbClusterIngressIP[0], port)
lbLocalUrl := net.JoinHostPort(lbLocalIngressIP[0], port)
// These are expected client IP.
expectedGatewayIP, _ := nodeGatewayIPs(1)
expectedVirtualIP := config.VirtualServiceIPv4.String()
expectedControllerIP := controllerNodeIP
if isIPv6 {
_, expectedGatewayIP = nodeGatewayIPs(1)
expectedVirtualIP = config.VirtualServiceIPv6.String()
}
agnhost := fmt.Sprintf("agnhost-%v", isIPv6)
createAgnhostPod(t, data, agnhost, node, false)
t.Run("Non-HostNetwork Endpoints", func(t *testing.T) {
testProxyIntraNodeHairpinCases(data, t, expectedGatewayIP, agnhost, clusterIPUrl, workerNodePortClusterUrl, workerNodePortLocalUrl, lbClusterUrl, lbLocalUrl)
testProxyInterNodeHairpinCases(data, t, false, expectedControllerIP, nodeName(0), clusterIPUrl, controllerNodePortClusterUrl, lbClusterUrl)
})
require.NoError(t, data.DeletePod(data.testNamespace, agnhost))
agnhostHost := fmt.Sprintf("agnhost-host-%v", isIPv6)
createAgnhostPod(t, data, agnhostHost, node, true)
t.Run("HostNetwork Endpoints", func(t *testing.T) {
skipIfProxyAllDisabled(t, data)
testProxyIntraNodeHairpinCases(data, t, expectedVirtualIP, agnhostHost, clusterIPUrl, workerNodePortClusterUrl, workerNodePortLocalUrl, lbClusterUrl, lbLocalUrl)
testProxyInterNodeHairpinCases(data, t, true, expectedControllerIP, nodeName(0), clusterIPUrl, controllerNodePortClusterUrl, lbClusterUrl)
})
}
// If a Pod is not on host network, when it accesses a ClusterIP/NodePort/LoadBalancer Service whose Endpoint is on itself,
// that means a hairpin connection. Antrea gateway IP is used to SNAT the connection. The IP changes of the connection are:
// - Pod : Pod IP -> Service IP
// - OVS DNAT: Pod IP -> Pod IP
// - OVS SNAT: Antrea gateway IP -> Pod IP
// - Pod : Antrea gateway IP -> Pod IP
//
// If a Pod is on host network, when it accesses a ClusterIP/NodePort/LoadBalancer Service whose Endpoint is on itself
// (this is equivalent to that a Node accesses a Cluster/NodePort/LoadBalancer whose Endpoint is host network and the
// Endpoint is on this Node), that means a hairpin connection. A virtual IP is used to SNAT the connection to ensure
// that the packet can be routed via Antrea gateway. The IP changes of the connection are:
// - Antrea gateway: Antrea gateway IP -> Service IP
// - OVS DNAT: Antrea gateway IP -> Node IP
// - OVS SNAT: virtual IP -> Node IP
// - Antrea gateway: virtual IP -> Node IP
func testProxyIntraNodeHairpinCases(data *TestData, t *testing.T, expectedClientIP, pod, clusterIPUrl, nodePortClusterUrl, nodePortLocalUrl, lbClusterUrl, lbLocalUrl string) {
t.Run("IntraNode/ClusterIP", func(t *testing.T) {
clientIP, err := probeClientIPFromPod(data, pod, agnhostContainerName, clusterIPUrl)
require.NoError(t, err, "ClusterIP hairpin should be able to be connected")
require.Equal(t, expectedClientIP, clientIP)
})
t.Run("IntraNode/NodePort/ExternalTrafficPolicy:Cluster", func(t *testing.T) {
skipIfProxyAllDisabled(t, data)
clientIP, err := probeClientIPFromPod(data, pod, agnhostContainerName, nodePortClusterUrl)
require.NoError(t, err, "NodePort whose externalTrafficPolicy is Cluster hairpin should be able to be connected")
require.Equal(t, expectedClientIP, clientIP)
})
t.Run("IntraNode/NodePort/ExternalTrafficPolicy:Local", func(t *testing.T) {
skipIfProxyAllDisabled(t, data)
clientIP, err := probeClientIPFromPod(data, pod, agnhostContainerName, nodePortLocalUrl)
require.NoError(t, err, "NodePort whose externalTrafficPolicy is Local hairpin should be able to be connected")
require.Equal(t, expectedClientIP, clientIP)
})
t.Run("IntraNode/LoadBalancer/ExternalTrafficPolicy:Cluster", func(t *testing.T) {
clientIP, err := probeClientIPFromPod(data, pod, agnhostContainerName, lbClusterUrl)
require.NoError(t, err, "LoadBalancer whose externalTrafficPolicy is Cluster hairpin should be able to be connected")
require.Equal(t, expectedClientIP, clientIP)
})
t.Run("IntraNode/LoadBalancer/ExternalTrafficPolicy:Local", func(t *testing.T) {
clientIP, err := probeClientIPFromPod(data, pod, agnhostContainerName, lbLocalUrl)
require.NoError(t, err, "LoadBalancer whose externalTrafficPolicy is Local hairpin should be able to be connected")
require.Equal(t, expectedClientIP, clientIP)
})
}
// If client is Node A, when it accesses a ClusterIP/NodePort/LoadBalancer Service whose Endpoint is on Node B, below
// cases are hairpin (assumed that feature AntreaIPAM is not enabled):
// - Traffic mode: encap, Endpoint network: host network, OS: Linux/Windows
// - Traffic mode: noEncap, Endpoint network: not host network, OS: Linux (packets are routed via uplink interface)
// - Traffic mode: noEncap, Endpoint network: host network, OS: Linux/Windows
// The IP changes of the hairpin connections are:
// - Node A Antrea gateway: Antrea gateway IP -> Service IP
// - OVS DNAT: Antrea gateway IP -> Endpoint IP
// - OVS SNAT: virtual IP -> Endpoint IP
// - Node A Antrea gateway: virtual IP -> Endpoint IP
// - Node A output: Node A IP -> Endpoint IP (another SNAT for virtual IP, otherwise reply packets can't be routed back).
// - Node B: Node A IP -> Endpoint IP
func testProxyInterNodeHairpinCases(data *TestData, t *testing.T, hostNetwork bool, expectedClientIP, node, clusterIPUrl, nodePortClusterUrl, lbClusterUrl string) {
skipIfAntreaIPAMTest(t)
currentEncapMode, err := data.GetEncapMode()
if err != nil {
t.Fatalf("Failed to get encap mode: %v", err)
}
if !hostNetwork {
if testOptions.providerName == "kind" && (currentEncapMode == config.TrafficEncapModeEncap || currentEncapMode == config.TrafficEncapModeHybrid) {
t.Skipf("Skipping test because inter-Node Pod traffic is encapsulated when testbed is Kind and traffic mode is encap/hybrid")
} else if currentEncapMode == config.TrafficEncapModeEncap {
t.Skipf("Skipping test because inter-Node Pod traffic is encapsulated when testbed is not Kind and traffic mode encap")
}
}
t.Run("InterNode/ClusterIP", func(t *testing.T) {
clientIP, err := probeClientIPFromNode(node, clusterIPUrl, data)
require.NoError(t, err, "ClusterIP hairpin should be able to be connected")
require.Equal(t, expectedClientIP, clientIP)
})
t.Run("InterNode/NodePort/ExternalTrafficPolicy:Cluster", func(t *testing.T) {
skipIfProxyAllDisabled(t, data)
if !hostNetwork && currentEncapMode == config.TrafficEncapModeNoEncap {
skipIfHasWindowsNodes(t)
}
clientIP, err := probeClientIPFromNode(node, nodePortClusterUrl, data)
require.NoError(t, err, "NodePort whose externalTrafficPolicy is Cluster hairpin should be able to be connected")
require.Equal(t, expectedClientIP, clientIP)
})
t.Run("InterNode/LoadBalancer/ExternalTrafficPolicy:Cluster", func(t *testing.T) {
skipIfProxyAllDisabled(t, data)
if !hostNetwork && currentEncapMode == config.TrafficEncapModeNoEncap {
skipIfHasWindowsNodes(t)
}
clientIP, err := probeClientIPFromNode(node, lbClusterUrl, data)
require.NoError(t, err, "LoadBalancer whose externalTrafficPolicy is Cluster hairpin should be able to be connected")
require.Equal(t, expectedClientIP, clientIP)
})
}
func testProxyEndpointLifeCycleCase(t *testing.T, data *TestData) {
if len(clusterInfo.podV4NetworkCIDR) != 0 {
ipFamily := corev1.IPv4Protocol
testProxyEndpointLifeCycle(&ipFamily, data, t)
}
if len(clusterInfo.podV6NetworkCIDR) != 0 {
ipFamily := corev1.IPv6Protocol
testProxyEndpointLifeCycle(&ipFamily, data, t)
}
}
func TestProxyEndpointLifeCycle(t *testing.T) {
skipIfHasWindowsNodes(t)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
if len(clusterInfo.podV4NetworkCIDR) != 0 {
ipFamily := corev1.IPv4Protocol
testProxyEndpointLifeCycle(&ipFamily, data, t)
}
if len(clusterInfo.podV6NetworkCIDR) != 0 {
ipFamily := corev1.IPv6Protocol
testProxyEndpointLifeCycle(&ipFamily, data, t)
}
}
func testProxyEndpointLifeCycle(ipFamily *corev1.IPFamily, data *TestData, t *testing.T) {
nodeName := nodeName(1)
nginx := randName("nginx-")
require.NoError(t, data.createNginxPodOnNode(nginx, data.testNamespace, nodeName, false))
nginxIPs, err := data.podWaitForIPs(defaultTimeout, nginx, data.testNamespace)
require.NoError(t, err)
_, err = data.createNginxClusterIPService(nginx, data.testNamespace, false, ipFamily)
defer data.deleteServiceAndWait(defaultTimeout, nginx, data.testNamespace)
require.NoError(t, err)
// Hold on to make sure that the Service is realized.
time.Sleep(3 * time.Second)
agentName, err := data.getAntreaPodOnNode(nodeName)
require.NoError(t, err)
var nginxIP string
if *ipFamily == corev1.IPv6Protocol {
nginxIP = nginxIPs.IPv6.String()
} else {
nginxIP = nginxIPs.IPv4.String()
}
keywords := make(map[string]string)
keywords["EndpointDNAT"] = fmt.Sprintf("nat(dst=%s)", net.JoinHostPort(nginxIP, "80")) // endpointNATTable
var groupKeywords []string
if *ipFamily == corev1.IPv6Protocol {
groupKeywords = append(groupKeywords,
fmt.Sprintf("load:0x%s->NXM_NX_XXREG3[0..63],load:0x%s->NXM_NX_XXREG3[64..127]", strings.TrimLeft(hex.EncodeToString((*nginxIPs.IPv6)[8:16]), "0"), strings.TrimLeft(hex.EncodeToString((*nginxIPs.IPv6)[:8]), "0")))
} else {
groupKeywords = append(groupKeywords, fmt.Sprintf("0x%s->NXM_NX_REG3[]", strings.TrimLeft(hex.EncodeToString(nginxIPs.IPv4.To4()), "0")))
}
for tableName, keyword := range keywords {
tableOutput, _, err := data.RunCommandFromPod(metav1.NamespaceSystem, agentName, "antrea-agent", []string{"ovs-ofctl", "dump-flows", defaultBridgeName, fmt.Sprintf("table=%s", tableName)})
require.NoError(t, err)
require.Contains(t, tableOutput, keyword)
}
groupOutput, _, err := data.RunCommandFromPod(metav1.NamespaceSystem, agentName, "antrea-agent", []string{"ovs-ofctl", "dump-groups", defaultBridgeName})
require.NoError(t, err)
for _, k := range groupKeywords {
require.Contains(t, groupOutput, k)
}
require.NoError(t, data.DeletePodAndWait(defaultTimeout, nginx, data.testNamespace))
// Wait for one second to make sure the pipeline to be updated.
time.Sleep(time.Second)
for tableName, keyword := range keywords {
tableOutput, _, err := data.RunCommandFromPod(metav1.NamespaceSystem, agentName, "antrea-agent", []string{"ovs-ofctl", "dump-flows", defaultBridgeName, fmt.Sprintf("table=%s", tableName)})
require.NoError(t, err)
require.NotContains(t, tableOutput, keyword)
}
groupOutput, _, err = data.RunCommandFromPod(metav1.NamespaceSystem, agentName, "antrea-agent", []string{"ovs-ofctl", "dump-groups", defaultBridgeName})
require.NoError(t, err)
for _, k := range groupKeywords {
require.NotContains(t, groupOutput, k)
}
}
func testProxyServiceLifeCycleCase(t *testing.T, data *TestData) {
if len(clusterInfo.podV4NetworkCIDR) != 0 {
ipFamily := corev1.IPv4Protocol
testProxyServiceLifeCycle(&ipFamily, []string{"169.254.169.1", "169.254.169.2"}, data, t)
}
if len(clusterInfo.podV6NetworkCIDR) != 0 {
ipFamily := corev1.IPv6Protocol
testProxyServiceLifeCycle(&ipFamily, []string{"fd75::aabb:ccdd:ef00", "fd75::aabb:ccdd:ef01"}, data, t)
}
}
func TestProxyServiceLifeCycle(t *testing.T) {
skipIfHasWindowsNodes(t)
data, err := setupTest(t)
if err != nil {
t.Fatalf("Error when setting up test: %v", err)
}
defer teardownTest(t, data)
skipIfProxyDisabled(t, data)
if len(clusterInfo.podV4NetworkCIDR) != 0 {
ipFamily := corev1.IPv4Protocol
testProxyServiceLifeCycle(&ipFamily, []string{"169.254.169.1", "169.254.169.2"}, data, t)
}
if len(clusterInfo.podV6NetworkCIDR) != 0 {
ipFamily := corev1.IPv6Protocol
testProxyServiceLifeCycle(&ipFamily, []string{"fd75::aabb:ccdd:ef00", "fd75::aabb:ccdd:ef01"}, data, t)
}
}
func testProxyServiceLifeCycle(ipFamily *corev1.IPFamily, ingressIPs []string, data *TestData, t *testing.T) {
nodeName := nodeName(1)
nginx := randName("nginx-")
require.NoError(t, data.createNginxPodOnNode(nginx, data.testNamespace, nodeName, false))
defer data.DeletePodAndWait(defaultTimeout, nginx, data.testNamespace)
nginxIPs, err := data.podWaitForIPs(defaultTimeout, nginx, data.testNamespace)
require.NoError(t, err)
var nginxIP string
if *ipFamily == corev1.IPv6Protocol {
nginxIP = nginxIPs.IPv6.String()