disk_eviction_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_node
  14. import (
  15. "fmt"
  16. "os/exec"
  17. "strings"
  18. "time"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/kubelet/dockertools"
  21. "k8s.io/kubernetes/pkg/util/uuid"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. . "github.com/onsi/ginkgo"
  24. . "github.com/onsi/gomega"
  25. client "k8s.io/kubernetes/pkg/client/unversioned"
  26. )
  27. const (
  28. // podCheckInterval is the interval seconds between pod status checks.
  29. podCheckInterval = time.Second * 2
  30. dummyFile = "dummy."
  31. )
  32. // TODO: Leverage dynamic Kubelet settings when it's implemented to only modify the kubelet eviction option in this test.
  33. var _ = framework.KubeDescribe("Kubelet Eviction Manager [Serial] [Disruptive]", func() {
  34. f := framework.NewDefaultFramework("kubelet-eviction-manager")
  35. var podClient *framework.PodClient
  36. var c *client.Client
  37. BeforeEach(func() {
  38. podClient = f.PodClient()
  39. c = f.Client
  40. })
  41. Describe("hard eviction test", func() {
  42. Context("pod using the most disk space gets evicted when the node disk usage is above the eviction hard threshold", func() {
  43. var busyPodName, idlePodName, verifyPodName string
  44. var containersToCleanUp map[string]bool
  45. AfterEach(func() {
  46. podClient.Delete(busyPodName, &api.DeleteOptions{})
  47. podClient.Delete(idlePodName, &api.DeleteOptions{})
  48. podClient.Delete(verifyPodName, &api.DeleteOptions{})
  49. for container := range containersToCleanUp {
  50. // TODO: to be container implementation agnostic
  51. cmd := exec.Command("docker", "rm", "-f", strings.Trim(container, dockertools.DockerPrefix))
  52. cmd.Run()
  53. }
  54. })
  55. BeforeEach(func() {
  56. if !isImageSupported() || !evictionOptionIsSet() {
  57. return
  58. }
  59. busyPodName = "to-evict" + string(uuid.NewUUID())
  60. idlePodName = "idle" + string(uuid.NewUUID())
  61. verifyPodName = "verify" + string(uuid.NewUUID())
  62. containersToCleanUp = make(map[string]bool)
  63. createIdlePod(idlePodName, podClient)
  64. podClient.Create(&api.Pod{
  65. ObjectMeta: api.ObjectMeta{
  66. Name: busyPodName,
  67. },
  68. Spec: api.PodSpec{
  69. RestartPolicy: api.RestartPolicyNever,
  70. Containers: []api.Container{
  71. {
  72. Image: ImageRegistry[busyBoxImage],
  73. Name: busyPodName,
  74. // Filling the disk
  75. Command: []string{"sh", "-c",
  76. fmt.Sprintf("for NUM in `seq 1 1 100000`; do dd if=/dev/urandom of=%s.$NUM bs=50000000 count=10; sleep 0.5; done",
  77. dummyFile)},
  78. },
  79. },
  80. },
  81. })
  82. })
  83. It("should evict the pod using the most disk space [Slow]", func() {
  84. if !isImageSupported() {
  85. framework.Logf("test skipped because the image is not supported by the test")
  86. return
  87. }
  88. if !evictionOptionIsSet() {
  89. framework.Logf("test skipped because eviction option is not set")
  90. return
  91. }
  92. evictionOccurred := false
  93. nodeDiskPressureCondition := false
  94. podRescheduleable := false
  95. Eventually(func() error {
  96. // The pod should be evicted.
  97. if !evictionOccurred {
  98. podData, err := podClient.Get(busyPodName)
  99. if err != nil {
  100. return err
  101. }
  102. recordContainerId(containersToCleanUp, podData.Status.ContainerStatuses)
  103. err = verifyPodEviction(podData)
  104. if err != nil {
  105. return err
  106. }
  107. podData, err = podClient.Get(idlePodName)
  108. if err != nil {
  109. return err
  110. }
  111. recordContainerId(containersToCleanUp, podData.Status.ContainerStatuses)
  112. if podData.Status.Phase != api.PodRunning {
  113. err = verifyPodEviction(podData)
  114. if err != nil {
  115. return err
  116. }
  117. }
  118. evictionOccurred = true
  119. return fmt.Errorf("waiting for node disk pressure condition to be set")
  120. }
  121. // The node should have disk pressure condition after the pods are evicted.
  122. if !nodeDiskPressureCondition {
  123. if !nodeHasDiskPressure(f.Client) {
  124. return fmt.Errorf("expected disk pressure condition is not set")
  125. }
  126. nodeDiskPressureCondition = true
  127. return fmt.Errorf("waiting for node disk pressure condition to be cleared")
  128. }
  129. // After eviction happens the pod is evicted so eventually the node disk pressure should be relieved.
  130. if !podRescheduleable {
  131. if nodeHasDiskPressure(f.Client) {
  132. return fmt.Errorf("expected disk pressure condition relief has not happened")
  133. }
  134. createIdlePod(verifyPodName, podClient)
  135. podRescheduleable = true
  136. return fmt.Errorf("waiting for the node to accept a new pod")
  137. }
  138. // The new pod should be able to be scheduled and run after the disk pressure is relieved.
  139. podData, err := podClient.Get(verifyPodName)
  140. if err != nil {
  141. return err
  142. }
  143. recordContainerId(containersToCleanUp, podData.Status.ContainerStatuses)
  144. if podData.Status.Phase != api.PodRunning {
  145. return fmt.Errorf("waiting for the new pod to be running")
  146. }
  147. return nil
  148. }, time.Minute*15 /* based on n1-standard-1 machine type */, podCheckInterval).Should(BeNil())
  149. })
  150. })
  151. })
  152. })
  153. func createIdlePod(podName string, podClient *framework.PodClient) {
  154. podClient.Create(&api.Pod{
  155. ObjectMeta: api.ObjectMeta{
  156. Name: podName,
  157. },
  158. Spec: api.PodSpec{
  159. RestartPolicy: api.RestartPolicyNever,
  160. Containers: []api.Container{
  161. {
  162. Image: ImageRegistry[pauseImage],
  163. Name: podName,
  164. },
  165. },
  166. },
  167. })
  168. }
  169. func verifyPodEviction(podData *api.Pod) error {
  170. if podData.Status.Phase != api.PodFailed {
  171. return fmt.Errorf("expected phase to be failed. got %+v", podData.Status.Phase)
  172. }
  173. if podData.Status.Reason != "Evicted" {
  174. return fmt.Errorf("expected failed reason to be evicted. got %+v", podData.Status.Reason)
  175. }
  176. return nil
  177. }
  178. func nodeHasDiskPressure(c *client.Client) bool {
  179. nodeList := framework.GetReadySchedulableNodesOrDie(c)
  180. for _, condition := range nodeList.Items[0].Status.Conditions {
  181. if condition.Type == api.NodeDiskPressure {
  182. return condition.Status == api.ConditionTrue
  183. }
  184. }
  185. return false
  186. }
  187. func recordContainerId(containersToCleanUp map[string]bool, containerStatuses []api.ContainerStatus) {
  188. for _, status := range containerStatuses {
  189. containersToCleanUp[status.ContainerID] = true
  190. }
  191. }
  192. func evictionOptionIsSet() bool {
  193. return len(framework.TestContext.EvictionHard) > 0
  194. }
  195. func isImageSupported() bool {
  196. // TODO: Only images with image fs is selected for testing for now. When the kubelet settings can be dynamically updated,
  197. // instead of skipping images the eviction thresholds should be adjusted based on the images.
  198. return strings.Contains(framework.TestContext.NodeName, "-gci-dev-")
  199. }