sysctl.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Copyright 2014 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 common
  14. import (
  15. "k8s.io/api/core/v1"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/util/uuid"
  18. "k8s.io/kubernetes/pkg/kubelet/sysctl"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. imageutils "k8s.io/kubernetes/test/utils/image"
  21. . "github.com/onsi/ginkgo"
  22. . "github.com/onsi/gomega"
  23. )
  24. var _ = framework.KubeDescribe("Sysctls [NodeFeature:Sysctls]", func() {
  25. f := framework.NewDefaultFramework("sysctl")
  26. var podClient *framework.PodClient
  27. testPod := func() *v1.Pod {
  28. podName := "sysctl-" + string(uuid.NewUUID())
  29. pod := v1.Pod{
  30. ObjectMeta: metav1.ObjectMeta{
  31. Name: podName,
  32. Annotations: map[string]string{},
  33. },
  34. Spec: v1.PodSpec{
  35. Containers: []v1.Container{
  36. {
  37. Name: "test-container",
  38. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  39. },
  40. },
  41. RestartPolicy: v1.RestartPolicyNever,
  42. },
  43. }
  44. return &pod
  45. }
  46. BeforeEach(func() {
  47. podClient = f.PodClient()
  48. })
  49. It("should support sysctls", func() {
  50. pod := testPod()
  51. pod.Spec.SecurityContext = &v1.PodSecurityContext{
  52. Sysctls: []v1.Sysctl{
  53. {
  54. Name: "kernel.shm_rmid_forced",
  55. Value: "1",
  56. },
  57. },
  58. }
  59. pod.Spec.Containers[0].Command = []string{"/bin/sysctl", "kernel.shm_rmid_forced"}
  60. By("Creating a pod with the kernel.shm_rmid_forced sysctl")
  61. pod = podClient.Create(pod)
  62. By("Watching for error events or started pod")
  63. // watch for events instead of termination of pod because the kubelet deletes
  64. // failed pods without running containers. This would create a race as the pod
  65. // might have already been deleted here.
  66. ev, err := f.PodClient().WaitForErrorEventOrSuccess(pod)
  67. framework.ExpectNoError(err)
  68. if ev != nil && ev.Reason == sysctl.UnsupportedReason {
  69. framework.Skipf("No sysctl support in Docker <1.12")
  70. }
  71. Expect(ev).To(BeNil())
  72. By("Waiting for pod completion")
  73. err = f.WaitForPodNoLongerRunning(pod.Name)
  74. framework.ExpectNoError(err)
  75. pod, err = podClient.Get(pod.Name, metav1.GetOptions{})
  76. framework.ExpectNoError(err)
  77. By("Checking that the pod succeeded")
  78. Expect(pod.Status.Phase).To(Equal(v1.PodSucceeded))
  79. By("Getting logs from the pod")
  80. log, err := framework.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, pod.Spec.Containers[0].Name)
  81. framework.ExpectNoError(err)
  82. By("Checking that the sysctl is actually updated")
  83. Expect(log).To(ContainSubstring("kernel.shm_rmid_forced = 1"))
  84. })
  85. It("should support unsafe sysctls which are actually whitelisted", func() {
  86. pod := testPod()
  87. pod.Spec.SecurityContext = &v1.PodSecurityContext{
  88. Sysctls: []v1.Sysctl{
  89. {
  90. Name: "kernel.shm_rmid_forced",
  91. Value: "1",
  92. },
  93. },
  94. }
  95. pod.Spec.Containers[0].Command = []string{"/bin/sysctl", "kernel.shm_rmid_forced"}
  96. By("Creating a pod with the kernel.shm_rmid_forced sysctl")
  97. pod = podClient.Create(pod)
  98. By("Watching for error events or started pod")
  99. // watch for events instead of termination of pod because the kubelet deletes
  100. // failed pods without running containers. This would create a race as the pod
  101. // might have already been deleted here.
  102. ev, err := f.PodClient().WaitForErrorEventOrSuccess(pod)
  103. framework.ExpectNoError(err)
  104. if ev != nil && ev.Reason == sysctl.UnsupportedReason {
  105. framework.Skipf("No sysctl support in Docker <1.12")
  106. }
  107. Expect(ev).To(BeNil())
  108. By("Waiting for pod completion")
  109. err = f.WaitForPodNoLongerRunning(pod.Name)
  110. framework.ExpectNoError(err)
  111. pod, err = podClient.Get(pod.Name, metav1.GetOptions{})
  112. framework.ExpectNoError(err)
  113. By("Checking that the pod succeeded")
  114. Expect(pod.Status.Phase).To(Equal(v1.PodSucceeded))
  115. By("Getting logs from the pod")
  116. log, err := framework.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, pod.Spec.Containers[0].Name)
  117. framework.ExpectNoError(err)
  118. By("Checking that the sysctl is actually updated")
  119. Expect(log).To(ContainSubstring("kernel.shm_rmid_forced = 1"))
  120. })
  121. It("should reject invalid sysctls", func() {
  122. pod := testPod()
  123. pod.Spec.SecurityContext = &v1.PodSecurityContext{
  124. Sysctls: []v1.Sysctl{
  125. // Safe parameters
  126. {
  127. Name: "foo-",
  128. Value: "bar",
  129. },
  130. {
  131. Name: "kernel.shmmax",
  132. Value: "100000000",
  133. },
  134. {
  135. Name: "safe-and-unsafe",
  136. Value: "100000000",
  137. },
  138. {
  139. Name: "bar..",
  140. Value: "42",
  141. },
  142. },
  143. }
  144. By("Creating a pod with one valid and two invalid sysctls")
  145. client := f.ClientSet.CoreV1().Pods(f.Namespace.Name)
  146. _, err := client.Create(pod)
  147. Expect(err).NotTo(BeNil())
  148. Expect(err.Error()).To(ContainSubstring(`Invalid value: "foo-"`))
  149. Expect(err.Error()).To(ContainSubstring(`Invalid value: "bar.."`))
  150. Expect(err.Error()).NotTo(ContainSubstring(`safe-and-unsafe`))
  151. Expect(err.Error()).NotTo(ContainSubstring("kernel.shmmax"))
  152. })
  153. It("should not launch unsafe, but not explicitly enabled sysctls on the node", func() {
  154. pod := testPod()
  155. pod.Spec.SecurityContext = &v1.PodSecurityContext{
  156. Sysctls: []v1.Sysctl{
  157. {
  158. Name: "kernel.msgmax",
  159. Value: "10000000000",
  160. },
  161. },
  162. }
  163. By("Creating a pod with a greylisted, but not whitelisted sysctl on the node")
  164. pod = podClient.Create(pod)
  165. By("Watching for error events or started pod")
  166. // watch for events instead of termination of pod because the kubelet deletes
  167. // failed pods without running containers. This would create a race as the pod
  168. // might have already been deleted here.
  169. ev, err := f.PodClient().WaitForErrorEventOrSuccess(pod)
  170. framework.ExpectNoError(err)
  171. if ev != nil && ev.Reason == sysctl.UnsupportedReason {
  172. framework.Skipf("No sysctl support in Docker <1.12")
  173. }
  174. By("Checking that the pod was rejected")
  175. Expect(ev).ToNot(BeNil())
  176. Expect(ev.Reason).To(Equal("SysctlForbidden"))
  177. })
  178. })