hybrid_network.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2018 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 windows
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/uuid"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  21. imageutils "k8s.io/kubernetes/test/utils/image"
  22. "github.com/onsi/ginkgo"
  23. "github.com/onsi/gomega"
  24. )
  25. const (
  26. linuxOS = "linux"
  27. windowsOS = "windows"
  28. )
  29. var (
  30. windowsBusyBoximage = imageutils.GetE2EImage(imageutils.Agnhost)
  31. linuxBusyBoxImage = "docker.io/library/nginx:1.15-alpine"
  32. )
  33. var _ = SIGDescribe("Hybrid cluster network", func() {
  34. f := framework.NewDefaultFramework("hybrid-network")
  35. ginkgo.BeforeEach(func() {
  36. e2eskipper.SkipUnlessNodeOSDistroIs("windows")
  37. })
  38. ginkgo.Context("for all supported CNIs", func() {
  39. ginkgo.It("should have stable networking for Linux and Windows pods", func() {
  40. ginkgo.By("creating linux and windows pods")
  41. linuxPod := createTestPod(f, linuxBusyBoxImage, linuxOS)
  42. windowsPod := createTestPod(f, windowsBusyBoximage, windowsOS)
  43. windowsPod.Spec.Containers[0].Args = []string{"test-webserver"}
  44. ginkgo.By("checking connectivity to 8.8.8.8 53 (google.com) from Linux")
  45. assertConsistentConnectivity(f, linuxPod.ObjectMeta.Name, linuxOS, linuxCheck("8.8.8.8", 53))
  46. ginkgo.By("checking connectivity to www.google.com from Windows")
  47. assertConsistentConnectivity(f, windowsPod.ObjectMeta.Name, windowsOS, windowsCheck("www.google.com"))
  48. ginkgo.By("checking connectivity from Linux to Windows")
  49. assertConsistentConnectivity(f, linuxPod.ObjectMeta.Name, linuxOS, linuxCheck(windowsPod.Status.PodIP, 80))
  50. ginkgo.By("checking connectivity from Windows to Linux")
  51. assertConsistentConnectivity(f, windowsPod.ObjectMeta.Name, windowsOS, windowsCheck(linuxPod.Status.PodIP))
  52. })
  53. })
  54. })
  55. var (
  56. duration = "10s"
  57. pollInterval = "1s"
  58. timeout = 10 // seconds
  59. )
  60. func assertConsistentConnectivity(f *framework.Framework, podName string, os string, cmd []string) {
  61. gomega.Consistently(func() error {
  62. ginkgo.By(fmt.Sprintf("checking connectivity of %s-container in %s", os, podName))
  63. _, _, err := f.ExecCommandInContainerWithFullOutput(podName, os+"-container", cmd...)
  64. return err
  65. }, duration, pollInterval).ShouldNot(gomega.HaveOccurred())
  66. }
  67. func linuxCheck(address string, port int) []string {
  68. nc := fmt.Sprintf("nc -vz %s %v -w %v", address, port, timeout)
  69. cmd := []string{"/bin/sh", "-c", nc}
  70. return cmd
  71. }
  72. func windowsCheck(address string) []string {
  73. curl := fmt.Sprintf("curl.exe %s --connect-timeout %v --fail", address, timeout)
  74. cmd := []string{"cmd", "/c", curl}
  75. return cmd
  76. }
  77. func createTestPod(f *framework.Framework, image string, os string) *v1.Pod {
  78. containerName := fmt.Sprintf("%s-container", os)
  79. podName := "pod-" + string(uuid.NewUUID())
  80. pod := &v1.Pod{
  81. TypeMeta: metav1.TypeMeta{
  82. Kind: "Pod",
  83. APIVersion: "v1",
  84. },
  85. ObjectMeta: metav1.ObjectMeta{
  86. Name: podName,
  87. },
  88. Spec: v1.PodSpec{
  89. Containers: []v1.Container{
  90. {
  91. Name: containerName,
  92. Image: image,
  93. Ports: []v1.ContainerPort{{ContainerPort: 80}},
  94. },
  95. },
  96. NodeSelector: map[string]string{
  97. "kubernetes.io/os": os,
  98. },
  99. },
  100. }
  101. if os == linuxOS {
  102. pod.Spec.Tolerations = []v1.Toleration{
  103. {
  104. Operator: v1.TolerationOpExists,
  105. Effect: v1.TaintEffectNoSchedule,
  106. },
  107. }
  108. }
  109. return f.PodClient().CreateSync(pod)
  110. }