rescheduler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2016 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. "time"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/labels"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. . "github.com/onsi/ginkgo"
  21. . "github.com/onsi/gomega"
  22. )
  23. // This test requires Rescheduler to be enabled.
  24. var _ = framework.KubeDescribe("Rescheduler [Serial]", func() {
  25. f := framework.NewDefaultFramework("rescheduler")
  26. var ns string
  27. var totalMillicores int
  28. BeforeEach(func() {
  29. framework.SkipUnlessProviderIs("gce")
  30. ns = f.Namespace.Name
  31. nodes := framework.GetReadySchedulableNodesOrDie(f.Client)
  32. nodeCount := len(nodes.Items)
  33. Expect(nodeCount).NotTo(BeZero())
  34. cpu := nodes.Items[0].Status.Capacity[api.ResourceCPU]
  35. totalMillicores = int((&cpu).MilliValue()) * nodeCount
  36. })
  37. It("should ensure that critical pod is scheduled in case there is no resources available", func() {
  38. By("reserving all available cpu")
  39. err := reserveAllCpu(f, "reserve-all-cpu", totalMillicores)
  40. defer framework.DeleteRCAndPods(f.Client, ns, "reserve-all-cpu")
  41. framework.ExpectNoError(err)
  42. By("creating a new instance of DNS and waiting for DNS to be scheduled")
  43. label := labels.SelectorFromSet(labels.Set(map[string]string{"k8s-app": "kube-dns"}))
  44. listOpts := api.ListOptions{LabelSelector: label}
  45. rcs, err := f.Client.ReplicationControllers(api.NamespaceSystem).List(listOpts)
  46. framework.ExpectNoError(err)
  47. Expect(len(rcs.Items)).Should(Equal(1))
  48. rc := rcs.Items[0]
  49. replicas := uint(rc.Spec.Replicas)
  50. err = framework.ScaleRC(f.Client, api.NamespaceSystem, rc.Name, replicas+1, true)
  51. defer framework.ExpectNoError(framework.ScaleRC(f.Client, api.NamespaceSystem, rc.Name, replicas, true))
  52. framework.ExpectNoError(err)
  53. })
  54. })
  55. func reserveAllCpu(f *framework.Framework, id string, millicores int) error {
  56. timeout := 5 * time.Minute
  57. replicas := millicores / 100
  58. ReserveCpu(f, id, 1, 100)
  59. framework.ExpectNoError(framework.ScaleRC(f.Client, f.Namespace.Name, id, uint(replicas), false))
  60. for start := time.Now(); time.Since(start) < timeout; time.Sleep(10 * time.Second) {
  61. pods, err := framework.GetPodsInNamespace(f.Client, f.Namespace.Name, framework.ImagePullerLabels)
  62. if err != nil {
  63. return err
  64. }
  65. if len(pods) != replicas {
  66. continue
  67. }
  68. allRunningOrUnschedulable := true
  69. for _, pod := range pods {
  70. if !podRunningOrUnschedulable(pod) {
  71. allRunningOrUnschedulable = false
  72. break
  73. }
  74. }
  75. if allRunningOrUnschedulable {
  76. return nil
  77. }
  78. }
  79. return fmt.Errorf("Pod name %s: Gave up waiting %v for %d pods to come up", id, timeout, replicas)
  80. }
  81. func podRunningOrUnschedulable(pod *api.Pod) bool {
  82. _, cond := api.GetPodCondition(&pod.Status, api.PodScheduled)
  83. if cond != nil && cond.Status == api.ConditionFalse && cond.Reason == "Unschedulable" {
  84. return true
  85. }
  86. running, _ := framework.PodRunningReady(pod)
  87. return running
  88. }