dual_stack.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package network
  14. import (
  15. "context"
  16. "fmt"
  17. "net"
  18. "time"
  19. "github.com/onsi/ginkgo"
  20. "github.com/onsi/gomega"
  21. appsv1 "k8s.io/api/apps/v1"
  22. v1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/util/wait"
  25. clientset "k8s.io/client-go/kubernetes"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. e2edeploy "k8s.io/kubernetes/test/e2e/framework/deployment"
  28. e2enode "k8s.io/kubernetes/test/e2e/framework/node"
  29. e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
  30. imageutils "k8s.io/kubernetes/test/utils/image"
  31. netutils "k8s.io/utils/net"
  32. )
  33. // Tests for ipv6 dual stack feature
  34. var _ = SIGDescribe("[Feature:IPv6DualStackAlphaFeature] [LinuxOnly]", func() {
  35. f := framework.NewDefaultFramework("dualstack")
  36. var cs clientset.Interface
  37. var podClient *framework.PodClient
  38. ginkgo.BeforeEach(func() {
  39. cs = f.ClientSet
  40. podClient = f.PodClient()
  41. })
  42. ginkgo.It("should have ipv4 and ipv6 internal node ip", func() {
  43. // TODO (aramase) can switch to new function to get all nodes
  44. nodeList, err := e2enode.GetReadySchedulableNodes(cs)
  45. framework.ExpectNoError(err)
  46. for _, node := range nodeList.Items {
  47. // get all internal ips for node
  48. internalIPs := e2enode.GetAddresses(&node, v1.NodeInternalIP)
  49. framework.ExpectEqual(len(internalIPs), 2)
  50. // assert 2 ips belong to different families
  51. framework.ExpectEqual(isIPv4(internalIPs[0]) != isIPv4(internalIPs[1]), true)
  52. }
  53. })
  54. ginkgo.It("should have ipv4 and ipv6 node podCIDRs", func() {
  55. // TODO (aramase) can switch to new function to get all nodes
  56. nodeList, err := e2enode.GetReadySchedulableNodes(cs)
  57. framework.ExpectNoError(err)
  58. for _, node := range nodeList.Items {
  59. framework.ExpectEqual(len(node.Spec.PodCIDRs), 2)
  60. // assert podCIDR is same as podCIDRs[0]
  61. framework.ExpectEqual(node.Spec.PodCIDR, node.Spec.PodCIDRs[0])
  62. // assert one is ipv4 and other is ipv6
  63. framework.ExpectEqual(isIPv4CIDR(node.Spec.PodCIDRs[0]) != isIPv4CIDR(node.Spec.PodCIDRs[1]), true)
  64. }
  65. })
  66. ginkgo.It("should create pod, add ipv6 and ipv4 ip to pod ips", func() {
  67. podName := "pod-dualstack-ips"
  68. pod := &v1.Pod{
  69. ObjectMeta: metav1.ObjectMeta{
  70. Name: podName,
  71. Labels: map[string]string{"test": "dualstack-pod-ips"},
  72. },
  73. Spec: v1.PodSpec{
  74. Containers: []v1.Container{
  75. {
  76. Name: "dualstack-pod-ips",
  77. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  78. },
  79. },
  80. },
  81. }
  82. ginkgo.By("submitting the pod to kubernetes")
  83. podClient.CreateSync(pod)
  84. framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
  85. p, err := podClient.Get(context.TODO(), pod.Name, metav1.GetOptions{})
  86. framework.ExpectNoError(err, "Failed to get pod %q", pod.Name)
  87. gomega.Expect(p.Status.PodIP).ShouldNot(gomega.BeEquivalentTo(""))
  88. gomega.Expect(p.Status.PodIPs).ShouldNot(gomega.BeNil())
  89. // validate there are 2 ips in podIPs
  90. framework.ExpectEqual(len(p.Status.PodIPs), 2)
  91. // validate first ip in PodIPs is same as PodIP
  92. framework.ExpectEqual(p.Status.PodIP, p.Status.PodIPs[0].IP)
  93. // assert 2 pod ips belong to different families
  94. framework.ExpectEqual(isIPv4(p.Status.PodIPs[0].IP) != isIPv4(p.Status.PodIPs[1].IP), true)
  95. ginkgo.By("deleting the pod")
  96. err = podClient.Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(30))
  97. framework.ExpectNoError(err, "failed to delete pod")
  98. })
  99. // takes close to 140s to complete, so doesn't need to be marked [SLOW]
  100. // this test is tagged with phase2 so we can skip this until phase 2 is completed and merged
  101. // TODO (aramase) remove phase 2 tag once phase 2 of dual stack is merged
  102. ginkgo.It("should be able to reach pod on ipv4 and ipv6 ip [Feature:IPv6DualStackAlphaFeature:Phase2]", func() {
  103. serverDeploymentName := "dualstack-server"
  104. clientDeploymentName := "dualstack-client"
  105. // get all schedulable nodes to determine the number of replicas for pods
  106. // this is to ensure connectivity from all nodes on cluster
  107. // FIXME: tests may be run in large clusters. This test is O(n^2) in the
  108. // number of nodes used. It should use GetBoundedReadySchedulableNodes().
  109. nodeList, err := e2enode.GetReadySchedulableNodes(cs)
  110. framework.ExpectNoError(err)
  111. replicas := int32(len(nodeList.Items))
  112. serverDeploymentSpec := e2edeploy.NewDeployment(serverDeploymentName,
  113. replicas,
  114. map[string]string{"test": "dual-stack-server"},
  115. "dualstack-test-server",
  116. imageutils.GetE2EImage(imageutils.TestWebserver),
  117. appsv1.RollingUpdateDeploymentStrategyType)
  118. // to ensure all the pods land on different nodes and we can thereby
  119. // validate connectivity across all nodes.
  120. serverDeploymentSpec.Spec.Template.Spec.Affinity = &v1.Affinity{
  121. PodAntiAffinity: &v1.PodAntiAffinity{
  122. RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
  123. {
  124. LabelSelector: &metav1.LabelSelector{
  125. MatchExpressions: []metav1.LabelSelectorRequirement{
  126. {
  127. Key: "test",
  128. Operator: metav1.LabelSelectorOpIn,
  129. Values: []string{"dualstack-test-server"},
  130. },
  131. },
  132. },
  133. TopologyKey: "kubernetes.io/hostname",
  134. },
  135. },
  136. },
  137. }
  138. clientDeploymentSpec := e2edeploy.NewDeployment(clientDeploymentName,
  139. replicas,
  140. map[string]string{"test": "dual-stack-client"},
  141. "dualstack-test-client",
  142. imageutils.GetE2EImage(imageutils.Agnhost),
  143. appsv1.RollingUpdateDeploymentStrategyType)
  144. clientDeploymentSpec.Spec.Template.Spec.Containers[0].Command = []string{"sleep", "3600"}
  145. clientDeploymentSpec.Spec.Template.Spec.Affinity = &v1.Affinity{
  146. PodAntiAffinity: &v1.PodAntiAffinity{
  147. RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
  148. {
  149. LabelSelector: &metav1.LabelSelector{
  150. MatchExpressions: []metav1.LabelSelectorRequirement{
  151. {
  152. Key: "test",
  153. Operator: metav1.LabelSelectorOpIn,
  154. Values: []string{"dualstack-test-client"},
  155. },
  156. },
  157. },
  158. TopologyKey: "kubernetes.io/hostname",
  159. },
  160. },
  161. },
  162. }
  163. serverDeployment, err := cs.AppsV1().Deployments(f.Namespace.Name).Create(context.TODO(), serverDeploymentSpec, metav1.CreateOptions{})
  164. framework.ExpectNoError(err)
  165. clientDeployment, err := cs.AppsV1().Deployments(f.Namespace.Name).Create(context.TODO(), clientDeploymentSpec, metav1.CreateOptions{})
  166. framework.ExpectNoError(err)
  167. err = e2edeploy.WaitForDeploymentComplete(cs, serverDeployment)
  168. framework.ExpectNoError(err)
  169. err = e2edeploy.WaitForDeploymentComplete(cs, clientDeployment)
  170. framework.ExpectNoError(err)
  171. serverPods, err := e2edeploy.GetPodsForDeployment(cs, serverDeployment)
  172. framework.ExpectNoError(err)
  173. clientPods, err := e2edeploy.GetPodsForDeployment(cs, clientDeployment)
  174. framework.ExpectNoError(err)
  175. assertNetworkConnectivity(f, *serverPods, *clientPods, "dualstack-test-client", "80")
  176. })
  177. ginkgo.It("should create service with cluster ip from primary service range [Feature:IPv6DualStackAlphaFeature:Phase2]", func() {
  178. serviceName := "defaultclusterip"
  179. ns := f.Namespace.Name
  180. jig := e2eservice.NewTestJig(cs, ns, serviceName)
  181. defaultIPFamily := v1.IPv4Protocol
  182. if framework.TestContext.ClusterIsIPv6() {
  183. defaultIPFamily = v1.IPv6Protocol
  184. }
  185. t := NewServerTest(cs, ns, serviceName)
  186. defer func() {
  187. defer ginkgo.GinkgoRecover()
  188. if errs := t.Cleanup(); len(errs) != 0 {
  189. framework.Failf("errors in cleanup: %v", errs)
  190. }
  191. }()
  192. ginkgo.By("creating service " + ns + "/" + serviceName + " with Service.Spec.IPFamily not set")
  193. service := createService(t.ServiceName, t.Namespace, t.Labels, nil)
  194. jig.Labels = t.Labels
  195. err := jig.CreateServicePods(2)
  196. framework.ExpectNoError(err)
  197. svc, err := t.CreateService(service)
  198. framework.ExpectNoError(err, "failed to create service: %s in namespace: %s", serviceName, ns)
  199. validateNumOfServicePorts(svc, 2)
  200. // check the spec has been set to default ip family
  201. validateServiceAndClusterIPFamily(svc, defaultIPFamily)
  202. // ensure endpoint belong to same ipfamily as service
  203. if err := wait.PollImmediate(500*time.Millisecond, 10*time.Second, func() (bool, error) {
  204. endpoint, err := cs.CoreV1().Endpoints(svc.Namespace).Get(context.TODO(), svc.Name, metav1.GetOptions{})
  205. if err != nil {
  206. return false, nil
  207. }
  208. validateEndpointsBelongToIPFamily(svc, endpoint, defaultIPFamily)
  209. return true, nil
  210. }); err != nil {
  211. framework.Failf("Get endpoints for service %s/%s failed (%s)", svc.Namespace, svc.Name, err)
  212. }
  213. })
  214. ginkgo.It("should create service with ipv4 cluster ip [Feature:IPv6DualStackAlphaFeature:Phase2]", func() {
  215. serviceName := "ipv4clusterip"
  216. ns := f.Namespace.Name
  217. ipv4 := v1.IPv4Protocol
  218. jig := e2eservice.NewTestJig(cs, ns, serviceName)
  219. t := NewServerTest(cs, ns, serviceName)
  220. defer func() {
  221. defer ginkgo.GinkgoRecover()
  222. if errs := t.Cleanup(); len(errs) != 0 {
  223. framework.Failf("errors in cleanup: %v", errs)
  224. }
  225. }()
  226. ginkgo.By("creating service " + ns + "/" + serviceName + " with Service.Spec.IPFamily IPv4" + ns)
  227. service := createService(t.ServiceName, t.Namespace, t.Labels, &ipv4)
  228. jig.Labels = t.Labels
  229. err := jig.CreateServicePods(2)
  230. framework.ExpectNoError(err)
  231. svc, err := t.CreateService(service)
  232. framework.ExpectNoError(err, "failed to create service: %s in namespace: %s", serviceName, ns)
  233. validateNumOfServicePorts(svc, 2)
  234. // check the spec has been set to IPv4 and cluster ip belong to IPv4 family
  235. validateServiceAndClusterIPFamily(svc, ipv4)
  236. // ensure endpoints belong to same ipfamily as service
  237. if err := wait.PollImmediate(500*time.Millisecond, 10*time.Second, func() (bool, error) {
  238. endpoint, err := cs.CoreV1().Endpoints(svc.Namespace).Get(context.TODO(), svc.Name, metav1.GetOptions{})
  239. if err != nil {
  240. return false, nil
  241. }
  242. validateEndpointsBelongToIPFamily(svc, endpoint, ipv4)
  243. return true, nil
  244. }); err != nil {
  245. framework.Failf("Get endpoints for service %s/%s failed (%s)", svc.Namespace, svc.Name, err)
  246. }
  247. })
  248. ginkgo.It("should create service with ipv6 cluster ip [Feature:IPv6DualStackAlphaFeature:Phase2]", func() {
  249. serviceName := "ipv6clusterip"
  250. ns := f.Namespace.Name
  251. ipv6 := v1.IPv6Protocol
  252. jig := e2eservice.NewTestJig(cs, ns, serviceName)
  253. t := NewServerTest(cs, ns, serviceName)
  254. defer func() {
  255. defer ginkgo.GinkgoRecover()
  256. if errs := t.Cleanup(); len(errs) != 0 {
  257. framework.Failf("errors in cleanup: %v", errs)
  258. }
  259. }()
  260. ginkgo.By("creating service " + ns + "/" + serviceName + " with Service.Spec.IPFamily IPv6" + ns)
  261. service := createService(t.ServiceName, t.Namespace, t.Labels, &ipv6)
  262. jig.Labels = t.Labels
  263. err := jig.CreateServicePods(2)
  264. framework.ExpectNoError(err)
  265. svc, err := t.CreateService(service)
  266. framework.ExpectNoError(err, "failed to create service: %s in namespace: %s", serviceName, ns)
  267. validateNumOfServicePorts(svc, 2)
  268. // check the spec has been set to IPv6 and cluster ip belongs to IPv6 family
  269. validateServiceAndClusterIPFamily(svc, ipv6)
  270. // ensure endpoints belong to same ipfamily as service
  271. if err := wait.PollImmediate(500*time.Millisecond, 10*time.Second, func() (bool, error) {
  272. endpoint, err := cs.CoreV1().Endpoints(svc.Namespace).Get(context.TODO(), svc.Name, metav1.GetOptions{})
  273. if err != nil {
  274. return false, nil
  275. }
  276. validateEndpointsBelongToIPFamily(svc, endpoint, ipv6)
  277. return true, nil
  278. }); err != nil {
  279. framework.Failf("Get endpoints for service %s/%s failed (%s)", svc.Namespace, svc.Name, err)
  280. }
  281. })
  282. })
  283. func validateNumOfServicePorts(svc *v1.Service, expectedNumOfPorts int) {
  284. if len(svc.Spec.Ports) != expectedNumOfPorts {
  285. framework.Failf("got unexpected len(Spec.Ports) for service: %v", svc)
  286. }
  287. }
  288. func validateServiceAndClusterIPFamily(svc *v1.Service, expectedIPFamily v1.IPFamily) {
  289. if svc.Spec.IPFamily == nil {
  290. framework.Failf("service ip family nil for service %s/%s", svc.Namespace, svc.Name)
  291. }
  292. if *svc.Spec.IPFamily != expectedIPFamily {
  293. framework.Failf("ip family mismatch for service: %s/%s, expected: %s, actual: %s", svc.Namespace, svc.Name, expectedIPFamily, *svc.Spec.IPFamily)
  294. }
  295. isIPv6ClusterIP := netutils.IsIPv6String(svc.Spec.ClusterIP)
  296. if (expectedIPFamily == v1.IPv4Protocol && isIPv6ClusterIP) || (expectedIPFamily == v1.IPv6Protocol && !isIPv6ClusterIP) {
  297. framework.Failf("got unexpected service ip %s, should belong to %s ip family", svc.Spec.ClusterIP, expectedIPFamily)
  298. }
  299. }
  300. func validateEndpointsBelongToIPFamily(svc *v1.Service, endpoint *v1.Endpoints, expectedIPFamily v1.IPFamily) {
  301. if len(endpoint.Subsets) == 0 {
  302. framework.Failf("Endpoint has no subsets, cannot determine service ip family matches endpoints ip family for service %s/%s", svc.Namespace, svc.Name)
  303. }
  304. for _, ss := range endpoint.Subsets {
  305. for _, e := range ss.Addresses {
  306. if (expectedIPFamily == v1.IPv6Protocol && isIPv4(e.IP)) || (expectedIPFamily == v1.IPv4Protocol && netutils.IsIPv6String(e.IP)) {
  307. framework.Failf("service endpoint %s doesn't belong to %s ip family", e.IP, expectedIPFamily)
  308. }
  309. }
  310. }
  311. }
  312. func assertNetworkConnectivity(f *framework.Framework, serverPods v1.PodList, clientPods v1.PodList, containerName, port string) {
  313. // curl from each client pod to all server pods to assert connectivity
  314. duration := "10s"
  315. pollInterval := "1s"
  316. timeout := 10
  317. var serverIPs []string
  318. for _, pod := range serverPods.Items {
  319. if pod.Status.PodIPs == nil || len(pod.Status.PodIPs) != 2 {
  320. framework.Failf("PodIPs list not expected value, got %v", pod.Status.PodIPs)
  321. }
  322. if isIPv4(pod.Status.PodIPs[0].IP) == isIPv4(pod.Status.PodIPs[1].IP) {
  323. framework.Failf("PodIPs should belong to different families, got %v", pod.Status.PodIPs)
  324. }
  325. serverIPs = append(serverIPs, pod.Status.PodIPs[0].IP, pod.Status.PodIPs[1].IP)
  326. }
  327. for _, clientPod := range clientPods.Items {
  328. for _, ip := range serverIPs {
  329. gomega.Consistently(func() error {
  330. ginkgo.By(fmt.Sprintf("checking connectivity from pod %s to serverIP: %s, port: %s", clientPod.Name, ip, port))
  331. cmd := checkNetworkConnectivity(ip, port, timeout)
  332. _, _, err := f.ExecCommandInContainerWithFullOutput(clientPod.Name, containerName, cmd...)
  333. return err
  334. }, duration, pollInterval).ShouldNot(gomega.HaveOccurred())
  335. }
  336. }
  337. }
  338. func checkNetworkConnectivity(ip, port string, timeout int) []string {
  339. curl := fmt.Sprintf("curl -g --connect-timeout %v http://%s", timeout, net.JoinHostPort(ip, port))
  340. cmd := []string{"/bin/sh", "-c", curl}
  341. return cmd
  342. }
  343. // isIPv4 checks if the provided ip belongs to ipv4 family.
  344. // If ip belongs to ipv4 family, return true else it returns false
  345. // TODO (aramase) move this to https://github.com/kubernetes/utils/blob/master/net/net.go
  346. func isIPv4(ip string) bool {
  347. return net.ParseIP(ip).To4() != nil
  348. }
  349. // isIPv4CIDR checks if the provided cidr block belongs to ipv4 family.
  350. // If cidr belongs to ipv4 family, return true else it returns false
  351. // TODO (aramase) move this to https://github.com/kubernetes/utils/blob/master/net/net.go
  352. func isIPv4CIDR(cidr string) bool {
  353. ip, _, err := net.ParseCIDR(cidr)
  354. framework.ExpectNoError(err)
  355. return isIPv4(ip.String())
  356. }
  357. // createService returns a service spec with defined arguments
  358. func createService(name, ns string, labels map[string]string, ipFamily *v1.IPFamily) *v1.Service {
  359. return &v1.Service{
  360. ObjectMeta: metav1.ObjectMeta{
  361. Name: name,
  362. Namespace: ns,
  363. },
  364. Spec: v1.ServiceSpec{
  365. Selector: labels,
  366. Type: v1.ServiceTypeNodePort,
  367. IPFamily: ipFamily,
  368. Ports: []v1.ServicePort{
  369. {
  370. Name: "tcp-port",
  371. Port: 53,
  372. Protocol: v1.ProtocolTCP,
  373. },
  374. {
  375. Name: "udp-port",
  376. Port: 53,
  377. Protocol: v1.ProtocolUDP,
  378. },
  379. },
  380. },
  381. }
  382. }