networking_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 kubeadm
  14. import (
  15. "context"
  16. "net"
  17. "strings"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  21. "github.com/onsi/ginkgo"
  22. )
  23. var (
  24. dualStack bool
  25. podSubnetInKubeadmConfig bool
  26. serviceSubnetInKubeadmConfig bool
  27. )
  28. // Define container for all the test specification aimed at verifying
  29. // that kubeadm configures the networking as expected.
  30. // in case you want to skip this test use SKIP=setup-networking
  31. var _ = Describe("networking [setup-networking]", func() {
  32. // Get an instance of the k8s test framework
  33. f := framework.NewDefaultFramework("networking")
  34. // Tests in this container are not expected to create new objects in the cluster
  35. // so we are disabling the creation of a namespace in order to get a faster execution
  36. f.SkipNamespaceCreation = true
  37. // Tests are either single-stack (either IPv4 or IPv6 address) or dual-stack
  38. // We can determine this from the kubeadm config by looking at the number of
  39. // IPs specified in networking.podSubnet
  40. ginkgo.BeforeEach(func() {
  41. // gets the ClusterConfiguration from the kubeadm kubeadm-config ConfigMap as a untyped map
  42. cc := getClusterConfiguration(f.ClientSet)
  43. // Extract the networking.podSubnet
  44. // Please note that this test assumes that the user does not alter network configs
  45. // using the extraArgs option.
  46. if _, ok := cc["networking"]; ok {
  47. netCC := cc["networking"].(map[interface{}]interface{})
  48. if ps, ok := netCC["podSubnet"]; ok {
  49. // If podSubnet is not specified, podSubnet cases will be skipped.
  50. // Note that kubeadm does not currently apply defaults for PodSubnet, so we skip.
  51. podSubnetInKubeadmConfig = true
  52. cidrs := strings.Split(ps.(string), ",")
  53. if len(cidrs) > 1 {
  54. dualStack = true
  55. }
  56. }
  57. if _, ok := netCC["serviceSubnet"]; ok {
  58. // If serviceSubnet is not specified, serviceSubnet cases will be skipped.
  59. serviceSubnetInKubeadmConfig = true
  60. }
  61. }
  62. })
  63. ginkgo.Context("single-stack", func() {
  64. ginkgo.Context("podSubnet", func() {
  65. ginkgo.It("should be properly configured if specified in kubeadm-config", func() {
  66. if dualStack {
  67. e2eskipper.Skipf("Skipping because cluster is dual-stack")
  68. }
  69. if !podSubnetInKubeadmConfig {
  70. e2eskipper.Skipf("Skipping because podSubnet was not specified in kubeadm-config")
  71. }
  72. cc := getClusterConfiguration(f.ClientSet)
  73. if _, ok := cc["networking"]; ok {
  74. netCC := cc["networking"].(map[interface{}]interface{})
  75. if ps, ok := netCC["podSubnet"]; ok {
  76. // Check that the pod CIDR allocated to the node(s) is within the kubeadm-config podCIDR.
  77. nodes, err := f.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
  78. framework.ExpectNoError(err, "error listing nodes")
  79. for _, node := range nodes.Items {
  80. if !subnetWithinSubnet(ps.(string), node.Spec.PodCIDR) {
  81. framework.Failf("failed due to node(%v) IP %v not inside configured pod subnet: %s", node.Name, node.Spec.PodCIDR, ps)
  82. }
  83. }
  84. }
  85. }
  86. })
  87. })
  88. ginkgo.Context("serviceSubnet", func() {
  89. ginkgo.It("should be properly configured if specified in kubeadm-config", func() {
  90. if dualStack {
  91. e2eskipper.Skipf("Skipping because cluster is dual-stack")
  92. }
  93. if !serviceSubnetInKubeadmConfig {
  94. e2eskipper.Skipf("Skipping because serviceSubnet was not specified in kubeadm-config")
  95. }
  96. cc := getClusterConfiguration(f.ClientSet)
  97. if _, ok := cc["networking"]; ok {
  98. netCC := cc["networking"].(map[interface{}]interface{})
  99. if ss, ok := netCC["serviceSubnet"]; ok {
  100. // Get the kubernetes service in the default namespace.
  101. // Check that service CIDR allocated is within the serviceSubnet range.
  102. svc, err := f.ClientSet.CoreV1().Services("default").Get(context.TODO(), "kubernetes", metav1.GetOptions{})
  103. framework.ExpectNoError(err, "error getting Service %q from namespace %q", "kubernetes", "default")
  104. if !ipWithinSubnet(ss.(string), svc.Spec.ClusterIP) {
  105. framework.Failf("failed due to service(%v) cluster-IP %v not inside configured service subnet: %s", svc.Name, svc.Spec.ClusterIP, ss)
  106. }
  107. }
  108. }
  109. })
  110. })
  111. })
  112. ginkgo.Context("dual-stack [Feature:IPv6DualStack]", func() {
  113. ginkgo.Context("podSubnet", func() {
  114. ginkgo.It("should be properly configured if specified in kubeadm-config", func() {
  115. if !dualStack {
  116. e2eskipper.Skipf("Skipping because cluster is not dual-stack")
  117. }
  118. if !podSubnetInKubeadmConfig {
  119. e2eskipper.Skipf("Skipping because podSubnet was not specified in kubeadm-config")
  120. }
  121. cc := getClusterConfiguration(f.ClientSet)
  122. if _, ok := cc["networking"]; ok {
  123. netCC := cc["networking"].(map[interface{}]interface{})
  124. if ps, ok := netCC["podSubnet"]; ok {
  125. nodes, err := f.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
  126. framework.ExpectNoError(err, "error listing nodes")
  127. // Check that the pod CIDRs allocated to the node(s) are within the kubeadm-config podCIDR.
  128. var found bool
  129. configCIDRs := strings.Split(ps.(string), ",")
  130. for _, node := range nodes.Items {
  131. for _, nCIDR := range node.Spec.PodCIDRs {
  132. found = false
  133. for _, cCIDR := range configCIDRs {
  134. if subnetWithinSubnet(cCIDR, nCIDR) {
  135. found = true
  136. break
  137. }
  138. }
  139. if !found {
  140. framework.Failf("failed due to the PodCIDRs (%v) of Node %q not being inside the configuration podSubnet CIDR %q", node.Spec.PodCIDRs, node.Name, configCIDRs)
  141. }
  142. }
  143. }
  144. }
  145. }
  146. })
  147. })
  148. })
  149. })
  150. // ipWithinSubnet returns true if an IP (targetIP) falls within the reference subnet (refIPNet)
  151. func ipWithinSubnet(refIPNet, targetIP string) bool {
  152. _, rNet, _ := net.ParseCIDR(refIPNet)
  153. tIP := net.ParseIP(targetIP)
  154. return rNet.Contains(tIP)
  155. }
  156. // subnetWithinSubnet returns true if a subnet (targetNet) falls within the reference subnet (refIPNet)
  157. func subnetWithinSubnet(refIPNet, targetNet string) bool {
  158. _, rNet, _ := net.ParseCIDR(refIPNet)
  159. tNet, _, _ := net.ParseCIDR(targetNet)
  160. return rNet.Contains(tNet)
  161. }