PageRenderTime 69ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/test/e2e/common/downwardapi_volume.go

https://bitbucket.org/Jake-Qu/kubernetes-mirror
Go | 474 lines | 341 code | 57 blank | 76 comment | 6 complexity | 7f7fd824048d480b72c30a5e8c131305 MD5 | raw file
Possible License(s): MIT, MPL-2.0-no-copyleft-exception, 0BSD, CC0-1.0, BSD-2-Clause, Apache-2.0, BSD-3-Clause
  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 common
  14. import (
  15. "fmt"
  16. "time"
  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/test/e2e/framework"
  22. . "github.com/onsi/ginkgo"
  23. . "github.com/onsi/gomega"
  24. )
  25. var _ = Describe("[sig-storage] Downward API volume", func() {
  26. // How long to wait for a log pod to be displayed
  27. const podLogTimeout = 2 * time.Minute
  28. f := framework.NewDefaultFramework("downward-api")
  29. var podClient *framework.PodClient
  30. BeforeEach(func() {
  31. podClient = f.PodClient()
  32. })
  33. /*
  34. Testname: downwardapi-volume-podname
  35. Description: Ensure that downward API can provide pod's name through
  36. DownwardAPIVolumeFiles.
  37. */
  38. framework.ConformanceIt("should provide podname only [NodeConformance]", func() {
  39. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  40. pod := downwardAPIVolumePodForSimpleTest(podName, "/etc/podinfo/podname")
  41. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  42. fmt.Sprintf("%s\n", podName),
  43. })
  44. })
  45. /*
  46. Testname: downwardapi-volume-set-default-mode
  47. Description: Ensure that downward API can set default file permission
  48. mode for DownwardAPIVolumeFiles if no mode is specified.
  49. */
  50. framework.ConformanceIt("should set DefaultMode on files [NodeConformance]", func() {
  51. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  52. defaultMode := int32(0400)
  53. pod := downwardAPIVolumePodForModeTest(podName, "/etc/podinfo/podname", nil, &defaultMode)
  54. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  55. "mode of file \"/etc/podinfo/podname\": -r--------",
  56. })
  57. })
  58. /*
  59. Testname: downwardapi-volume-set-mode
  60. Description: Ensure that downward API can set file permission mode for
  61. DownwardAPIVolumeFiles.
  62. */
  63. framework.ConformanceIt("should set mode on item file [NodeConformance]", func() {
  64. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  65. mode := int32(0400)
  66. pod := downwardAPIVolumePodForModeTest(podName, "/etc/podinfo/podname", &mode, nil)
  67. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  68. "mode of file \"/etc/podinfo/podname\": -r--------",
  69. })
  70. })
  71. It("should provide podname as non-root with fsgroup [NodeFeature:FSGroup]", func() {
  72. podName := "metadata-volume-" + string(uuid.NewUUID())
  73. uid := int64(1001)
  74. gid := int64(1234)
  75. pod := downwardAPIVolumePodForSimpleTest(podName, "/etc/podinfo/podname")
  76. pod.Spec.SecurityContext = &v1.PodSecurityContext{
  77. RunAsUser: &uid,
  78. FSGroup: &gid,
  79. }
  80. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  81. fmt.Sprintf("%s\n", podName),
  82. })
  83. })
  84. It("should provide podname as non-root with fsgroup and defaultMode [NodeFeature:FSGroup]", func() {
  85. podName := "metadata-volume-" + string(uuid.NewUUID())
  86. uid := int64(1001)
  87. gid := int64(1234)
  88. mode := int32(0440) /* setting fsGroup sets mode to at least 440 */
  89. pod := downwardAPIVolumePodForModeTest(podName, "/etc/podinfo/podname", &mode, nil)
  90. pod.Spec.SecurityContext = &v1.PodSecurityContext{
  91. RunAsUser: &uid,
  92. FSGroup: &gid,
  93. }
  94. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  95. "mode of file \"/etc/podinfo/podname\": -r--r-----",
  96. })
  97. })
  98. /*
  99. Testname: downwardapi-volume-update-label
  100. Description: Ensure that downward API updates labels in
  101. DownwardAPIVolumeFiles when pod's labels get modified.
  102. */
  103. framework.ConformanceIt("should update labels on modification [NodeConformance]", func() {
  104. labels := map[string]string{}
  105. labels["key1"] = "value1"
  106. labels["key2"] = "value2"
  107. podName := "labelsupdate" + string(uuid.NewUUID())
  108. pod := downwardAPIVolumePodForUpdateTest(podName, labels, map[string]string{}, "/etc/podinfo/labels")
  109. containerName := "client-container"
  110. By("Creating the pod")
  111. podClient.CreateSync(pod)
  112. Eventually(func() (string, error) {
  113. return framework.GetPodLogs(f.ClientSet, f.Namespace.Name, podName, containerName)
  114. },
  115. podLogTimeout, framework.Poll).Should(ContainSubstring("key1=\"value1\"\n"))
  116. //modify labels
  117. podClient.Update(podName, func(pod *v1.Pod) {
  118. pod.Labels["key3"] = "value3"
  119. })
  120. Eventually(func() (string, error) {
  121. return framework.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, containerName)
  122. },
  123. podLogTimeout, framework.Poll).Should(ContainSubstring("key3=\"value3\"\n"))
  124. })
  125. /*
  126. Testname: downwardapi-volume-update-annotation
  127. Description: Ensure that downward API updates annotations in
  128. DownwardAPIVolumeFiles when pod's annotations get modified.
  129. */
  130. framework.ConformanceIt("should update annotations on modification [NodeConformance]", func() {
  131. annotations := map[string]string{}
  132. annotations["builder"] = "bar"
  133. podName := "annotationupdate" + string(uuid.NewUUID())
  134. pod := downwardAPIVolumePodForUpdateTest(podName, map[string]string{}, annotations, "/etc/podinfo/annotations")
  135. containerName := "client-container"
  136. By("Creating the pod")
  137. podClient.CreateSync(pod)
  138. pod, err := podClient.Get(pod.Name, metav1.GetOptions{})
  139. Expect(err).NotTo(HaveOccurred(), "Failed to get pod %q", pod.Name)
  140. Eventually(func() (string, error) {
  141. return framework.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, containerName)
  142. },
  143. podLogTimeout, framework.Poll).Should(ContainSubstring("builder=\"bar\"\n"))
  144. //modify annotations
  145. podClient.Update(podName, func(pod *v1.Pod) {
  146. pod.Annotations["builder"] = "foo"
  147. })
  148. Eventually(func() (string, error) {
  149. return framework.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, containerName)
  150. },
  151. podLogTimeout, framework.Poll).Should(ContainSubstring("builder=\"foo\"\n"))
  152. })
  153. /*
  154. Testname: downwardapi-volume-cpu-limit
  155. Description: Ensure that downward API can provide container's CPU limit
  156. through DownwardAPIVolumeFiles.
  157. */
  158. framework.ConformanceIt("should provide container's cpu limit [NodeConformance]", func() {
  159. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  160. pod := downwardAPIVolumeForContainerResources(podName, "/etc/podinfo/cpu_limit")
  161. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  162. fmt.Sprintf("2\n"),
  163. })
  164. })
  165. /*
  166. Testname: downwardapi-volume-memory-limit
  167. Description: Ensure that downward API can provide container's memory
  168. limit through DownwardAPIVolumeFiles.
  169. */
  170. framework.ConformanceIt("should provide container's memory limit [NodeConformance]", func() {
  171. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  172. pod := downwardAPIVolumeForContainerResources(podName, "/etc/podinfo/memory_limit")
  173. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  174. fmt.Sprintf("67108864\n"),
  175. })
  176. })
  177. /*
  178. Testname: downwardapi-volume-cpu-request
  179. Description: Ensure that downward API can provide container's CPU
  180. request through DownwardAPIVolumeFiles.
  181. */
  182. framework.ConformanceIt("should provide container's cpu request [NodeConformance]", func() {
  183. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  184. pod := downwardAPIVolumeForContainerResources(podName, "/etc/podinfo/cpu_request")
  185. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  186. fmt.Sprintf("1\n"),
  187. })
  188. })
  189. /*
  190. Testname: downwardapi-volume-memory-request
  191. Description: Ensure that downward API can provide container's memory
  192. request through DownwardAPIVolumeFiles.
  193. */
  194. framework.ConformanceIt("should provide container's memory request [NodeConformance]", func() {
  195. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  196. pod := downwardAPIVolumeForContainerResources(podName, "/etc/podinfo/memory_request")
  197. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  198. fmt.Sprintf("33554432\n"),
  199. })
  200. })
  201. /*
  202. Testname: downwardapi-volume-default-cpu
  203. Description: Ensure that downward API can provide default node
  204. allocatable value for CPU through DownwardAPIVolumeFiles if CPU
  205. limit is not specified for a container.
  206. */
  207. framework.ConformanceIt("should provide node allocatable (cpu) as default cpu limit if the limit is not set [NodeConformance]", func() {
  208. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  209. pod := downwardAPIVolumeForDefaultContainerResources(podName, "/etc/podinfo/cpu_limit")
  210. f.TestContainerOutputRegexp("downward API volume plugin", pod, 0, []string{"[1-9]"})
  211. })
  212. /*
  213. Testname: downwardapi-volume-default-memory
  214. Description: Ensure that downward API can provide default node
  215. allocatable value for memory through DownwardAPIVolumeFiles if memory
  216. limit is not specified for a container.
  217. */
  218. framework.ConformanceIt("should provide node allocatable (memory) as default memory limit if the limit is not set [NodeConformance]", func() {
  219. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  220. pod := downwardAPIVolumeForDefaultContainerResources(podName, "/etc/podinfo/memory_limit")
  221. f.TestContainerOutputRegexp("downward API volume plugin", pod, 0, []string{"[1-9]"})
  222. })
  223. })
  224. func downwardAPIVolumePodForModeTest(name, filePath string, itemMode, defaultMode *int32) *v1.Pod {
  225. pod := downwardAPIVolumeBasePod(name, nil, nil)
  226. pod.Spec.Containers = []v1.Container{
  227. {
  228. Name: "client-container",
  229. Image: mountImage,
  230. Command: []string{"/mounttest", "--file_mode=" + filePath},
  231. VolumeMounts: []v1.VolumeMount{
  232. {
  233. Name: "podinfo",
  234. MountPath: "/etc/podinfo",
  235. },
  236. },
  237. },
  238. }
  239. if itemMode != nil {
  240. pod.Spec.Volumes[0].VolumeSource.DownwardAPI.Items[0].Mode = itemMode
  241. }
  242. if defaultMode != nil {
  243. pod.Spec.Volumes[0].VolumeSource.DownwardAPI.DefaultMode = defaultMode
  244. }
  245. return pod
  246. }
  247. func downwardAPIVolumePodForSimpleTest(name string, filePath string) *v1.Pod {
  248. pod := downwardAPIVolumeBasePod(name, nil, nil)
  249. pod.Spec.Containers = []v1.Container{
  250. {
  251. Name: "client-container",
  252. Image: mountImage,
  253. Command: []string{"/mounttest", "--file_content=" + filePath},
  254. VolumeMounts: []v1.VolumeMount{
  255. {
  256. Name: "podinfo",
  257. MountPath: "/etc/podinfo",
  258. ReadOnly: false,
  259. },
  260. },
  261. },
  262. }
  263. return pod
  264. }
  265. func downwardAPIVolumeForContainerResources(name string, filePath string) *v1.Pod {
  266. pod := downwardAPIVolumeBasePod(name, nil, nil)
  267. pod.Spec.Containers = downwardAPIVolumeBaseContainers("client-container", filePath)
  268. return pod
  269. }
  270. func downwardAPIVolumeForDefaultContainerResources(name string, filePath string) *v1.Pod {
  271. pod := downwardAPIVolumeBasePod(name, nil, nil)
  272. pod.Spec.Containers = downwardAPIVolumeDefaultBaseContainer("client-container", filePath)
  273. return pod
  274. }
  275. func downwardAPIVolumeBaseContainers(name, filePath string) []v1.Container {
  276. return []v1.Container{
  277. {
  278. Name: name,
  279. Image: mountImage,
  280. Command: []string{"/mounttest", "--file_content=" + filePath},
  281. Resources: v1.ResourceRequirements{
  282. Requests: v1.ResourceList{
  283. v1.ResourceCPU: resource.MustParse("250m"),
  284. v1.ResourceMemory: resource.MustParse("32Mi"),
  285. },
  286. Limits: v1.ResourceList{
  287. v1.ResourceCPU: resource.MustParse("1250m"),
  288. v1.ResourceMemory: resource.MustParse("64Mi"),
  289. },
  290. },
  291. VolumeMounts: []v1.VolumeMount{
  292. {
  293. Name: "podinfo",
  294. MountPath: "/etc/podinfo",
  295. ReadOnly: false,
  296. },
  297. },
  298. },
  299. }
  300. }
  301. func downwardAPIVolumeDefaultBaseContainer(name, filePath string) []v1.Container {
  302. return []v1.Container{
  303. {
  304. Name: name,
  305. Image: mountImage,
  306. Command: []string{"/mounttest", "--file_content=" + filePath},
  307. VolumeMounts: []v1.VolumeMount{
  308. {
  309. Name: "podinfo",
  310. MountPath: "/etc/podinfo",
  311. },
  312. },
  313. },
  314. }
  315. }
  316. func downwardAPIVolumePodForUpdateTest(name string, labels, annotations map[string]string, filePath string) *v1.Pod {
  317. pod := downwardAPIVolumeBasePod(name, labels, annotations)
  318. pod.Spec.Containers = []v1.Container{
  319. {
  320. Name: "client-container",
  321. Image: mountImage,
  322. Command: []string{"/mounttest", "--break_on_expected_content=false", "--retry_time=120", "--file_content_in_loop=" + filePath},
  323. VolumeMounts: []v1.VolumeMount{
  324. {
  325. Name: "podinfo",
  326. MountPath: "/etc/podinfo",
  327. ReadOnly: false,
  328. },
  329. },
  330. },
  331. }
  332. applyLabelsAndAnnotationsToDownwardAPIPod(labels, annotations, pod)
  333. return pod
  334. }
  335. func downwardAPIVolumeBasePod(name string, labels, annotations map[string]string) *v1.Pod {
  336. pod := &v1.Pod{
  337. ObjectMeta: metav1.ObjectMeta{
  338. Name: name,
  339. Labels: labels,
  340. Annotations: annotations,
  341. },
  342. Spec: v1.PodSpec{
  343. Volumes: []v1.Volume{
  344. {
  345. Name: "podinfo",
  346. VolumeSource: v1.VolumeSource{
  347. DownwardAPI: &v1.DownwardAPIVolumeSource{
  348. Items: []v1.DownwardAPIVolumeFile{
  349. {
  350. Path: "podname",
  351. FieldRef: &v1.ObjectFieldSelector{
  352. APIVersion: "v1",
  353. FieldPath: "metadata.name",
  354. },
  355. },
  356. {
  357. Path: "cpu_limit",
  358. ResourceFieldRef: &v1.ResourceFieldSelector{
  359. ContainerName: "client-container",
  360. Resource: "limits.cpu",
  361. },
  362. },
  363. {
  364. Path: "cpu_request",
  365. ResourceFieldRef: &v1.ResourceFieldSelector{
  366. ContainerName: "client-container",
  367. Resource: "requests.cpu",
  368. },
  369. },
  370. {
  371. Path: "memory_limit",
  372. ResourceFieldRef: &v1.ResourceFieldSelector{
  373. ContainerName: "client-container",
  374. Resource: "limits.memory",
  375. },
  376. },
  377. {
  378. Path: "memory_request",
  379. ResourceFieldRef: &v1.ResourceFieldSelector{
  380. ContainerName: "client-container",
  381. Resource: "requests.memory",
  382. },
  383. },
  384. },
  385. },
  386. },
  387. },
  388. },
  389. RestartPolicy: v1.RestartPolicyNever,
  390. },
  391. }
  392. return pod
  393. }
  394. func applyLabelsAndAnnotationsToDownwardAPIPod(labels, annotations map[string]string, pod *v1.Pod) {
  395. if len(labels) > 0 {
  396. pod.Spec.Volumes[0].DownwardAPI.Items = append(pod.Spec.Volumes[0].DownwardAPI.Items, v1.DownwardAPIVolumeFile{
  397. Path: "labels",
  398. FieldRef: &v1.ObjectFieldSelector{
  399. APIVersion: "v1",
  400. FieldPath: "metadata.labels",
  401. },
  402. })
  403. }
  404. if len(annotations) > 0 {
  405. pod.Spec.Volumes[0].DownwardAPI.Items = append(pod.Spec.Volumes[0].DownwardAPI.Items, v1.DownwardAPIVolumeFile{
  406. Path: "annotations",
  407. FieldRef: &v1.ObjectFieldSelector{
  408. APIVersion: "v1",
  409. FieldPath: "metadata.annotations",
  410. },
  411. })
  412. }
  413. }
  414. // TODO: add test-webserver example as pointed out in https://github.com/kubernetes/kubernetes/pull/5093#discussion-diff-37606771