pods_container_manager_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. "strings"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/api/resource"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/uuid"
  20. "k8s.io/kubernetes/pkg/kubelet/cm"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. imageutils "k8s.io/kubernetes/test/utils/image"
  23. . "github.com/onsi/ginkgo"
  24. . "github.com/onsi/gomega"
  25. "k8s.io/klog"
  26. )
  27. // getResourceList returns a ResourceList with the
  28. // specified cpu and memory resource values
  29. func getResourceList(cpu, memory string) v1.ResourceList {
  30. res := v1.ResourceList{}
  31. if cpu != "" {
  32. res[v1.ResourceCPU] = resource.MustParse(cpu)
  33. }
  34. if memory != "" {
  35. res[v1.ResourceMemory] = resource.MustParse(memory)
  36. }
  37. return res
  38. }
  39. // getResourceRequirements returns a ResourceRequirements object
  40. func getResourceRequirements(requests, limits v1.ResourceList) v1.ResourceRequirements {
  41. res := v1.ResourceRequirements{}
  42. res.Requests = requests
  43. res.Limits = limits
  44. return res
  45. }
  46. const (
  47. // Kubelet internal cgroup name for burstable tier
  48. burstableCgroup = "burstable"
  49. // Kubelet internal cgroup name for besteffort tier
  50. bestEffortCgroup = "besteffort"
  51. )
  52. // makePodToVerifyCgroups returns a pod that verifies the existence of the specified cgroups.
  53. func makePodToVerifyCgroups(cgroupNames []string) *v1.Pod {
  54. // convert the names to their literal cgroupfs forms...
  55. cgroupFsNames := []string{}
  56. rootCgroupName := cm.NewCgroupName(cm.RootCgroupName, defaultNodeAllocatableCgroup)
  57. for _, baseName := range cgroupNames {
  58. // Add top level cgroup used to enforce node allocatable.
  59. cgroupComponents := strings.Split(baseName, "/")
  60. cgroupName := cm.NewCgroupName(rootCgroupName, cgroupComponents...)
  61. cgroupFsNames = append(cgroupFsNames, toCgroupFsName(cgroupName))
  62. }
  63. klog.Infof("expecting %v cgroups to be found", cgroupFsNames)
  64. // build the pod command to either verify cgroups exist
  65. command := ""
  66. for _, cgroupFsName := range cgroupFsNames {
  67. localCommand := "if [ ! -d /tmp/memory/" + cgroupFsName + " ] || [ ! -d /tmp/cpu/" + cgroupFsName + " ]; then exit 1; fi; "
  68. command += localCommand
  69. }
  70. pod := &v1.Pod{
  71. ObjectMeta: metav1.ObjectMeta{
  72. Name: "pod" + string(uuid.NewUUID()),
  73. },
  74. Spec: v1.PodSpec{
  75. RestartPolicy: v1.RestartPolicyNever,
  76. Containers: []v1.Container{
  77. {
  78. Image: busyboxImage,
  79. Name: "container" + string(uuid.NewUUID()),
  80. Command: []string{"sh", "-c", command},
  81. VolumeMounts: []v1.VolumeMount{
  82. {
  83. Name: "sysfscgroup",
  84. MountPath: "/tmp",
  85. },
  86. },
  87. },
  88. },
  89. Volumes: []v1.Volume{
  90. {
  91. Name: "sysfscgroup",
  92. VolumeSource: v1.VolumeSource{
  93. HostPath: &v1.HostPathVolumeSource{Path: "/sys/fs/cgroup"},
  94. },
  95. },
  96. },
  97. },
  98. }
  99. return pod
  100. }
  101. // makePodToVerifyCgroupRemoved verfies the specified cgroup does not exist.
  102. func makePodToVerifyCgroupRemoved(baseName string) *v1.Pod {
  103. components := strings.Split(baseName, "/")
  104. cgroupName := cm.NewCgroupName(cm.RootCgroupName, components...)
  105. cgroupFsName := toCgroupFsName(cgroupName)
  106. pod := &v1.Pod{
  107. ObjectMeta: metav1.ObjectMeta{
  108. Name: "pod" + string(uuid.NewUUID()),
  109. },
  110. Spec: v1.PodSpec{
  111. RestartPolicy: v1.RestartPolicyOnFailure,
  112. Containers: []v1.Container{
  113. {
  114. Image: busyboxImage,
  115. Name: "container" + string(uuid.NewUUID()),
  116. Command: []string{"sh", "-c", "for i in `seq 1 10`; do if [ ! -d /tmp/memory/" + cgroupFsName + " ] && [ ! -d /tmp/cpu/" + cgroupFsName + " ]; then exit 0; else sleep 10; fi; done; exit 1"},
  117. VolumeMounts: []v1.VolumeMount{
  118. {
  119. Name: "sysfscgroup",
  120. MountPath: "/tmp",
  121. },
  122. },
  123. },
  124. },
  125. Volumes: []v1.Volume{
  126. {
  127. Name: "sysfscgroup",
  128. VolumeSource: v1.VolumeSource{
  129. HostPath: &v1.HostPathVolumeSource{Path: "/sys/fs/cgroup"},
  130. },
  131. },
  132. },
  133. },
  134. }
  135. return pod
  136. }
  137. var _ = framework.KubeDescribe("Kubelet Cgroup Manager", func() {
  138. f := framework.NewDefaultFramework("kubelet-cgroup-manager")
  139. Describe("QOS containers", func() {
  140. Context("On enabling QOS cgroup hierarchy", func() {
  141. It("Top level QoS containers should have been created [NodeConformance]", func() {
  142. if !framework.TestContext.KubeletConfig.CgroupsPerQOS {
  143. return
  144. }
  145. cgroupsToVerify := []string{burstableCgroup, bestEffortCgroup}
  146. pod := makePodToVerifyCgroups(cgroupsToVerify)
  147. f.PodClient().Create(pod)
  148. err := framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
  149. Expect(err).NotTo(HaveOccurred())
  150. })
  151. })
  152. })
  153. Describe("Pod containers [NodeConformance]", func() {
  154. Context("On scheduling a Guaranteed Pod", func() {
  155. It("Pod containers should have been created under the cgroup-root", func() {
  156. if !framework.TestContext.KubeletConfig.CgroupsPerQOS {
  157. return
  158. }
  159. var (
  160. guaranteedPod *v1.Pod
  161. podUID string
  162. )
  163. By("Creating a Guaranteed pod in Namespace", func() {
  164. guaranteedPod = f.PodClient().Create(&v1.Pod{
  165. ObjectMeta: metav1.ObjectMeta{
  166. Name: "pod" + string(uuid.NewUUID()),
  167. Namespace: f.Namespace.Name,
  168. },
  169. Spec: v1.PodSpec{
  170. Containers: []v1.Container{
  171. {
  172. Image: imageutils.GetPauseImageName(),
  173. Name: "container" + string(uuid.NewUUID()),
  174. Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
  175. },
  176. },
  177. },
  178. })
  179. podUID = string(guaranteedPod.UID)
  180. })
  181. By("Checking if the pod cgroup was created", func() {
  182. cgroupsToVerify := []string{"pod" + podUID}
  183. pod := makePodToVerifyCgroups(cgroupsToVerify)
  184. f.PodClient().Create(pod)
  185. err := framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
  186. Expect(err).NotTo(HaveOccurred())
  187. })
  188. By("Checking if the pod cgroup was deleted", func() {
  189. gp := int64(1)
  190. Expect(f.PodClient().Delete(guaranteedPod.Name, &metav1.DeleteOptions{GracePeriodSeconds: &gp})).NotTo(HaveOccurred())
  191. pod := makePodToVerifyCgroupRemoved("pod" + podUID)
  192. f.PodClient().Create(pod)
  193. err := framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
  194. Expect(err).NotTo(HaveOccurred())
  195. })
  196. })
  197. })
  198. Context("On scheduling a BestEffort Pod", func() {
  199. It("Pod containers should have been created under the BestEffort cgroup", func() {
  200. if !framework.TestContext.KubeletConfig.CgroupsPerQOS {
  201. return
  202. }
  203. var (
  204. podUID string
  205. bestEffortPod *v1.Pod
  206. )
  207. By("Creating a BestEffort pod in Namespace", func() {
  208. bestEffortPod = f.PodClient().Create(&v1.Pod{
  209. ObjectMeta: metav1.ObjectMeta{
  210. Name: "pod" + string(uuid.NewUUID()),
  211. Namespace: f.Namespace.Name,
  212. },
  213. Spec: v1.PodSpec{
  214. Containers: []v1.Container{
  215. {
  216. Image: imageutils.GetPauseImageName(),
  217. Name: "container" + string(uuid.NewUUID()),
  218. Resources: getResourceRequirements(getResourceList("", ""), getResourceList("", "")),
  219. },
  220. },
  221. },
  222. })
  223. podUID = string(bestEffortPod.UID)
  224. })
  225. By("Checking if the pod cgroup was created", func() {
  226. cgroupsToVerify := []string{"besteffort/pod" + podUID}
  227. pod := makePodToVerifyCgroups(cgroupsToVerify)
  228. f.PodClient().Create(pod)
  229. err := framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
  230. Expect(err).NotTo(HaveOccurred())
  231. })
  232. By("Checking if the pod cgroup was deleted", func() {
  233. gp := int64(1)
  234. Expect(f.PodClient().Delete(bestEffortPod.Name, &metav1.DeleteOptions{GracePeriodSeconds: &gp})).NotTo(HaveOccurred())
  235. pod := makePodToVerifyCgroupRemoved("besteffort/pod" + podUID)
  236. f.PodClient().Create(pod)
  237. err := framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
  238. Expect(err).NotTo(HaveOccurred())
  239. })
  240. })
  241. })
  242. Context("On scheduling a Burstable Pod", func() {
  243. It("Pod containers should have been created under the Burstable cgroup", func() {
  244. if !framework.TestContext.KubeletConfig.CgroupsPerQOS {
  245. return
  246. }
  247. var (
  248. podUID string
  249. burstablePod *v1.Pod
  250. )
  251. By("Creating a Burstable pod in Namespace", func() {
  252. burstablePod = f.PodClient().Create(&v1.Pod{
  253. ObjectMeta: metav1.ObjectMeta{
  254. Name: "pod" + string(uuid.NewUUID()),
  255. Namespace: f.Namespace.Name,
  256. },
  257. Spec: v1.PodSpec{
  258. Containers: []v1.Container{
  259. {
  260. Image: imageutils.GetPauseImageName(),
  261. Name: "container" + string(uuid.NewUUID()),
  262. Resources: getResourceRequirements(getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
  263. },
  264. },
  265. },
  266. })
  267. podUID = string(burstablePod.UID)
  268. })
  269. By("Checking if the pod cgroup was created", func() {
  270. cgroupsToVerify := []string{"burstable/pod" + podUID}
  271. pod := makePodToVerifyCgroups(cgroupsToVerify)
  272. f.PodClient().Create(pod)
  273. err := framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
  274. Expect(err).NotTo(HaveOccurred())
  275. })
  276. By("Checking if the pod cgroup was deleted", func() {
  277. gp := int64(1)
  278. Expect(f.PodClient().Delete(burstablePod.Name, &metav1.DeleteOptions{GracePeriodSeconds: &gp})).NotTo(HaveOccurred())
  279. pod := makePodToVerifyCgroupRemoved("burstable/pod" + podUID)
  280. f.PodClient().Create(pod)
  281. err := framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
  282. Expect(err).NotTo(HaveOccurred())
  283. })
  284. })
  285. })
  286. })
  287. })