framework.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2017 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. "time"
  18. "github.com/onsi/ginkgo"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/sets"
  22. clientset "k8s.io/client-go/kubernetes"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. )
  25. var (
  26. timeout = 10 * time.Minute
  27. waitTime = 2 * time.Second
  28. )
  29. // SIGDescribe annotates the test with the SIG label.
  30. func SIGDescribe(text string, body func()) bool {
  31. return ginkgo.Describe("[sig-scheduling] "+text, body)
  32. }
  33. // WaitForStableCluster waits until all existing pods are scheduled and returns their amount.
  34. func WaitForStableCluster(c clientset.Interface, masterNodes sets.String) int {
  35. startTime := time.Now()
  36. // Wait for all pods to be scheduled.
  37. allScheduledPods, allNotScheduledPods := getScheduledAndUnscheduledPods(c, masterNodes, metav1.NamespaceAll)
  38. for len(allNotScheduledPods) != 0 {
  39. time.Sleep(waitTime)
  40. if startTime.Add(timeout).Before(time.Now()) {
  41. framework.Logf("Timed out waiting for the following pods to schedule")
  42. for _, p := range allNotScheduledPods {
  43. framework.Logf("%v/%v", p.Namespace, p.Name)
  44. }
  45. framework.Failf("Timed out after %v waiting for stable cluster.", timeout)
  46. break
  47. }
  48. allScheduledPods, allNotScheduledPods = getScheduledAndUnscheduledPods(c, masterNodes, metav1.NamespaceAll)
  49. }
  50. return len(allScheduledPods)
  51. }
  52. // WaitForPodsToBeDeleted waits until pods that are terminating to get deleted.
  53. func WaitForPodsToBeDeleted(c clientset.Interface) {
  54. startTime := time.Now()
  55. deleting := getDeletingPods(c, metav1.NamespaceAll)
  56. for len(deleting) != 0 {
  57. if startTime.Add(timeout).Before(time.Now()) {
  58. framework.Logf("Pods still not deleted")
  59. for _, p := range deleting {
  60. framework.Logf("%v/%v", p.Namespace, p.Name)
  61. }
  62. framework.Failf("Timed out after %v waiting for pods to be deleted", timeout)
  63. break
  64. }
  65. time.Sleep(waitTime)
  66. deleting = getDeletingPods(c, metav1.NamespaceAll)
  67. }
  68. }
  69. // getScheduledAndUnscheduledPods lists scheduled and not scheduled pods in the given namespace, with succeeded and failed pods filtered out.
  70. func getScheduledAndUnscheduledPods(c clientset.Interface, masterNodes sets.String, ns string) (scheduledPods, notScheduledPods []v1.Pod) {
  71. pods, err := c.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{})
  72. framework.ExpectNoError(err, fmt.Sprintf("listing all pods in namespace %q while waiting for stable cluster", ns))
  73. // API server returns also Pods that succeeded. We need to filter them out.
  74. filteredPods := make([]v1.Pod, 0, len(pods.Items))
  75. for _, p := range pods.Items {
  76. if !podTerminated(p) {
  77. filteredPods = append(filteredPods, p)
  78. }
  79. }
  80. pods.Items = filteredPods
  81. return GetPodsScheduled(masterNodes, pods)
  82. }
  83. // getDeletingPods returns whether there are any pods marked for deletion.
  84. func getDeletingPods(c clientset.Interface, ns string) []v1.Pod {
  85. pods, err := c.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{})
  86. framework.ExpectNoError(err, fmt.Sprintf("listing all pods in namespace %q while waiting for pods to terminate", ns))
  87. var deleting []v1.Pod
  88. for _, p := range pods.Items {
  89. if p.ObjectMeta.DeletionTimestamp != nil && !podTerminated(p) {
  90. deleting = append(deleting, p)
  91. }
  92. }
  93. return deleting
  94. }
  95. func podTerminated(p v1.Pod) bool {
  96. return p.Status.Phase == v1.PodSucceeded || p.Status.Phase == v1.PodFailed
  97. }