pod_gc.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 node
  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/uuid"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. imageutils "k8s.io/kubernetes/test/utils/image"
  25. )
  26. // This test requires that --terminated-pod-gc-threshold=100 be set on the controller manager
  27. //
  28. // Slow by design (7 min)
  29. var _ = SIGDescribe("Pod garbage collector [Feature:PodGarbageCollector] [Slow]", func() {
  30. f := framework.NewDefaultFramework("pod-garbage-collector")
  31. ginkgo.It("should handle the creation of 1000 pods", func() {
  32. var count int
  33. for count < 1000 {
  34. pod, err := createTerminatingPod(f)
  35. if err != nil {
  36. framework.Failf("err creating pod: %v", err)
  37. }
  38. pod.ResourceVersion = ""
  39. pod.Status.Phase = v1.PodFailed
  40. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).UpdateStatus(context.TODO(), pod, metav1.UpdateOptions{})
  41. if err != nil {
  42. framework.Failf("err failing pod: %v", err)
  43. }
  44. count++
  45. if count%50 == 0 {
  46. framework.Logf("count: %v", count)
  47. }
  48. }
  49. framework.Logf("created: %v", count)
  50. // The gc controller polls every 30s and fires off a goroutine per
  51. // pod to terminate.
  52. var err error
  53. var pods *v1.PodList
  54. timeout := 2 * time.Minute
  55. gcThreshold := 100
  56. ginkgo.By(fmt.Sprintf("Waiting for gc controller to gc all but %d pods", gcThreshold))
  57. pollErr := wait.Poll(1*time.Minute, timeout, func() (bool, error) {
  58. pods, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{})
  59. if err != nil {
  60. framework.Logf("Failed to list pod %v", err)
  61. return false, nil
  62. }
  63. if len(pods.Items) != gcThreshold {
  64. framework.Logf("Number of observed pods %v, waiting for %v", len(pods.Items), gcThreshold)
  65. return false, nil
  66. }
  67. return true, nil
  68. })
  69. if pollErr != nil {
  70. framework.Failf("Failed to GC pods within %v, %v pods remaining, error: %v", timeout, len(pods.Items), err)
  71. }
  72. })
  73. })
  74. func createTerminatingPod(f *framework.Framework) (*v1.Pod, error) {
  75. uuid := uuid.NewUUID()
  76. pod := &v1.Pod{
  77. ObjectMeta: metav1.ObjectMeta{
  78. Name: string(uuid),
  79. },
  80. Spec: v1.PodSpec{
  81. Containers: []v1.Container{
  82. {
  83. Name: string(uuid),
  84. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  85. },
  86. },
  87. SchedulerName: "please don't schedule my pods",
  88. },
  89. }
  90. return f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  91. }