system_node_critical_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright 2019 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 e2enode
  14. import (
  15. "fmt"
  16. "os"
  17. "time"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. "k8s.io/apimachinery/pkg/util/uuid"
  21. kubeapi "k8s.io/kubernetes/pkg/apis/core"
  22. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  23. evictionapi "k8s.io/kubernetes/pkg/kubelet/eviction/api"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. "github.com/onsi/ginkgo"
  26. "github.com/onsi/gomega"
  27. )
  28. var _ = framework.KubeDescribe("SystemNodeCriticalPod [Slow] [Serial] [Disruptive] [NodeFeature:SystemNodeCriticalPod]", func() {
  29. f := framework.NewDefaultFramework("system-node-critical-pod-test")
  30. // this test only manipulates pods in kube-system
  31. f.SkipNamespaceCreation = true
  32. ginkgo.Context("when create a system-node-critical pod", func() {
  33. tempSetCurrentKubeletConfig(f, func(initialConfig *kubeletconfig.KubeletConfiguration) {
  34. diskConsumed := resource.MustParse("200Mi")
  35. summary := eventuallyGetSummary()
  36. availableBytes := *(summary.Node.Fs.AvailableBytes)
  37. initialConfig.EvictionHard = map[string]string{string(evictionapi.SignalNodeFsAvailable): fmt.Sprintf("%d", availableBytes-uint64(diskConsumed.Value()))}
  38. initialConfig.EvictionMinimumReclaim = map[string]string{}
  39. })
  40. // Place the remainder of the test within a context so that the kubelet config is set before and after the test.
  41. ginkgo.Context("", func() {
  42. var staticPodName, mirrorPodName, podPath string
  43. ns := kubeapi.NamespaceSystem
  44. ginkgo.BeforeEach(func() {
  45. ginkgo.By("create a static system-node-critical pod")
  46. staticPodName = "static-disk-hog-" + string(uuid.NewUUID())
  47. mirrorPodName = staticPodName + "-" + framework.TestContext.NodeName
  48. podPath = framework.TestContext.KubeletConfig.StaticPodPath
  49. // define a static pod consuming disk gradually
  50. // the upper limit is 1024 (iterations) * 10485760 bytes (10MB) = 10GB
  51. err := createStaticSystemNodeCriticalPod(
  52. podPath, staticPodName, ns, busyboxImage, v1.RestartPolicyNever, 1024,
  53. "dd if=/dev/urandom of=file${i} bs=10485760 count=1 2>/dev/null; sleep .1;",
  54. )
  55. gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
  56. ginkgo.By("wait for the mirror pod to be running")
  57. gomega.Eventually(func() error {
  58. return checkMirrorPodRunning(f.ClientSet, mirrorPodName, ns)
  59. }, time.Minute, time.Second*2).Should(gomega.BeNil())
  60. })
  61. ginkgo.It("should not be evicted upon DiskPressure", func() {
  62. ginkgo.By("wait for the node to have DiskPressure condition")
  63. gomega.Eventually(func() error {
  64. if hasNodeCondition(f, v1.NodeDiskPressure) {
  65. return nil
  66. }
  67. msg := fmt.Sprintf("NodeCondition: %s not encountered yet", v1.NodeDiskPressure)
  68. framework.Logf(msg)
  69. return fmt.Errorf(msg)
  70. }, time.Minute*2, time.Second*4).Should(gomega.BeNil())
  71. ginkgo.By("check if it's running all the time")
  72. gomega.Consistently(func() error {
  73. err := checkMirrorPodRunning(f.ClientSet, mirrorPodName, ns)
  74. if err == nil {
  75. framework.Logf("mirror pod %q is running", mirrorPodName)
  76. } else {
  77. framework.Logf(err.Error())
  78. }
  79. return err
  80. }, time.Minute*8, time.Second*4).ShouldNot(gomega.HaveOccurred())
  81. })
  82. ginkgo.AfterEach(func() {
  83. defer func() {
  84. if framework.TestContext.PrepullImages {
  85. // The test may cause the prepulled images to be evicted,
  86. // prepull those images again to ensure this test not affect following tests.
  87. PrePullAllImages()
  88. }
  89. }()
  90. ginkgo.By("delete the static pod")
  91. err := deleteStaticPod(podPath, staticPodName, ns)
  92. gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
  93. ginkgo.By("wait for the mirror pod to disappear")
  94. gomega.Eventually(func() error {
  95. return checkMirrorPodDisappear(f.ClientSet, mirrorPodName, ns)
  96. }, time.Minute, time.Second*2).Should(gomega.BeNil())
  97. ginkgo.By("making sure that node no longer has DiskPressure")
  98. gomega.Eventually(func() error {
  99. if hasNodeCondition(f, v1.NodeDiskPressure) {
  100. return fmt.Errorf("Conditions havent returned to normal, node still has DiskPressure")
  101. }
  102. return nil
  103. }, pressureDissapearTimeout, evictionPollInterval).Should(gomega.BeNil())
  104. })
  105. })
  106. })
  107. })
  108. func createStaticSystemNodeCriticalPod(dir, name, namespace, image string, restart v1.RestartPolicy,
  109. iterations int, command string) error {
  110. template := `
  111. apiVersion: v1
  112. kind: Pod
  113. metadata:
  114. name: %s
  115. namespace: %s
  116. spec:
  117. priorityClassName: system-node-critical
  118. containers:
  119. - name: %s
  120. image: %s
  121. restartPolicy: %s
  122. command: ["sh", "-c", "i=0; while [ $i -lt %d ]; do %s i=$(($i+1)); done; while true; do sleep 5; done"]
  123. `
  124. file := staticPodPath(dir, name, namespace)
  125. podYaml := fmt.Sprintf(template, name, namespace, name, image, string(restart), iterations, command)
  126. f, err := os.OpenFile(file, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
  127. if err != nil {
  128. return err
  129. }
  130. defer f.Close()
  131. _, err = f.WriteString(podYaml)
  132. return err
  133. }