container_manager_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 e2enode
  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. imageutils "k8s.io/kubernetes/test/utils/image"
  30. "github.com/onsi/ginkgo"
  31. "github.com/onsi/gomega"
  32. )
  33. func getOOMScoreForPid(pid int) (int, error) {
  34. procfsPath := path.Join("/proc", strconv.Itoa(pid), "oom_score_adj")
  35. out, err := exec.Command("sudo", "cat", procfsPath).CombinedOutput()
  36. if err != nil {
  37. return 0, err
  38. }
  39. return strconv.Atoi(strings.TrimSpace(string(out)))
  40. }
  41. func validateOOMScoreAdjSetting(pid int, expectedOOMScoreAdj int) error {
  42. oomScore, err := getOOMScoreForPid(pid)
  43. if err != nil {
  44. return fmt.Errorf("failed to get oom_score_adj for %d: %v", pid, err)
  45. }
  46. if expectedOOMScoreAdj != oomScore {
  47. return fmt.Errorf("expected pid %d's oom_score_adj to be %d; found %d", pid, expectedOOMScoreAdj, oomScore)
  48. }
  49. return nil
  50. }
  51. func validateOOMScoreAdjSettingIsInRange(pid int, expectedMinOOMScoreAdj, expectedMaxOOMScoreAdj int) error {
  52. oomScore, err := getOOMScoreForPid(pid)
  53. if err != nil {
  54. return fmt.Errorf("failed to get oom_score_adj for %d", pid)
  55. }
  56. if oomScore < expectedMinOOMScoreAdj {
  57. return fmt.Errorf("expected pid %d's oom_score_adj to be >= %d; found %d", pid, expectedMinOOMScoreAdj, oomScore)
  58. }
  59. if oomScore < expectedMaxOOMScoreAdj {
  60. return fmt.Errorf("expected pid %d's oom_score_adj to be < %d; found %d", pid, expectedMaxOOMScoreAdj, oomScore)
  61. }
  62. return nil
  63. }
  64. var _ = framework.KubeDescribe("Container Manager Misc [Serial]", func() {
  65. f := framework.NewDefaultFramework("kubelet-container-manager")
  66. ginkgo.Describe("Validate OOM score adjustments [NodeFeature:OOMScoreAdj]", func() {
  67. ginkgo.Context("once the node is setup", func() {
  68. ginkgo.It("container runtime's oom-score-adj should be -999", func() {
  69. runtimePids, err := getPidsForProcess(framework.TestContext.ContainerRuntimeProcessName, framework.TestContext.ContainerRuntimePidFile)
  70. framework.ExpectNoError(err, "failed to get list of container runtime pids")
  71. for _, pid := range runtimePids {
  72. gomega.Eventually(func() error {
  73. return validateOOMScoreAdjSetting(pid, -999)
  74. }, 5*time.Minute, 30*time.Second).Should(gomega.BeNil())
  75. }
  76. })
  77. ginkgo.It("Kubelet's oom-score-adj should be -999", func() {
  78. kubeletPids, err := getPidsForProcess(kubeletProcessName, "")
  79. framework.ExpectNoError(err, "failed to get list of kubelet pids")
  80. framework.ExpectEqual(len(kubeletPids), 1, "expected only one kubelet process; found %d", len(kubeletPids))
  81. gomega.Eventually(func() error {
  82. return validateOOMScoreAdjSetting(kubeletPids[0], -999)
  83. }, 5*time.Minute, 30*time.Second).Should(gomega.BeNil())
  84. })
  85. ginkgo.Context("", func() {
  86. ginkgo.It("pod infra containers oom-score-adj should be -998 and best effort container's should be 1000", func() {
  87. // Take a snapshot of existing pause processes. These were
  88. // created before this test, and may not be infra
  89. // containers. They should be excluded from the test.
  90. existingPausePIDs, err := getPidsForProcess("pause", "")
  91. framework.ExpectNoError(err, "failed to list all pause processes on the node")
  92. existingPausePIDSet := sets.NewInt(existingPausePIDs...)
  93. podClient := f.PodClient()
  94. podName := "besteffort" + string(uuid.NewUUID())
  95. podClient.Create(&v1.Pod{
  96. ObjectMeta: metav1.ObjectMeta{
  97. Name: podName,
  98. },
  99. Spec: v1.PodSpec{
  100. Containers: []v1.Container{
  101. {
  102. Image: framework.ServeHostnameImage,
  103. Name: podName,
  104. },
  105. },
  106. },
  107. })
  108. var pausePids []int
  109. ginkgo.By("checking infra container's oom-score-adj")
  110. gomega.Eventually(func() error {
  111. pausePids, err = getPidsForProcess("pause", "")
  112. if err != nil {
  113. return fmt.Errorf("failed to get list of pause pids: %v", err)
  114. }
  115. for _, pid := range pausePids {
  116. if existingPausePIDSet.Has(pid) {
  117. // Not created by this test. Ignore it.
  118. continue
  119. }
  120. if err := validateOOMScoreAdjSetting(pid, -998); err != nil {
  121. return err
  122. }
  123. }
  124. return nil
  125. }, 2*time.Minute, time.Second*4).Should(gomega.BeNil())
  126. var shPids []int
  127. ginkgo.By("checking besteffort container's oom-score-adj")
  128. gomega.Eventually(func() error {
  129. shPids, err = getPidsForProcess("agnhost", "")
  130. if err != nil {
  131. return fmt.Errorf("failed to get list of serve hostname process pids: %v", err)
  132. }
  133. if len(shPids) != 1 {
  134. return fmt.Errorf("expected only one agnhost process; found %d", len(shPids))
  135. }
  136. return validateOOMScoreAdjSetting(shPids[0], 1000)
  137. }, 2*time.Minute, time.Second*4).Should(gomega.BeNil())
  138. })
  139. // Log the running containers here to help debugging.
  140. ginkgo.AfterEach(func() {
  141. if ginkgo.CurrentGinkgoTestDescription().Failed {
  142. ginkgo.By("Dump all running containers")
  143. runtime, _, err := getCRIClient()
  144. framework.ExpectNoError(err)
  145. containers, err := runtime.ListContainers(&runtimeapi.ContainerFilter{
  146. State: &runtimeapi.ContainerStateValue{
  147. State: runtimeapi.ContainerState_CONTAINER_RUNNING,
  148. },
  149. })
  150. framework.ExpectNoError(err)
  151. framework.Logf("Running containers:")
  152. for _, c := range containers {
  153. framework.Logf("%+v", c)
  154. }
  155. }
  156. })
  157. })
  158. ginkgo.It("guaranteed container's oom-score-adj should be -998", func() {
  159. podClient := f.PodClient()
  160. podName := "guaranteed" + string(uuid.NewUUID())
  161. podClient.Create(&v1.Pod{
  162. ObjectMeta: metav1.ObjectMeta{
  163. Name: podName,
  164. },
  165. Spec: v1.PodSpec{
  166. Containers: []v1.Container{
  167. {
  168. Image: imageutils.GetE2EImage(imageutils.Nginx),
  169. Name: podName,
  170. Resources: v1.ResourceRequirements{
  171. Limits: v1.ResourceList{
  172. v1.ResourceCPU: resource.MustParse("100m"),
  173. v1.ResourceMemory: resource.MustParse("50Mi"),
  174. },
  175. },
  176. },
  177. },
  178. },
  179. })
  180. var (
  181. ngPids []int
  182. err error
  183. )
  184. gomega.Eventually(func() error {
  185. ngPids, err = getPidsForProcess("nginx", "")
  186. if err != nil {
  187. return fmt.Errorf("failed to get list of nginx process pids: %v", err)
  188. }
  189. for _, pid := range ngPids {
  190. if err := validateOOMScoreAdjSetting(pid, -998); err != nil {
  191. return err
  192. }
  193. }
  194. return nil
  195. }, 2*time.Minute, time.Second*4).Should(gomega.BeNil())
  196. })
  197. ginkgo.It("burstable container's oom-score-adj should be between [2, 1000)", func() {
  198. podClient := f.PodClient()
  199. podName := "burstable" + string(uuid.NewUUID())
  200. podClient.Create(&v1.Pod{
  201. ObjectMeta: metav1.ObjectMeta{
  202. Name: podName,
  203. },
  204. Spec: v1.PodSpec{
  205. Containers: []v1.Container{
  206. {
  207. Image: imageutils.GetE2EImage(imageutils.Agnhost),
  208. Args: []string{"test-webserver"},
  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. gomega.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(gomega.BeNil())
  236. // TODO: Test the oom-score-adj logic for burstable more accurately.
  237. })
  238. })
  239. })
  240. })