pod_gc.go 2.8 KB

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