ubernetes_lite.go 7.9 KB

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