k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/windows/hybrid_network.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package windows 18 19 import ( 20 "context" 21 "fmt" 22 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/util/uuid" 26 "k8s.io/kubernetes/test/e2e/feature" 27 "k8s.io/kubernetes/test/e2e/framework" 28 e2epod "k8s.io/kubernetes/test/e2e/framework/pod" 29 e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" 30 admissionapi "k8s.io/pod-security-admission/api" 31 32 imageutils "k8s.io/kubernetes/test/utils/image" 33 34 "github.com/onsi/ginkgo/v2" 35 "github.com/onsi/gomega" 36 ) 37 38 const ( 39 linuxOS = "linux" 40 windowsOS = "windows" 41 ) 42 43 var _ = sigDescribe("Hybrid cluster network", skipUnlessWindows(func() { 44 f := framework.NewDefaultFramework("hybrid-network") 45 f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged 46 47 ginkgo.BeforeEach(func() { 48 e2eskipper.SkipUnlessNodeOSDistroIs("windows") 49 }) 50 51 ginkgo.Context("for all supported CNIs", func() { 52 53 ginkgo.It("should have stable networking for Linux and Windows pods", func(ctx context.Context) { 54 55 linuxPod := createTestPod(f, imageutils.GetE2EImage(imageutils.Nginx), linuxOS) 56 ginkgo.By("creating a linux pod and waiting for it to be running") 57 linuxPod = e2epod.NewPodClient(f).CreateSync(ctx, linuxPod) 58 59 windowsPod := createTestPod(f, imageutils.GetE2EImage(imageutils.Agnhost), windowsOS) 60 61 windowsPod.Spec.Containers[0].Args = []string{"test-webserver"} 62 ginkgo.By("creating a windows pod and waiting for it to be running") 63 windowsPod = e2epod.NewPodClient(f).CreateSync(ctx, windowsPod) 64 65 ginkgo.By("verifying pod internal connectivity to the cluster dataplane") 66 67 ginkgo.By("checking connectivity from Linux to Windows") 68 assertConsistentConnectivity(ctx, f, linuxPod.ObjectMeta.Name, linuxOS, linuxCheck(windowsPod.Status.PodIP, 80), internalMaxTries) 69 70 ginkgo.By("checking connectivity from Windows to Linux") 71 assertConsistentConnectivity(ctx, f, windowsPod.ObjectMeta.Name, windowsOS, windowsCheck(linuxPod.Status.PodIP), internalMaxTries) 72 73 }) 74 75 f.It("should provide Internet connection for Linux containers", feature.NetworkingIPv4, func(ctx context.Context) { 76 linuxPod := createTestPod(f, imageutils.GetE2EImage(imageutils.Nginx), linuxOS) 77 ginkgo.By("creating a linux pod and waiting for it to be running") 78 linuxPod = e2epod.NewPodClient(f).CreateSync(ctx, linuxPod) 79 80 ginkgo.By("verifying pod external connectivity to the internet") 81 82 ginkgo.By("checking connectivity to 8.8.8.8 53 (google.com) from Linux") 83 assertConsistentConnectivity(ctx, f, linuxPod.ObjectMeta.Name, linuxOS, linuxCheck("8.8.8.8", 53), externalMaxTries) 84 }) 85 86 f.It("should provide Internet connection and DNS for Windows containers", feature.NetworkingIPv4, feature.NetworkingDNS, func(ctx context.Context) { 87 windowsPod := createTestPod(f, imageutils.GetE2EImage(imageutils.Agnhost), windowsOS) 88 ginkgo.By("creating a windows pod and waiting for it to be running") 89 windowsPod = e2epod.NewPodClient(f).CreateSync(ctx, windowsPod) 90 91 ginkgo.By("verifying pod external connectivity to the internet") 92 93 ginkgo.By("checking connectivity to 8.8.8.8 53 (google.com) from Windows") 94 assertConsistentConnectivity(ctx, f, windowsPod.ObjectMeta.Name, windowsOS, windowsCheck("www.google.com"), externalMaxTries) 95 }) 96 97 }) 98 })) 99 100 var ( 101 warmUpDuration = "30s" 102 duration = "10s" 103 pollInterval = "1s" 104 timeoutSeconds = 10 105 106 externalMaxTries = 10 107 internalMaxTries = 1 108 ) 109 110 func assertConsistentConnectivity(ctx context.Context, f *framework.Framework, podName string, os string, cmd []string, maxTries int) { 111 connChecker := func() error { 112 var err error 113 for i := 0; i < maxTries; i++ { 114 ginkgo.By(fmt.Sprintf("checking connectivity of %s-container in %s", os, podName)) 115 stdout, stderr, err := e2epod.ExecCommandInContainerWithFullOutput(f, podName, os+"-container", cmd...) 116 if err == nil { 117 break 118 } 119 framework.Logf("Encountered error while running command: %v.\nStdout: %s\nStderr: %s\nErr: %v", cmd, stdout, stderr, err) 120 } 121 return err 122 } 123 gomega.Eventually(ctx, connChecker, warmUpDuration, pollInterval).ShouldNot(gomega.HaveOccurred()) 124 gomega.Consistently(ctx, connChecker, duration, pollInterval).ShouldNot(gomega.HaveOccurred()) 125 } 126 127 func linuxCheck(address string, port int) []string { 128 nc := fmt.Sprintf("nc -vz %s %v -w %v", address, port, timeoutSeconds) 129 cmd := []string{"/bin/sh", "-c", nc} 130 return cmd 131 } 132 133 func windowsCheck(address string) []string { 134 curl := fmt.Sprintf("curl.exe -4 %s --connect-timeout %v --fail", address, timeoutSeconds) 135 cmd := []string{"cmd", "/c", curl} 136 return cmd 137 } 138 139 func createTestPod(f *framework.Framework, image string, os string) *v1.Pod { 140 containerName := fmt.Sprintf("%s-container", os) 141 podName := "pod-" + string(uuid.NewUUID()) 142 pod := &v1.Pod{ 143 TypeMeta: metav1.TypeMeta{ 144 Kind: "Pod", 145 APIVersion: "v1", 146 }, 147 ObjectMeta: metav1.ObjectMeta{ 148 Name: podName, 149 }, 150 Spec: v1.PodSpec{ 151 Containers: []v1.Container{ 152 { 153 Name: containerName, 154 Image: image, 155 Ports: []v1.ContainerPort{{ContainerPort: 80}}, 156 }, 157 }, 158 NodeSelector: map[string]string{ 159 "kubernetes.io/os": os, 160 }, 161 }, 162 } 163 if os == linuxOS { 164 pod.Spec.Tolerations = []v1.Toleration{ 165 { 166 Operator: v1.TolerationOpExists, 167 Effect: v1.TaintEffectNoSchedule, 168 }, 169 } 170 } 171 return pod 172 }