/
Services.go
1721 lines (1444 loc) · 65.7 KB
/
Services.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 2017-2020 Authors of Cilium
//
// 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 k8sTest
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"sync"
"time"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
"github.com/asaskevich/govalidator"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
)
var _ = Describe("K8sServicesTest", func() {
const (
serviceName = "app1-service"
testDSClient = "zgroup=testDSClient"
testDS = "zgroup=testDS"
testDSK8s2 = "zgroup=test-k8s2"
echoServiceName = "echo"
echoPodLabel = "name=echo"
)
var (
kubectl *helpers.Kubectl
ciliumFilename string
backgroundCancel context.CancelFunc = func() {}
backgroundError error
enableBackgroundReport = true
k8s1NodeName string
k8s2NodeName string
outsideNodeName string
k8s1IP string
k8s2IP string
outsideIP string
)
applyPolicy := func(path string) {
By(fmt.Sprintf("Applying policy %s", path))
_, err := kubectl.CiliumPolicyAction(helpers.DefaultNamespace, path, helpers.KubectlApply, helpers.HelperTimeout)
ExpectWithOffset(1, err).Should(BeNil(), fmt.Sprintf("Error creating resource %s: %s", path, err))
}
BeforeAll(func() {
kubectl = helpers.CreateKubectl(helpers.K8s1VMName(), logger)
k8s1NodeName, k8s1IP = kubectl.GetNodeInfo(helpers.K8s1)
k8s2NodeName, k8s2IP = kubectl.GetNodeInfo(helpers.K8s2)
if helpers.ExistNodeWithoutCilium() {
outsideNodeName, outsideIP = kubectl.GetNodeInfo(helpers.GetNodeWithoutCilium())
}
ciliumFilename = helpers.TimestampFilename("cilium.yaml")
DeployCiliumAndDNS(kubectl, ciliumFilename)
})
AfterFailed(func() {
kubectl.CiliumReport(helpers.CiliumNamespace,
"cilium service list",
"cilium endpoint list")
})
JustBeforeEach(func() {
if enableBackgroundReport {
backgroundCancel, backgroundError = kubectl.BackgroundReport("uptime")
Expect(backgroundError).To(BeNil(), "Cannot start background report process")
}
})
JustAfterEach(func() {
kubectl.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
backgroundCancel()
})
AfterEach(func() {
ExpectAllPodsTerminated(kubectl)
})
AfterAll(func() {
kubectl.CloseSSHClient()
})
ciliumIPv6Backends := func(label string, port string) (backends []string) {
ciliumPods, err := kubectl.GetCiliumPods(helpers.CiliumNamespace)
Expect(err).To(BeNil(), "Cannot get cilium pods")
for _, pod := range ciliumPods {
endpointIPs := kubectl.CiliumEndpointIPv6(pod, label)
for _, ip := range endpointIPs {
backends = append(backends, net.JoinHostPort(ip, port))
}
}
ExpectWithOffset(1, backends).To(Not(BeEmpty()), "Cannot find any IPv6 backends")
return backends
}
ciliumAddService := func(id int64, frontend string, backends []string, svcType, trafficPolicy string) {
ciliumPods, err := kubectl.GetCiliumPods(helpers.CiliumNamespace)
ExpectWithOffset(1, err).To(BeNil(), "Cannot get cilium pods")
for _, pod := range ciliumPods {
err := kubectl.CiliumServiceAdd(pod, id, frontend, backends, svcType, trafficPolicy)
ExpectWithOffset(1, err).To(BeNil(), "Failed to add cilium service")
}
}
ciliumDelService := func(id int64) {
ciliumPods, err := kubectl.GetCiliumPods(helpers.CiliumNamespace)
ExpectWithOffset(1, err).To(BeNil(), "Cannot get cilium pods")
for _, pod := range ciliumPods {
// ignore result so tear down still continues on failures
_ = kubectl.CiliumServiceDel(pod, id)
}
}
testCurlFromPods := func(clientPodLabel, url string) {
// A DS with client is running in each node. So we try from each node
// that can connect to the service. To make sure that the cross-node
// service connectivity is correct we tried 10 times, so balance in the
// two nodes
pods, err := kubectl.GetPodNames(helpers.DefaultNamespace, clientPodLabel)
ExpectWithOffset(1, err).Should(BeNil(), "cannot retrieve pod names by filter %q", testDSClient)
count := 10
cmd := fmt.Sprintf(`/bin/sh -c 'set -e; for i in $(seq 1 %d); do %s; done'`, count, helpers.CurlFailNoStats(url))
for _, pod := range pods {
By("Making %d curl requests from %s pod to service %s", count, pod, url)
res := kubectl.ExecPodCmd(helpers.DefaultNamespace, pod, cmd)
ExpectWithOffset(1, res).Should(helpers.CMDSuccess(), "Request from %s pod to service %s failed", pod, url)
}
}
testCurlFromPodsFail := func(clientPodLabel, url string) {
pods, err := kubectl.GetPodNames(helpers.DefaultNamespace, clientPodLabel)
ExpectWithOffset(1, err).Should(BeNil(), "cannot retrieve pod names by filter %q", testDSClient)
for _, pod := range pods {
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, pod,
helpers.CurlFail(url))
ExpectWithOffset(1, res).ShouldNot(helpers.CMDSuccess(),
"Pod %q can unexpectedly connect to service %q", pod, url)
}
}
waitPodsDs := func() {
groups := []string{testDS, testDSClient, testDSK8s2}
for _, pod := range groups {
err := kubectl.WaitforPods(helpers.DefaultNamespace, fmt.Sprintf("-l %s", pod), helpers.HelperTimeout)
ExpectWithOffset(1, err).Should(BeNil())
}
}
Context("Checks ClusterIP Connectivity", func() {
var (
demoYAML string
echoSVCYAML string
)
BeforeAll(func() {
demoYAML = helpers.ManifestGet(kubectl.BasePath(), "demo.yaml")
echoSVCYAML = helpers.ManifestGet(kubectl.BasePath(), "echo-svc.yaml")
res := kubectl.ApplyDefault(demoYAML)
res.ExpectSuccess("unable to apply %s", demoYAML)
res = kubectl.ApplyDefault(echoSVCYAML)
res.ExpectSuccess("unable to apply %s", echoSVCYAML)
err := kubectl.WaitforPods(helpers.DefaultNamespace, "-l zgroup=testapp", helpers.HelperTimeout)
Expect(err).Should(BeNil())
err = kubectl.WaitforPods(helpers.DefaultNamespace, "-l id=app1", helpers.HelperTimeout)
Expect(err).Should(BeNil())
err = kubectl.WaitforPods(helpers.DefaultNamespace, "-l name=echo", helpers.HelperTimeout)
Expect(err).Should(BeNil())
})
AfterAll(func() {
// Explicitly ignore result of deletion of resources to avoid incomplete
// teardown if any step fails.
_ = kubectl.Delete(demoYAML)
_ = kubectl.Delete(echoSVCYAML)
})
SkipItIf(helpers.RunsWithoutKubeProxy, "Checks service on same node", func() {
clusterIP, _, err := kubectl.GetServiceHostPort(helpers.DefaultNamespace, serviceName)
Expect(err).Should(BeNil(), "Cannot get service %s", serviceName)
Expect(govalidator.IsIP(clusterIP)).Should(BeTrue(), "ClusterIP is not an IP")
By("testing connectivity via cluster IP %s", clusterIP)
ciliumPodK8s1, err := kubectl.GetCiliumPodOnNodeWithLabel(helpers.CiliumNamespace, helpers.K8s1)
Expect(err).Should(BeNil(), "Cannot get cilium pod on k8s1")
monitorRes, monitorCancel := kubectl.MonitorStart(helpers.CiliumNamespace, ciliumPodK8s1)
defer func() {
monitorCancel()
helpers.WriteToReportFile(monitorRes.CombineOutput().Bytes(), "cluster-ip-same-node.log")
}()
httpSVCURL := fmt.Sprintf("http://%s/", clusterIP)
tftpSVCURL := fmt.Sprintf("tftp://%s/hello", clusterIP)
status, err := kubectl.ExecInHostNetNS(context.TODO(), k8s1NodeName,
helpers.CurlFail(httpSVCURL))
Expect(err).To(BeNil(), "Cannot run curl in host netns")
status.ExpectSuccess("cannot curl to service IP from host")
status, err = kubectl.ExecInHostNetNS(context.TODO(), k8s1NodeName,
helpers.CurlFail(tftpSVCURL))
Expect(err).To(BeNil(), "Cannot run curl in host netns")
status.ExpectSuccess("cannot curl to service IP from host")
ciliumPods, err := kubectl.GetCiliumPods(helpers.CiliumNamespace)
Expect(err).To(BeNil(), "Cannot get cilium pods")
for _, pod := range ciliumPods {
service := kubectl.CiliumExecMustSucceed(context.TODO(), pod, "cilium service list", "Cannot retrieve services on cilium Pod")
service.ExpectContains(clusterIP, "ClusterIP is not present in the cilium service list")
}
for i := 0; i < 10; i++ {
// Send requests from "app2" pod which runs on the same node as
// "app1" pods
testCurlFromPods("id=app2", httpSVCURL)
testCurlFromPods("id=app2", tftpSVCURL)
}
})
It("Checks service accessing itself (hairpin flow)", func() {
clusterIP, _, err := kubectl.GetServiceHostPort(helpers.DefaultNamespace, echoServiceName)
Expect(err).Should(BeNil(), "Cannot get service %q ClusterIP", echoServiceName)
Expect(govalidator.IsIP(clusterIP)).Should(BeTrue(), "ClusterIP is not an IP")
url := fmt.Sprintf("http://%s/", clusterIP)
testCurlFromPods(echoPodLabel, url)
url = fmt.Sprintf("tftp://%s/hello", clusterIP)
testCurlFromPods(echoPodLabel, url)
}, 300)
SkipContextIf(helpers.RunsWithKubeProxy, "IPv6 Connectivity", func() {
// Because the deployed K8s does not have dual-stack mode enabled,
// we install the Cilium service rules manually via Cilium CLI.
demoClusterIPv6 := "fd03::100"
echoClusterIPv6 := "fd03::200"
BeforeAll(func() {
// Installs the IPv6 equivalent of app1-service (demo.yaml)
httpBackends := ciliumIPv6Backends("-l k8s:id=app1,k8s:io.kubernetes.pod.namespace=default", "80")
ciliumAddService(10080, net.JoinHostPort(demoClusterIPv6, "80"), httpBackends, "ClusterIP", "Cluster")
tftpBackends := ciliumIPv6Backends("-l k8s:id=app1,k8s:io.kubernetes.pod.namespace=default", "69")
ciliumAddService(10069, net.JoinHostPort(demoClusterIPv6, "69"), tftpBackends, "ClusterIP", "Cluster")
// Installs the IPv6 equivalent of echo (echo-svc.yaml)
httpBackends = ciliumIPv6Backends("-l k8s:name=echo,k8s:io.kubernetes.pod.namespace=default", "80")
ciliumAddService(20080, net.JoinHostPort(echoClusterIPv6, "80"), httpBackends, "ClusterIP", "Cluster")
tftpBackends = ciliumIPv6Backends("-l k8s:name=echo,k8s:io.kubernetes.pod.namespace=default", "69")
ciliumAddService(20069, net.JoinHostPort(echoClusterIPv6, "69"), tftpBackends, "ClusterIP", "Cluster")
})
AfterAll(func() {
ciliumDelService(10080)
ciliumDelService(10069)
ciliumDelService(20080)
ciliumDelService(20069)
})
It("Checks service on same node", func() {
status, err := kubectl.ExecInHostNetNS(context.TODO(), k8s1NodeName,
helpers.CurlFail(`"http://[%s]/"`, demoClusterIPv6))
Expect(err).To(BeNil(), "Cannot run curl in host netns")
status.ExpectSuccess("cannot curl to service IP from host")
status, err = kubectl.ExecInHostNetNS(context.TODO(), k8s1NodeName,
helpers.CurlFail(`"tftp://[%s]/hello"`, demoClusterIPv6))
Expect(err).To(BeNil(), "Cannot run curl in host netns")
status.ExpectSuccess("cannot curl to service IP from host")
})
It("Checks service accessing itself (hairpin flow)", func() {
url := fmt.Sprintf(`"http://[%s]/"`, echoClusterIPv6)
testCurlFromPods(echoPodLabel, url)
url = fmt.Sprintf(`"tftp://[%s]/hello"`, echoClusterIPv6)
testCurlFromPods(echoPodLabel, url)
})
})
})
Context("Checks service across nodes", func() {
var (
demoYAML string
)
BeforeAll(func() {
demoYAML = helpers.ManifestGet(kubectl.BasePath(), "demo_ds.yaml")
res := kubectl.ApplyDefault(demoYAML)
res.ExpectSuccess("Unable to apply %s", demoYAML)
waitPodsDs()
})
AfterAll(func() {
// Explicitly ignore result of deletion of resources to avoid incomplete
// teardown if any step fails.
_ = kubectl.Delete(demoYAML)
ExpectAllPodsTerminated(kubectl)
})
SkipItIf(helpers.RunsWithoutKubeProxy, "Checks ClusterIP Connectivity", func() {
service := "testds-service"
clusterIP, _, err := kubectl.GetServiceHostPort(helpers.DefaultNamespace, service)
Expect(err).Should(BeNil(), "Cannot get service %s", service)
Expect(govalidator.IsIP(clusterIP)).Should(BeTrue(), "ClusterIP is not an IP")
url := fmt.Sprintf("http://%s/", clusterIP)
testCurlFromPods(testDSClient, url)
url = fmt.Sprintf("tftp://%s/hello", clusterIP)
testCurlFromPods(testDSClient, url)
})
SkipContextIf(helpers.RunsWithKubeProxy, "IPv6 Connectivity", func() {
testDSIPv6 := "fd03::310"
BeforeAll(func() {
// Install rules for testds-service (demo_ds.yaml)
httpBackends := ciliumIPv6Backends("-l k8s:zgroup=testDS,k8s:io.kubernetes.pod.namespace=default", "80")
ciliumAddService(31080, net.JoinHostPort(testDSIPv6, "80"), httpBackends, "ClusterIP", "Cluster")
tftpBackends := ciliumIPv6Backends("-l k8s:zgroup=testDS,k8s:io.kubernetes.pod.namespace=default", "69")
ciliumAddService(31069, net.JoinHostPort(testDSIPv6, "69"), tftpBackends, "ClusterIP", "Cluster")
})
AfterAll(func() {
ciliumDelService(31080)
ciliumDelService(31069)
})
It("Checks ClusterIP Connectivity", func() {
url := fmt.Sprintf(`"http://[%s]/"`, testDSIPv6)
testCurlFromPods(testDSClient, url)
url = fmt.Sprintf(`"tftp://[%s]/hello"`, testDSIPv6)
testCurlFromPods(testDSClient, url)
})
})
getHTTPLink := func(host string, port int32) string {
return fmt.Sprintf("http://%s",
net.JoinHostPort(host, fmt.Sprintf("%d", port)))
}
getTFTPLink := func(host string, port int32) string {
// TFTP requires a filename. Otherwise the packet will be
// silently dropped by the server.
return fmt.Sprintf("tftp://%s/hello",
net.JoinHostPort(host, fmt.Sprintf("%d", port)))
}
testCurlFromPodInHostNetNS := func(url string, count int, fromPod string) {
By("Making %d curl requests from pod (host netns) %s to %q", count, fromPod, url)
cmd := fmt.Sprintf(`/bin/sh -c 'set -e; for i in $(seq 1 %d); do %s; done'`, count,
helpers.CurlFailNoStats(url))
res, err := kubectl.ExecInHostNetNS(context.TODO(), fromPod, cmd)
ExpectWithOffset(1, err).To(BeNil(), "Cannot run curl in host netns")
ExpectWithOffset(1, res).Should(helpers.CMDSuccess(),
"Request from %s to service %s failed", fromPod, url)
}
testCurlFailFromPodInHostNetNS := func(url string, count int, fromPod string) {
By("Making %d curl requests from %s to %q", count, fromPod, url)
for i := 1; i <= count; i++ {
res, err := kubectl.ExecInHostNetNS(context.TODO(), fromPod, helpers.CurlFail(url, "--max-time 3"))
ExpectWithOffset(1, err).To(BeNil(), "Cannot run curl in host netns")
ExpectWithOffset(1, res).ShouldNot(helpers.CMDSuccess(),
"%s host unexpectedly connected to service %q, it should fail", fromPod, url)
}
}
failBind := func(addr string, port int32, proto, fromPod string) {
By("Trying to bind NodePort addr %q:%d on %s", addr, port, fromPod)
res, err := kubectl.ExecInHostNetNS(context.TODO(), fromPod,
helpers.PythonBind(addr, uint16(port), proto))
ExpectWithOffset(2, err).To(BeNil(), "Cannot run python in host netns")
ExpectWithOffset(2, res).ShouldNot(helpers.CMDSuccess(),
"%s host unexpectedly was able to bind on %q:%d, it should fail", fromPod, addr, port)
}
testCurlFromPodInHostNetNSExpectingHTTPCode := func(url string, count int, expectedCode string, fromPod string) {
By("Making %d HTTP requests from %s to %q, expecting HTTP %s", count, fromPod, url, expectedCode)
for i := 1; i <= count; i++ {
res, err := kubectl.ExecInHostNetNS(context.TODO(), fromPod, helpers.CurlWithHTTPCode(url))
ExpectWithOffset(1, err).To(BeNil(), "Cannot run curl in host netns")
ExpectWithOffset(1, res).Should(helpers.CMDSuccess(),
"%s host can not connect to service %q", fromPod, url)
res.ExpectContains(expectedCode, "Request from %s to %q returned HTTP Code %q, expected %q",
fromPod, url, res.Output(), expectedCode)
}
}
testCurlFromOutsideWithLocalPort :=
func(url string, count int, checkSourceIP bool, fromPort int) {
var cmd string
By("Making %d HTTP requests from outside cluster to %q", count, url)
for i := 1; i <= count; i++ {
if fromPort == 0 {
cmd = helpers.CurlFail(url)
} else {
cmd = helpers.CurlFail("--local-port %d %s", fromPort, url)
}
if checkSourceIP {
cmd += " | grep client_address="
}
res, err := kubectl.ExecInHostNetNS(context.TODO(), outsideNodeName, cmd)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot exec in k8s3 host netns")
ExpectWithOffset(1, res).Should(helpers.CMDSuccess(),
"Can not connect to service %q from outside cluster", url)
if checkSourceIP {
// Parse the IPs to avoid issues with 4-in-6 formats
sourceIP := net.ParseIP(strings.TrimSpace(strings.Split(res.GetStdOut(), "=")[1]))
outsideIP := net.ParseIP(outsideIP)
ExpectWithOffset(1, sourceIP).To(Equal(outsideIP))
}
}
}
testCurlFromOutside := func(url string, count int, checkSourceIP bool) {
testCurlFromOutsideWithLocalPort(url, count, checkSourceIP, 0)
}
// srcPod: Name of pod sending the datagram
// srcPort: Source UDP port
// dstPodIP: Receiver pod IP (for checking in CT table)
// dstPodPort: Receiver pod port (for checking in CT table)
// dstIP: Target endpoint IP for sending the datagram
// dstPort: Target endpoint port for sending the datagram
// kubeProxy: True if kube-proxy is enabled
doFragmentedRequest := func(srcPod string, srcPort, dstPodPort int, dstIP string, dstPort int32, kubeProxy bool) {
var (
blockSize = 5120
blockCount = 1
)
ciliumPodK8s1, err := kubectl.GetCiliumPodOnNode(helpers.CiliumNamespace, helpers.K8s1)
ExpectWithOffset(2, err).Should(BeNil(), "Cannot get cilium pod on k8s1")
ciliumPodK8s2, err := kubectl.GetCiliumPodOnNode(helpers.CiliumNamespace, helpers.K8s2)
ExpectWithOffset(2, err).Should(BeNil(), "Cannot get cilium pod on k8s2")
_, dstPodIPK8s1 := kubectl.GetPodOnNodeWithOffset(helpers.K8s1, testDS, 1)
_, dstPodIPK8s2 := kubectl.GetPodOnNodeWithOffset(helpers.K8s2, testDS, 1)
// Get initial number of packets for the flow we test
// from conntrack table. The flow is probably not in
// the table the first time we check, so do not stop if
// Atoi() throws an error and simply consider we have 0
// packets.
// Field #7 is "RxPackets=<n>"
cmdIn := "cilium bpf ct list global | awk '/%s/ { sub(\".*=\",\"\", $7); print $7 }'"
endpointK8s1 := fmt.Sprintf("%s:%d", dstPodIPK8s1, dstPodPort)
patternInK8s1 := fmt.Sprintf("UDP IN [^:]+:%d -> %s", srcPort, endpointK8s1)
cmdInK8s1 := fmt.Sprintf(cmdIn, patternInK8s1)
res := kubectl.CiliumExecMustSucceed(context.TODO(), ciliumPodK8s1, cmdInK8s1)
countInK8s1, _ := strconv.Atoi(strings.TrimSpace(res.GetStdOut()))
endpointK8s2 := fmt.Sprintf("%s:%d", dstPodIPK8s2, dstPodPort)
patternInK8s2 := fmt.Sprintf("UDP IN [^:]+:%d -> %s", srcPort, endpointK8s2)
cmdInK8s2 := fmt.Sprintf(cmdIn, patternInK8s2)
res = kubectl.CiliumExecMustSucceed(context.TODO(), ciliumPodK8s2, cmdInK8s2)
countInK8s2, _ := strconv.Atoi(strings.TrimSpace(res.GetStdOut()))
// Field #11 is "TxPackets=<n>"
cmdOut := "cilium bpf ct list global | awk '/%s/ { sub(\".*=\",\"\", $11); print $11 }'"
if kubeProxy {
// If kube-proxy is enabled, we see packets in ctmap with the
// service's IP address and port, not backend's.
dstIPv4 := strings.Replace(dstIP, "::ffff:", "", 1)
endpointK8s1 = fmt.Sprintf("%s:%d", dstIPv4, dstPort)
endpointK8s2 = endpointK8s1
}
patternOutK8s1 := fmt.Sprintf("UDP OUT [^:]+:%d -> %s", srcPort, endpointK8s1)
cmdOutK8s1 := fmt.Sprintf(cmdOut, patternOutK8s1)
res = kubectl.CiliumExecMustSucceed(context.TODO(), ciliumPodK8s1, cmdOutK8s1)
countOutK8s1, _ := strconv.Atoi(strings.TrimSpace(res.GetStdOut()))
// If kube-proxy is enabled, the two commands are the same and
// there's no point executing it twice.
countOutK8s2 := 0
patternOutK8s2 := fmt.Sprintf("UDP OUT [^:]+:%d -> %s", srcPort, endpointK8s2)
cmdOutK8s2 := fmt.Sprintf(cmdOut, patternOutK8s2)
if !kubeProxy {
res = kubectl.CiliumExecMustSucceed(context.TODO(), ciliumPodK8s1, cmdOutK8s2)
countOutK8s2, _ = strconv.Atoi(strings.TrimSpace(res.GetStdOut()))
}
// Send datagram
By("Sending a fragmented packet from %s to endpoint %s:%d", srcPod, dstIP, dstPort)
cmd := fmt.Sprintf("bash -c 'dd if=/dev/zero bs=%d count=%d | nc -u -w 1 -p %d %s %d'", blockSize, blockCount, srcPort, dstIP, dstPort)
res = kubectl.ExecPodCmd(helpers.DefaultNamespace, srcPod, cmd)
res.ExpectSuccess("Cannot send fragmented datagram: %s", res.CombineOutput())
// Let's compute the expected number of packets. First
// fragment holds 1416 bytes of data under standard
// conditions for temperature, pressure and MTU.
// Following ones do not have UDP header: up to 1424
// bytes of data.
delta := 1
if blockSize*blockCount >= 1416 {
delta += (blockSize*blockCount - 1416) / 1424
if (blockSize*blockCount-1416)%1424 != 0 {
delta++
}
}
// Check that the expected packets were processed
// Because of load balancing we do not know what
// backend pod received the datagram, so we check for
// each node.
res = kubectl.CiliumExecMustSucceed(context.TODO(), ciliumPodK8s1, cmdInK8s1)
newCountInK8s1, _ := strconv.Atoi(strings.TrimSpace(res.GetStdOut()))
res = kubectl.CiliumExecMustSucceed(context.TODO(), ciliumPodK8s2, cmdInK8s2)
newCountInK8s2, _ := strconv.Atoi(strings.TrimSpace(res.GetStdOut()))
ExpectWithOffset(2, []int{newCountInK8s1, newCountInK8s2}).To(SatisfyAny(
Equal([]int{countInK8s1, countInK8s2 + delta}),
Equal([]int{countInK8s1 + delta, countInK8s2}),
), "Failed to account for IPv4 fragments to %s (in)", dstIP)
res = kubectl.CiliumExecMustSucceed(context.TODO(), ciliumPodK8s1, cmdOutK8s1)
newCountOutK8s1, _ := strconv.Atoi(strings.TrimSpace(res.GetStdOut()))
// If kube-proxy is enabled, the two commands are the same and
// there's no point executing it twice.
newCountOutK8s2 := 0
if !kubeProxy {
res = kubectl.CiliumExecMustSucceed(context.TODO(), ciliumPodK8s1, cmdOutK8s2)
newCountOutK8s2, _ = strconv.Atoi(strings.TrimSpace(res.GetStdOut()))
}
ExpectWithOffset(2, []int{newCountOutK8s1, newCountOutK8s2}).To(SatisfyAny(
Equal([]int{countOutK8s1, countOutK8s2 + delta}),
Equal([]int{countOutK8s1 + delta, countOutK8s2}),
), "Failed to account for IPv4 fragments to %s (out)", dstIP)
}
getIPv4Andv6AddrForIface := func(nodeName, iface string) (string, string) {
cmd := fmt.Sprintf("ip -4 -o a s dev %s scope global | awk '{print $4}' | cut -d/ -f1", iface)
res, err := kubectl.ExecInHostNetNS(context.TODO(), nodeName, cmd)
ExpectWithOffset(2, err).To(BeNil(), cmd)
res.ExpectSuccess(cmd)
ipv4 := strings.Trim(res.GetStdOut(), "\n")
cmd = fmt.Sprintf("ip -6 -o a s dev %s scope global | awk '{print $4}' | cut -d/ -f1", iface)
res, err = kubectl.ExecInHostNetNS(context.TODO(), nodeName, cmd)
ExpectWithOffset(2, err).To(BeNil(), cmd)
res.ExpectSuccess(cmd)
ipv6 := strings.Trim(res.GetStdOut(), "\n")
return ipv4, ipv6
}
testNodePort := func(bpfNodePort, testSecondaryNodePortIP, testFromOutside bool) {
var (
err error
data v1.Service
wg sync.WaitGroup
secondaryK8s1IPv4, secondaryK8s2IPv4 string
)
err = kubectl.Get(helpers.DefaultNamespace, "service test-nodeport").Unmarshal(&data)
ExpectWithOffset(1, err).Should(BeNil(), "Can not retrieve service")
// These are going to be tested from pods running in their own net namespaces
testURLsFromPods := []string{
getHTTPLink(data.Spec.ClusterIP, data.Spec.Ports[0].Port),
getTFTPLink(data.Spec.ClusterIP, data.Spec.Ports[1].Port),
getHTTPLink(k8s1IP, data.Spec.Ports[0].NodePort),
getTFTPLink(k8s1IP, data.Spec.Ports[1].NodePort),
getHTTPLink("::ffff:"+k8s1IP, data.Spec.Ports[0].NodePort),
getTFTPLink("::ffff:"+k8s1IP, data.Spec.Ports[1].NodePort),
getHTTPLink(k8s2IP, data.Spec.Ports[0].NodePort),
getTFTPLink(k8s2IP, data.Spec.Ports[1].NodePort),
getHTTPLink("::ffff:"+k8s2IP, data.Spec.Ports[0].NodePort),
getTFTPLink("::ffff:"+k8s2IP, data.Spec.Ports[1].NodePort),
}
// There are tested from pods running in the host net namespace
testURLsFromHosts := []string{
getHTTPLink(data.Spec.ClusterIP, data.Spec.Ports[0].Port),
getTFTPLink(data.Spec.ClusterIP, data.Spec.Ports[1].Port),
getHTTPLink("127.0.0.1", data.Spec.Ports[0].NodePort),
getTFTPLink("127.0.0.1", data.Spec.Ports[1].NodePort),
getHTTPLink("::ffff:127.0.0.1", data.Spec.Ports[0].NodePort),
getTFTPLink("::ffff:127.0.0.1", data.Spec.Ports[1].NodePort),
getHTTPLink(k8s1IP, data.Spec.Ports[0].NodePort),
getTFTPLink(k8s1IP, data.Spec.Ports[1].NodePort),
getHTTPLink("::ffff:"+k8s1IP, data.Spec.Ports[0].NodePort),
getTFTPLink("::ffff:"+k8s1IP, data.Spec.Ports[1].NodePort),
getHTTPLink(k8s2IP, data.Spec.Ports[0].NodePort),
getTFTPLink(k8s2IP, data.Spec.Ports[1].NodePort),
getHTTPLink("::ffff:"+k8s2IP, data.Spec.Ports[0].NodePort),
getTFTPLink("::ffff:"+k8s2IP, data.Spec.Ports[1].NodePort),
}
if testSecondaryNodePortIP {
secondaryK8s1IPv4, _ = getIPv4Andv6AddrForIface(k8s1NodeName, helpers.SecondaryIface)
secondaryK8s2IPv4, _ = getIPv4Andv6AddrForIface(k8s2NodeName, helpers.SecondaryIface)
testURLsFromHosts = append(testURLsFromHosts, []string{
getHTTPLink(secondaryK8s1IPv4, data.Spec.Ports[0].NodePort),
getTFTPLink(secondaryK8s1IPv4, data.Spec.Ports[1].NodePort),
getHTTPLink(secondaryK8s2IPv4, data.Spec.Ports[0].NodePort),
getTFTPLink(secondaryK8s2IPv4, data.Spec.Ports[1].NodePort),
}...)
}
testURLsFromOutside := []string{}
if testFromOutside {
// These are tested from external node which does not run
// cilium-agent (so it's not a subject to bpf_sock)
testURLsFromOutside = []string{
getHTTPLink(k8s1IP, data.Spec.Ports[0].NodePort),
getTFTPLink(k8s1IP, data.Spec.Ports[1].NodePort),
getHTTPLink(k8s2IP, data.Spec.Ports[0].NodePort),
getTFTPLink(k8s2IP, data.Spec.Ports[1].NodePort),
}
if testSecondaryNodePortIP {
testURLsFromOutside = append(testURLsFromOutside, []string{
getHTTPLink(secondaryK8s1IPv4, data.Spec.Ports[0].NodePort),
getTFTPLink(secondaryK8s1IPv4, data.Spec.Ports[1].NodePort),
getHTTPLink(secondaryK8s2IPv4, data.Spec.Ports[0].NodePort),
getTFTPLink(secondaryK8s2IPv4, data.Spec.Ports[1].NodePort),
}...)
}
}
count := 10
for _, url := range testURLsFromPods {
wg.Add(1)
go func(url string) {
defer GinkgoRecover()
defer wg.Done()
testCurlFromPods(testDSClient, url)
}(url)
}
for _, url := range testURLsFromHosts {
wg.Add(1)
go func(url string) {
defer GinkgoRecover()
defer wg.Done()
testCurlFromPodInHostNetNS(url, count, k8s1NodeName)
}(url)
}
for _, url := range testURLsFromOutside {
wg.Add(1)
go func(url string) {
defer GinkgoRecover()
defer wg.Done()
testCurlFromOutside(url, count, false)
}(url)
}
// TODO: IPv6
if bpfNodePort {
httpURL := getHTTPLink("127.0.0.1", data.Spec.Ports[0].NodePort)
tftpURL := getTFTPLink("127.0.0.1", data.Spec.Ports[1].NodePort)
testCurlFromPodsFail(testDSClient, httpURL)
testCurlFromPodsFail(testDSClient, tftpURL)
httpURL = getHTTPLink("::ffff:127.0.0.1", data.Spec.Ports[0].NodePort)
tftpURL = getTFTPLink("::ffff:127.0.0.1", data.Spec.Ports[1].NodePort)
testCurlFromPodsFail(testDSClient, httpURL)
testCurlFromPodsFail(testDSClient, tftpURL)
}
wg.Wait()
}
testExternalIPs := func() {
var data v1.Service
count := 10
err := kubectl.Get(helpers.DefaultNamespace, "service test-external-ips").Unmarshal(&data)
ExpectWithOffset(1, err).Should(BeNil(), "Can not retrieve service")
svcExternalIP := data.Spec.ExternalIPs[0]
// Append k8s1 IP addr to the external IPs for testing whether the svc
// can be reached from within a cluster via k8s1 IP addr
kubectl.Patch(helpers.DefaultNamespace, "service", "test-external-ips",
fmt.Sprintf(`{"spec":{"externalIPs":["%s","%s"]}}`, svcExternalIP, k8s1IP))
httpURL := getHTTPLink(svcExternalIP, data.Spec.Ports[0].Port)
tftpURL := getTFTPLink(svcExternalIP, data.Spec.Ports[1].Port)
// Add the route on the outside node to the external IP addr
cmd := fmt.Sprintf("ip r a %s/32 via %s", svcExternalIP, k8s1IP)
res, err := kubectl.ExecInHostNetNS(context.TODO(), outsideNodeName, cmd)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot exec in outside node %s", outsideNodeName)
ExpectWithOffset(1, res).Should(helpers.CMDSuccess(),
"Can not exec %q on outside node %s", cmd, outsideNodeName)
defer func() {
cmd = fmt.Sprintf("ip r d %s/32 via %s", svcExternalIP, k8s1IP)
kubectl.ExecInHostNetNS(context.TODO(), outsideNodeName, cmd)
}()
// Should work from outside via the external IP
testCurlFromOutside(httpURL, count, false)
testCurlFromOutside(tftpURL, count, false)
// Same from inside a pod
testCurlFromPods(testDSClient, httpURL)
testCurlFromPods(testDSClient, tftpURL)
// But not from the host netns (to prevent MITM)
testCurlFailFromPodInHostNetNS(httpURL, 1, k8s1NodeName)
testCurlFailFromPodInHostNetNS(httpURL, 1, k8s1NodeName)
testCurlFailFromPodInHostNetNS(httpURL, 1, k8s2NodeName)
testCurlFailFromPodInHostNetNS(httpURL, 1, k8s2NodeName)
// However, it should work via the k8s1 IP addr
httpURL = getHTTPLink(k8s1IP, data.Spec.Ports[0].Port)
tftpURL = getTFTPLink(k8s1IP, data.Spec.Ports[1].Port)
testCurlFromPodInHostNetNS(httpURL, count, k8s1NodeName)
testCurlFromPodInHostNetNS(tftpURL, count, k8s1NodeName)
testCurlFromPods(testDSClient, httpURL)
testCurlFromPods(testDSClient, tftpURL)
}
testFailBind := func() {
var data v1.Service
err := kubectl.Get(helpers.DefaultNamespace, "service test-nodeport").Unmarshal(&data)
ExpectWithOffset(1, err).Should(BeNil(), "Can not retrieve service")
// Ensure the NodePort cannot be bound from any redirected address
failBind("127.0.0.1", data.Spec.Ports[0].NodePort, "tcp", k8s1NodeName)
failBind("127.0.0.1", data.Spec.Ports[1].NodePort, "udp", k8s1NodeName)
failBind("", data.Spec.Ports[0].NodePort, "tcp", k8s1NodeName)
failBind("", data.Spec.Ports[1].NodePort, "udp", k8s1NodeName)
failBind("::ffff:127.0.0.1", data.Spec.Ports[0].NodePort, "tcp", k8s1NodeName)
failBind("::ffff:127.0.0.1", data.Spec.Ports[1].NodePort, "udp", k8s1NodeName)
}
testNodePortExternal := func(checkTCP, checkUDP bool) {
var data v1.Service
err := kubectl.Get(helpers.DefaultNamespace, "service test-nodeport").Unmarshal(&data)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot retrieve service")
httpURL := getHTTPLink(k8s1IP, data.Spec.Ports[0].NodePort)
tftpURL := getTFTPLink(k8s1IP, data.Spec.Ports[1].NodePort)
// Test from external connectivity
// Note:
// In case of SNAT checkSourceIP is false here since the HTTP request
// won't have the client IP but the service IP (given the request comes
// from the Cilium node to the backend, not from the client directly).
// Same in case of Hybrid mode for UDP.
testCurlFromOutside(httpURL, 10, checkTCP)
testCurlFromOutside(tftpURL, 10, checkUDP)
// Make sure all the rest works as expected as well
testNodePort(true, false, false)
// Clear CT tables on both Cilium nodes
pod, err := kubectl.GetCiliumPodOnNode(helpers.CiliumNamespace, helpers.K8s1)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot determine cilium pod name")
kubectl.CiliumExecMustSucceed(context.TODO(), pod, "cilium bpf ct flush global", "Unable to flush CT maps")
pod, err = kubectl.GetCiliumPodOnNode(helpers.CiliumNamespace, helpers.K8s2)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot determine cilium pod name")
kubectl.CiliumExecMustSucceed(context.TODO(), pod, "cilium bpf ct flush global", "Unable to flush CT maps")
}
// fromOutside=true tests session affinity implementation from lb.h, while
// fromOutside=false tests from bpf_sock.c.
testSessionAffinity := func(fromOutside, vxlan bool) {
var (
data v1.Service
dstPod string
count = 10
from string
err error
res *helpers.CmdRes
)
err = kubectl.Get(helpers.DefaultNamespace, "service test-affinity").Unmarshal(&data)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot retrieve service")
httpURL := getHTTPLink(k8s1IP, data.Spec.Ports[0].NodePort)
cmd := helpers.CurlFail(httpURL) + " | grep 'Hostname:' " // pod name is in the hostname
if fromOutside {
from = outsideNodeName
} else {
pods, err := kubectl.GetPodNames(helpers.DefaultNamespace, testDSClient)
ExpectWithOffset(1, err).Should(BeNil(), "cannot retrieve pod names by filter %q", testDSClient)
from = pods[0]
}
// Send 10 requests to the test-affinity and check that the same backend is chosen
By("Making %d HTTP requests from %s to %q (sessionAffinity)", count, from, httpURL)
for i := 1; i <= count; i++ {
if fromOutside {
res, err = kubectl.ExecInHostNetNS(context.TODO(), from, cmd)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot exec in %s host netns", from)
} else {
res = kubectl.ExecPodCmd(helpers.DefaultNamespace, from, cmd)
}
ExpectWithOffset(1, res).Should(helpers.CMDSuccess(),
"Cannot connect to service %q from %s (%d/%d)", httpURL, from, i, count)
pod := strings.TrimSpace(strings.Split(res.GetStdOut(), ": ")[1])
if i == 1 {
// Retrieve the destination pod from the first request
dstPod = pod
} else {
// Check that destination pod is always the same
ExpectWithOffset(1, dstPod).To(Equal(pod))
}
}
By("Removing %s pod so that another pod is chosen", dstPod)
// Delete the pod, and check that a new backend is chosen
kubectl.DeleteResource("pod", dstPod).ExpectSuccess("Unable to delete %s pod", dstPod)
// Wait until the replacement pod has been provisioned and appeared
// in the ipcache of the second node.
//
// The first wait should give enough time for cilium-agents to remove
// the deleted pod from the BPF LB maps, so that the next request won't
// choose the deleted pod.
waitPodsDs()
// The second wait is needed to make sure that an IPCache entry of the
// new pod appears on the k8s1 node. Otherwise, if the new pod runs
// on k8s2 and a request below selects it, the request will be dropped
// in the vxlan mode (the tailcall IPV4_NODEPORT_NAT body won't pass
// the request to the encap routines, and instead it will be dropped
// due to failing fib_lookup).
if fromOutside && vxlan {
podIPs, err := kubectl.GetPodsIPs(helpers.DefaultNamespace, testDS)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot get pod IP addrs for -l %s pods", testDS)
for _, ipAddr := range podIPs {
err = kubectl.WaitForIPCacheEntry(k8s1NodeName, ipAddr)
ExpectWithOffset(1, err).Should(BeNil(), "Failed waiting for %s ipcache entry on k8s1", ipAddr)
}
}
for i := 1; i <= count; i++ {
if fromOutside {
res, err = kubectl.ExecInHostNetNS(context.TODO(), from, cmd)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot exec in %s host netns", from)
} else {
res = kubectl.ExecPodCmd(helpers.DefaultNamespace, from, cmd)
}
ExpectWithOffset(1, res).Should(helpers.CMDSuccess(),
"Cannot connect to service %q from %s (%d/%d) after restart", httpURL, from, i, count)
pod := strings.TrimSpace(strings.Split(res.GetStdOut(), ": ")[1])
if i == 1 {
// Retrieve the destination pod from the first request
ExpectWithOffset(1, dstPod).ShouldNot(Equal(pod))
dstPod = pod
} else {
// Check that destination pod is always the same
ExpectWithOffset(1, dstPod).To(Equal(pod))
}
}
}
testExternalTrafficPolicyLocal := func() {
var (
data v1.Service
httpURL string
tftpURL string
)
// Checks requests are not SNATed when externalTrafficPolicy=Local
err := kubectl.Get(helpers.DefaultNamespace, "service test-nodeport-local").Unmarshal(&data)
ExpectWithOffset(1, err).Should(BeNil(), "Can not retrieve service")
count := 10
if helpers.ExistNodeWithoutCilium() {
httpURL = getHTTPLink(k8s1IP, data.Spec.Ports[0].NodePort)
tftpURL = getTFTPLink(k8s1IP, data.Spec.Ports[1].NodePort)
testCurlFromOutside(httpURL, count, true)
testCurlFromOutside(tftpURL, count, true)
} else {
GinkgoPrint("Skipping externalTrafficPolicy=Local test from external node")
}
// Checks that requests to k8s2 succeed, while external requests to k8s1 are dropped
err = kubectl.Get(helpers.DefaultNamespace, "service test-nodeport-local-k8s2").Unmarshal(&data)
ExpectWithOffset(1, err).Should(BeNil(), "Can not retrieve service")
httpURL = getHTTPLink(k8s2IP, data.Spec.Ports[0].NodePort)
tftpURL = getTFTPLink(k8s2IP, data.Spec.Ports[1].NodePort)
testCurlFromPodInHostNetNS(httpURL, count, k8s1NodeName)
testCurlFromPodInHostNetNS(tftpURL, count, k8s1NodeName)
testCurlFromPodInHostNetNS(httpURL, count, k8s2NodeName)
testCurlFromPodInHostNetNS(tftpURL, count, k8s2NodeName)
httpURL = getHTTPLink(k8s1IP, data.Spec.Ports[0].NodePort)
tftpURL = getTFTPLink(k8s1IP, data.Spec.Ports[1].NodePort)
// Local requests should be load-balanced
if helpers.RunsWithKubeProxy() {
// FIXME: The Nodeport BPF implementation for
// externalTrafficPolicy=Local is not compliant
// here, and the following checks fail. Let's
// disable them for now. See #11746.
testCurlFromPodInHostNetNS(httpURL, count, k8s1NodeName)
testCurlFromPodInHostNetNS(tftpURL, count, k8s1NodeName)
}
// Requests from another node are not
testCurlFailFromPodInHostNetNS(httpURL, count, k8s2NodeName)
testCurlFailFromPodInHostNetNS(tftpURL, count, k8s2NodeName)
}
testHostPort := func() {
var (
httpURL string
tftpURL string
)
httpHostPort := int32(8080)
tftpHostPort := int32(6969)
httpHostPortStr := strconv.Itoa(int(httpHostPort))
tftpHostPortStr := strconv.Itoa(int(tftpHostPort))
count := 10
pod, err := kubectl.GetCiliumPodOnNode(helpers.CiliumNamespace, helpers.K8s2)
ExpectWithOffset(1, err).Should(BeNil(), "Cannot determine cilium pod name")
res := kubectl.CiliumExecContext(context.TODO(), pod, "cilium service list | grep "+k8s2IP+":"+httpHostPortStr+" | grep HostPort")
ExpectWithOffset(1, res.GetStdOut()).ShouldNot(BeEmpty(), "No HostPort entry for "+k8s2IP+":"+httpHostPortStr)
res = kubectl.CiliumExecContext(context.TODO(), pod, "cilium service list | grep "+k8s2IP+":"+tftpHostPortStr+" | grep HostPort")
ExpectWithOffset(1, res.GetStdOut()).ShouldNot(BeEmpty(), "No HostPort entry for "+k8s2IP+":"+tftpHostPortStr)
// Cluster-internal connectivity to HostPort
httpURL = getHTTPLink(k8s2IP, httpHostPort)
tftpURL = getTFTPLink(k8s2IP, tftpHostPort)
// ... from same node
testCurlFromPodInHostNetNS(httpURL, count, k8s2NodeName)
testCurlFromPodInHostNetNS(tftpURL, count, k8s2NodeName)
// ... from different node
testCurlFromPodInHostNetNS(httpURL, count, k8s1NodeName)
testCurlFromPodInHostNetNS(tftpURL, count, k8s1NodeName)
}
testHealthCheckNodePort := func() {
var data v1.Service
// Service with HealthCheckNodePort that only has backends on k8s2
err := kubectl.Get(helpers.DefaultNamespace, "service test-lb-local-k8s2").Unmarshal(&data)
ExpectWithOffset(1, err).Should(BeNil(), "Can not retrieve service")
count := 10
// Checks that requests to k8s2 return 200
url := getHTTPLink(k8s2IP, data.Spec.HealthCheckNodePort)
testCurlFromPodInHostNetNSExpectingHTTPCode(url, count, "200", k8s1NodeName)
testCurlFromPodInHostNetNSExpectingHTTPCode(url, count, "200", k8s2NodeName)
// Checks that requests to k8s1 return 503 Service Unavailable
url = getHTTPLink(k8s1IP, data.Spec.HealthCheckNodePort)
testCurlFromPodInHostNetNSExpectingHTTPCode(url, count, "503", k8s1NodeName)
testCurlFromPodInHostNetNSExpectingHTTPCode(url, count, "503", k8s2NodeName)
}
testIPv4FragmentSupport := func() {
var (