ubernetes_lite.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright 2015 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 scheduling
  14. import (
  15. "context"
  16. "fmt"
  17. "math"
  18. "github.com/onsi/ginkgo"
  19. "github.com/onsi/gomega"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/labels"
  23. "k8s.io/apimachinery/pkg/util/intstr"
  24. "k8s.io/apimachinery/pkg/util/uuid"
  25. clientset "k8s.io/client-go/kubernetes"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  28. e2erc "k8s.io/kubernetes/test/e2e/framework/rc"
  29. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  30. testutils "k8s.io/kubernetes/test/utils"
  31. imageutils "k8s.io/kubernetes/test/utils/image"
  32. )
  33. var _ = SIGDescribe("Multi-AZ Clusters", func() {
  34. f := framework.NewDefaultFramework("multi-az")
  35. var zoneCount int
  36. var err error
  37. image := framework.ServeHostnameImage
  38. ginkgo.BeforeEach(func() {
  39. e2eskipper.SkipUnlessProviderIs("gce", "gke", "aws")
  40. if zoneCount <= 0 {
  41. zoneCount, err = getZoneCount(f.ClientSet)
  42. framework.ExpectNoError(err)
  43. }
  44. ginkgo.By(fmt.Sprintf("Checking for multi-zone cluster. Zone count = %d", zoneCount))
  45. msg := fmt.Sprintf("Zone count is %d, only run for multi-zone clusters, skipping test", zoneCount)
  46. e2eskipper.SkipUnlessAtLeast(zoneCount, 2, msg)
  47. // TODO: SkipUnlessDefaultScheduler() // Non-default schedulers might not spread
  48. })
  49. ginkgo.It("should spread the pods of a service across zones", func() {
  50. SpreadServiceOrFail(f, (2*zoneCount)+1, image)
  51. })
  52. ginkgo.It("should spread the pods of a replication controller across zones", func() {
  53. SpreadRCOrFail(f, int32((2*zoneCount)+1), image, []string{"serve-hostname"})
  54. })
  55. })
  56. // SpreadServiceOrFail check that the pods comprising a service
  57. // get spread evenly across available zones
  58. func SpreadServiceOrFail(f *framework.Framework, replicaCount int, image string) {
  59. // First create the service
  60. serviceName := "test-service"
  61. serviceSpec := &v1.Service{
  62. ObjectMeta: metav1.ObjectMeta{
  63. Name: serviceName,
  64. Namespace: f.Namespace.Name,
  65. },
  66. Spec: v1.ServiceSpec{
  67. Selector: map[string]string{
  68. "service": serviceName,
  69. },
  70. Ports: []v1.ServicePort{{
  71. Port: 80,
  72. TargetPort: intstr.FromInt(80),
  73. }},
  74. },
  75. }
  76. _, err := f.ClientSet.CoreV1().Services(f.Namespace.Name).Create(context.TODO(), serviceSpec, metav1.CreateOptions{})
  77. framework.ExpectNoError(err)
  78. // Now create some pods behind the service
  79. podSpec := &v1.Pod{
  80. ObjectMeta: metav1.ObjectMeta{
  81. Name: serviceName,
  82. Labels: map[string]string{"service": serviceName},
  83. },
  84. Spec: v1.PodSpec{
  85. Containers: []v1.Container{
  86. {
  87. Name: "test",
  88. Image: imageutils.GetPauseImageName(),
  89. },
  90. },
  91. },
  92. }
  93. // Caution: StartPods requires at least one pod to replicate.
  94. // Based on the callers, replicas is always positive number: zoneCount >= 0 implies (2*zoneCount)+1 > 0.
  95. // Thus, no need to test for it. Once the precondition changes to zero number of replicas,
  96. // test for replicaCount > 0. Otherwise, StartPods panics.
  97. framework.ExpectNoError(testutils.StartPods(f.ClientSet, replicaCount, f.Namespace.Name, serviceName, *podSpec, false, framework.Logf))
  98. // Wait for all of them to be scheduled
  99. selector := labels.SelectorFromSet(labels.Set(map[string]string{"service": serviceName}))
  100. pods, err := e2epod.WaitForPodsWithLabelScheduled(f.ClientSet, f.Namespace.Name, selector)
  101. framework.ExpectNoError(err)
  102. // Now make sure they're spread across zones
  103. zoneNames, err := framework.GetClusterZones(f.ClientSet)
  104. framework.ExpectNoError(err)
  105. checkZoneSpreading(f.ClientSet, pods, zoneNames.List())
  106. }
  107. // Find the name of the zone in which a Node is running
  108. func getZoneNameForNode(node v1.Node) (string, error) {
  109. for key, value := range node.Labels {
  110. if key == v1.LabelZoneFailureDomain {
  111. return value, nil
  112. }
  113. }
  114. return "", fmt.Errorf("Zone name for node %s not found. No label with key %s",
  115. node.Name, v1.LabelZoneFailureDomain)
  116. }
  117. // Return the number of zones in which we have nodes in this cluster.
  118. func getZoneCount(c clientset.Interface) (int, error) {
  119. zoneNames, err := framework.GetClusterZones(c)
  120. if err != nil {
  121. return -1, err
  122. }
  123. return len(zoneNames), nil
  124. }
  125. // Find the name of the zone in which the pod is scheduled
  126. func getZoneNameForPod(c clientset.Interface, pod v1.Pod) (string, error) {
  127. ginkgo.By(fmt.Sprintf("Getting zone name for pod %s, on node %s", pod.Name, pod.Spec.NodeName))
  128. node, err := c.CoreV1().Nodes().Get(context.TODO(), pod.Spec.NodeName, metav1.GetOptions{})
  129. framework.ExpectNoError(err)
  130. return getZoneNameForNode(*node)
  131. }
  132. // Determine whether a set of pods are approximately evenly spread
  133. // across a given set of zones
  134. func checkZoneSpreading(c clientset.Interface, pods *v1.PodList, zoneNames []string) {
  135. podsPerZone := make(map[string]int)
  136. for _, zoneName := range zoneNames {
  137. podsPerZone[zoneName] = 0
  138. }
  139. for _, pod := range pods.Items {
  140. if pod.DeletionTimestamp != nil {
  141. continue
  142. }
  143. zoneName, err := getZoneNameForPod(c, pod)
  144. framework.ExpectNoError(err)
  145. podsPerZone[zoneName] = podsPerZone[zoneName] + 1
  146. }
  147. minPodsPerZone := math.MaxInt32
  148. maxPodsPerZone := 0
  149. for _, podCount := range podsPerZone {
  150. if podCount < minPodsPerZone {
  151. minPodsPerZone = podCount
  152. }
  153. if podCount > maxPodsPerZone {
  154. maxPodsPerZone = podCount
  155. }
  156. }
  157. gomega.Expect(minPodsPerZone).To(gomega.BeNumerically("~", maxPodsPerZone, 1),
  158. "Pods were not evenly spread across zones. %d in one zone and %d in another zone",
  159. minPodsPerZone, maxPodsPerZone)
  160. }
  161. // SpreadRCOrFail Check that the pods comprising a replication
  162. // controller get spread evenly across available zones
  163. func SpreadRCOrFail(f *framework.Framework, replicaCount int32, image string, args []string) {
  164. name := "ubelite-spread-rc-" + string(uuid.NewUUID())
  165. ginkgo.By(fmt.Sprintf("Creating replication controller %s", name))
  166. controller, err := f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Create(context.TODO(), &v1.ReplicationController{
  167. ObjectMeta: metav1.ObjectMeta{
  168. Namespace: f.Namespace.Name,
  169. Name: name,
  170. },
  171. Spec: v1.ReplicationControllerSpec{
  172. Replicas: &replicaCount,
  173. Selector: map[string]string{
  174. "name": name,
  175. },
  176. Template: &v1.PodTemplateSpec{
  177. ObjectMeta: metav1.ObjectMeta{
  178. Labels: map[string]string{"name": name},
  179. },
  180. Spec: v1.PodSpec{
  181. Containers: []v1.Container{
  182. {
  183. Name: name,
  184. Image: image,
  185. Args: args,
  186. Ports: []v1.ContainerPort{{ContainerPort: 9376}},
  187. },
  188. },
  189. },
  190. },
  191. },
  192. }, metav1.CreateOptions{})
  193. framework.ExpectNoError(err)
  194. // Cleanup the replication controller when we are done.
  195. defer func() {
  196. // Resize the replication controller to zero to get rid of pods.
  197. if err := e2erc.DeleteRCAndWaitForGC(f.ClientSet, f.Namespace.Name, controller.Name); err != nil {
  198. framework.Logf("Failed to cleanup replication controller %v: %v.", controller.Name, err)
  199. }
  200. }()
  201. // List the pods, making sure we observe all the replicas.
  202. selector := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
  203. _, err = e2epod.PodsCreated(f.ClientSet, f.Namespace.Name, name, replicaCount)
  204. framework.ExpectNoError(err)
  205. // Wait for all of them to be scheduled
  206. ginkgo.By(fmt.Sprintf("Waiting for %d replicas of %s to be scheduled. Selector: %v", replicaCount, name, selector))
  207. pods, err := e2epod.WaitForPodsWithLabelScheduled(f.ClientSet, f.Namespace.Name, selector)
  208. framework.ExpectNoError(err)
  209. // Now make sure they're spread across zones
  210. zoneNames, err := framework.GetClusterZones(f.ClientSet)
  211. framework.ExpectNoError(err)
  212. checkZoneSpreading(f.ClientSet, pods, zoneNames.List())
  213. }