pods_container_manager_test.go 10 KB

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