container_manager_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // +build linux
  2. /*
  3. Copyright 2016 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package e2e_node
  15. import (
  16. "fmt"
  17. "os/exec"
  18. "path"
  19. "strconv"
  20. "strings"
  21. "time"
  22. v1 "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/api/resource"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/util/sets"
  26. "k8s.io/apimachinery/pkg/util/uuid"
  27. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  28. "k8s.io/kubernetes/test/e2e/framework"
  29. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  30. . "github.com/onsi/ginkgo"
  31. . "github.com/onsi/gomega"
  32. imageutils "k8s.io/kubernetes/test/utils/image"
  33. )
  34. func getOOMScoreForPid(pid int) (int, error) {
  35. procfsPath := path.Join("/proc", strconv.Itoa(pid), "oom_score_adj")
  36. out, err := exec.Command("sudo", "cat", procfsPath).CombinedOutput()
  37. if err != nil {
  38. return 0, err
  39. }
  40. return strconv.Atoi(strings.TrimSpace(string(out)))
  41. }
  42. func validateOOMScoreAdjSetting(pid int, expectedOOMScoreAdj int) error {
  43. oomScore, err := getOOMScoreForPid(pid)
  44. if err != nil {
  45. return fmt.Errorf("failed to get oom_score_adj for %d: %v", pid, err)
  46. }
  47. if expectedOOMScoreAdj != oomScore {
  48. return fmt.Errorf("expected pid %d's oom_score_adj to be %d; found %d", pid, expectedOOMScoreAdj, oomScore)
  49. }
  50. return nil
  51. }
  52. func validateOOMScoreAdjSettingIsInRange(pid int, expectedMinOOMScoreAdj, expectedMaxOOMScoreAdj int) error {
  53. oomScore, err := getOOMScoreForPid(pid)
  54. if err != nil {
  55. return fmt.Errorf("failed to get oom_score_adj for %d", pid)
  56. }
  57. if oomScore < expectedMinOOMScoreAdj {
  58. return fmt.Errorf("expected pid %d's oom_score_adj to be >= %d; found %d", pid, expectedMinOOMScoreAdj, oomScore)
  59. }
  60. if oomScore < expectedMaxOOMScoreAdj {
  61. return fmt.Errorf("expected pid %d's oom_score_adj to be < %d; found %d", pid, expectedMaxOOMScoreAdj, oomScore)
  62. }
  63. return nil
  64. }
  65. var _ = framework.KubeDescribe("Container Manager Misc [Serial]", func() {
  66. f := framework.NewDefaultFramework("kubelet-container-manager")
  67. Describe("Validate OOM score adjustments [NodeFeature:OOMScoreAdj]", func() {
  68. Context("once the node is setup", func() {
  69. It("container runtime's oom-score-adj should be -999", func() {
  70. runtimePids, err := getPidsForProcess(framework.TestContext.ContainerRuntimeProcessName, framework.TestContext.ContainerRuntimePidFile)
  71. Expect(err).To(BeNil(), "failed to get list of container runtime pids")
  72. for _, pid := range runtimePids {
  73. Eventually(func() error {
  74. return validateOOMScoreAdjSetting(pid, -999)
  75. }, 5*time.Minute, 30*time.Second).Should(BeNil())
  76. }
  77. })
  78. It("Kubelet's oom-score-adj should be -999", func() {
  79. kubeletPids, err := getPidsForProcess(kubeletProcessName, "")
  80. Expect(err).To(BeNil(), "failed to get list of kubelet pids")
  81. Expect(len(kubeletPids)).To(Equal(1), "expected only one kubelet process; found %d", len(kubeletPids))
  82. Eventually(func() error {
  83. return validateOOMScoreAdjSetting(kubeletPids[0], -999)
  84. }, 5*time.Minute, 30*time.Second).Should(BeNil())
  85. })
  86. Context("", func() {
  87. It("pod infra containers oom-score-adj should be -998 and best effort container's should be 1000", func() {
  88. // Take a snapshot of existing pause processes. These were
  89. // created before this test, and may not be infra
  90. // containers. They should be excluded from the test.
  91. existingPausePIDs, err := getPidsForProcess("pause", "")
  92. Expect(err).To(BeNil(), "failed to list all pause processes on the node")
  93. existingPausePIDSet := sets.NewInt(existingPausePIDs...)
  94. podClient := f.PodClient()
  95. podName := "besteffort" + string(uuid.NewUUID())
  96. podClient.Create(&v1.Pod{
  97. ObjectMeta: metav1.ObjectMeta{
  98. Name: podName,
  99. },
  100. Spec: v1.PodSpec{
  101. Containers: []v1.Container{
  102. {
  103. Image: framework.ServeHostnameImage,
  104. Name: podName,
  105. },
  106. },
  107. },
  108. })
  109. var pausePids []int
  110. By("checking infra container's oom-score-adj")
  111. Eventually(func() error {
  112. pausePids, err = getPidsForProcess("pause", "")
  113. if err != nil {
  114. return fmt.Errorf("failed to get list of pause pids: %v", err)
  115. }
  116. for _, pid := range pausePids {
  117. if existingPausePIDSet.Has(pid) {
  118. // Not created by this test. Ignore it.
  119. continue
  120. }
  121. if err := validateOOMScoreAdjSetting(pid, -998); err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }, 2*time.Minute, time.Second*4).Should(BeNil())
  127. var shPids []int
  128. By("checking besteffort container's oom-score-adj")
  129. Eventually(func() error {
  130. shPids, err = getPidsForProcess("serve_hostname", "")
  131. if err != nil {
  132. return fmt.Errorf("failed to get list of serve hostname process pids: %v", err)
  133. }
  134. if len(shPids) != 1 {
  135. return fmt.Errorf("expected only one serve_hostname process; found %d", len(shPids))
  136. }
  137. return validateOOMScoreAdjSetting(shPids[0], 1000)
  138. }, 2*time.Minute, time.Second*4).Should(BeNil())
  139. })
  140. // Log the running containers here to help debugging.
  141. AfterEach(func() {
  142. if CurrentGinkgoTestDescription().Failed {
  143. By("Dump all running containers")
  144. runtime, _, err := getCRIClient()
  145. Expect(err).NotTo(HaveOccurred())
  146. containers, err := runtime.ListContainers(&runtimeapi.ContainerFilter{
  147. State: &runtimeapi.ContainerStateValue{
  148. State: runtimeapi.ContainerState_CONTAINER_RUNNING,
  149. },
  150. })
  151. Expect(err).NotTo(HaveOccurred())
  152. e2elog.Logf("Running containers:")
  153. for _, c := range containers {
  154. e2elog.Logf("%+v", c)
  155. }
  156. }
  157. })
  158. })
  159. It("guaranteed container's oom-score-adj should be -998", func() {
  160. podClient := f.PodClient()
  161. podName := "guaranteed" + string(uuid.NewUUID())
  162. podClient.Create(&v1.Pod{
  163. ObjectMeta: metav1.ObjectMeta{
  164. Name: podName,
  165. },
  166. Spec: v1.PodSpec{
  167. Containers: []v1.Container{
  168. {
  169. Image: imageutils.GetE2EImage(imageutils.Nginx),
  170. Name: podName,
  171. Resources: v1.ResourceRequirements{
  172. Limits: v1.ResourceList{
  173. v1.ResourceCPU: resource.MustParse("100m"),
  174. v1.ResourceMemory: resource.MustParse("50Mi"),
  175. },
  176. },
  177. },
  178. },
  179. },
  180. })
  181. var (
  182. ngPids []int
  183. err error
  184. )
  185. Eventually(func() error {
  186. ngPids, err = getPidsForProcess("nginx", "")
  187. if err != nil {
  188. return fmt.Errorf("failed to get list of nginx process pids: %v", err)
  189. }
  190. for _, pid := range ngPids {
  191. if err := validateOOMScoreAdjSetting(pid, -998); err != nil {
  192. return err
  193. }
  194. }
  195. return nil
  196. }, 2*time.Minute, time.Second*4).Should(BeNil())
  197. })
  198. It("burstable container's oom-score-adj should be between [2, 1000)", func() {
  199. podClient := f.PodClient()
  200. podName := "burstable" + string(uuid.NewUUID())
  201. podClient.Create(&v1.Pod{
  202. ObjectMeta: metav1.ObjectMeta{
  203. Name: podName,
  204. },
  205. Spec: v1.PodSpec{
  206. Containers: []v1.Container{
  207. {
  208. Image: imageutils.GetE2EImage(imageutils.TestWebserver),
  209. Name: podName,
  210. Resources: v1.ResourceRequirements{
  211. Requests: v1.ResourceList{
  212. v1.ResourceCPU: resource.MustParse("100m"),
  213. v1.ResourceMemory: resource.MustParse("50Mi"),
  214. },
  215. },
  216. },
  217. },
  218. },
  219. })
  220. var (
  221. wsPids []int
  222. err error
  223. )
  224. Eventually(func() error {
  225. wsPids, err = getPidsForProcess("test-webserver", "")
  226. if err != nil {
  227. return fmt.Errorf("failed to get list of test-webserver process pids: %v", err)
  228. }
  229. for _, pid := range wsPids {
  230. if err := validateOOMScoreAdjSettingIsInRange(pid, 2, 1000); err != nil {
  231. return err
  232. }
  233. }
  234. return nil
  235. }, 2*time.Minute, time.Second*4).Should(BeNil())
  236. // TODO: Test the oom-score-adj logic for burstable more accurately.
  237. })
  238. })
  239. })
  240. })