pids_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 e2enode
  14. import (
  15. "fmt"
  16. v1 "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. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  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. )
  27. // makePodToVerifyPids returns a pod that verifies specified cgroup with pids
  28. func makePodToVerifyPids(baseName string, pidsLimit resource.Quantity) *v1.Pod {
  29. // convert the cgroup name to its literal form
  30. cgroupFsName := ""
  31. cgroupName := cm.NewCgroupName(cm.RootCgroupName, defaultNodeAllocatableCgroup, baseName)
  32. if framework.TestContext.KubeletConfig.CgroupDriver == "systemd" {
  33. cgroupFsName = cgroupName.ToSystemd()
  34. } else {
  35. cgroupFsName = cgroupName.ToCgroupfs()
  36. }
  37. // this command takes the expected value and compares it against the actual value for the pod cgroup pids.max
  38. command := fmt.Sprintf("expected=%v; actual=$(cat /tmp/pids/%v/pids.max); if [ \"$expected\" -ne \"$actual\" ]; then exit 1; fi; ", pidsLimit.Value(), cgroupFsName)
  39. framework.Logf("Pod to run command: %v", command)
  40. pod := &v1.Pod{
  41. ObjectMeta: metav1.ObjectMeta{
  42. Name: "pod" + string(uuid.NewUUID()),
  43. },
  44. Spec: v1.PodSpec{
  45. RestartPolicy: v1.RestartPolicyNever,
  46. Containers: []v1.Container{
  47. {
  48. Image: busyboxImage,
  49. Name: "container" + string(uuid.NewUUID()),
  50. Command: []string{"sh", "-c", command},
  51. VolumeMounts: []v1.VolumeMount{
  52. {
  53. Name: "sysfscgroup",
  54. MountPath: "/tmp",
  55. },
  56. },
  57. },
  58. },
  59. Volumes: []v1.Volume{
  60. {
  61. Name: "sysfscgroup",
  62. VolumeSource: v1.VolumeSource{
  63. HostPath: &v1.HostPathVolumeSource{Path: "/sys/fs/cgroup"},
  64. },
  65. },
  66. },
  67. },
  68. }
  69. return pod
  70. }
  71. func runPodPidsLimitTests(f *framework.Framework) {
  72. ginkgo.It("should set pids.max for Pod", func() {
  73. ginkgo.By("by creating a G pod")
  74. pod := f.PodClient().Create(&v1.Pod{
  75. ObjectMeta: metav1.ObjectMeta{
  76. Name: "pod" + string(uuid.NewUUID()),
  77. Namespace: f.Namespace.Name,
  78. },
  79. Spec: v1.PodSpec{
  80. Containers: []v1.Container{
  81. {
  82. Image: imageutils.GetPauseImageName(),
  83. Name: "container" + string(uuid.NewUUID()),
  84. Resources: v1.ResourceRequirements{
  85. Limits: v1.ResourceList{
  86. v1.ResourceName("cpu"): resource.MustParse("10m"),
  87. v1.ResourceName("memory"): resource.MustParse("100Mi"),
  88. },
  89. },
  90. },
  91. },
  92. },
  93. })
  94. podUID := string(pod.UID)
  95. ginkgo.By("checking if the expected pids settings were applied")
  96. verifyPod := makePodToVerifyPids("pod"+podUID, resource.MustParse("1024"))
  97. f.PodClient().Create(verifyPod)
  98. err := e2epod.WaitForPodSuccessInNamespace(f.ClientSet, verifyPod.Name, f.Namespace.Name)
  99. framework.ExpectNoError(err)
  100. })
  101. }
  102. // Serial because the test updates kubelet configuration.
  103. var _ = SIGDescribe("PodPidsLimit [Serial] [Feature:SupportPodPidsLimit][NodeFeature:SupportPodPidsLimit]", func() {
  104. f := framework.NewDefaultFramework("pids-limit-test")
  105. ginkgo.Context("With config updated with pids feature enabled", func() {
  106. tempSetCurrentKubeletConfig(f, func(initialConfig *kubeletconfig.KubeletConfiguration) {
  107. if initialConfig.FeatureGates == nil {
  108. initialConfig.FeatureGates = make(map[string]bool)
  109. }
  110. initialConfig.FeatureGates["SupportPodPidsLimit"] = true
  111. initialConfig.PodPidsLimit = int64(1024)
  112. })
  113. runPodPidsLimitTests(f)
  114. })
  115. })