system_node_critical_test.go 4.7 KB

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