PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/test/e2e/common/downwardapi_volume.go

https://gitlab.com/github-cloud-corporation/kubernetes
Go | 411 lines | 334 code | 56 blank | 21 comment | 8 complexity | de45f03fa434020866b12d2e7812de60 MD5 | raw file
  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/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/resource"
  19. "k8s.io/kubernetes/pkg/util/uuid"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. . "github.com/onsi/ginkgo"
  22. . "github.com/onsi/gomega"
  23. )
  24. var _ = framework.KubeDescribe("Downward API volume", func() {
  25. // How long to wait for a log pod to be displayed
  26. const podLogTimeout = 45 * time.Second
  27. f := framework.NewDefaultFramework("downward-api")
  28. var podClient *framework.PodClient
  29. BeforeEach(func() {
  30. podClient = f.PodClient()
  31. })
  32. It("should provide podname only [Conformance]", func() {
  33. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  34. pod := downwardAPIVolumePodForSimpleTest(podName, "/etc/podname")
  35. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  36. fmt.Sprintf("%s\n", podName),
  37. })
  38. })
  39. It("should set DefaultMode on files [Conformance]", func() {
  40. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  41. defaultMode := int32(0400)
  42. pod := downwardAPIVolumePodForModeTest(podName, "/etc/podname", nil, &defaultMode)
  43. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  44. "mode of file \"/etc/podname\": -r--------",
  45. })
  46. })
  47. It("should set mode on item file [Conformance]", func() {
  48. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  49. mode := int32(0400)
  50. pod := downwardAPIVolumePodForModeTest(podName, "/etc/podname", &mode, nil)
  51. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  52. "mode of file \"/etc/podname\": -r--------",
  53. })
  54. })
  55. It("should provide podname as non-root with fsgroup [Feature:FSGroup]", func() {
  56. podName := "metadata-volume-" + string(uuid.NewUUID())
  57. uid := int64(1001)
  58. gid := int64(1234)
  59. pod := downwardAPIVolumePodForSimpleTest(podName, "/etc/podname")
  60. pod.Spec.SecurityContext = &api.PodSecurityContext{
  61. RunAsUser: &uid,
  62. FSGroup: &gid,
  63. }
  64. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  65. fmt.Sprintf("%s\n", podName),
  66. })
  67. })
  68. // Mark the following 2 tests as [Flaky] because of https://github.com/kubernetes/kubernetes/issues/29633,
  69. // we should re-enable these tests when the issue is fixed.
  70. It("should update labels on modification [Conformance] [Flaky]", func() {
  71. labels := map[string]string{}
  72. labels["key1"] = "value1"
  73. labels["key2"] = "value2"
  74. podName := "labelsupdate" + string(uuid.NewUUID())
  75. pod := downwardAPIVolumePodForUpdateTest(podName, labels, map[string]string{}, "/etc/labels")
  76. containerName := "client-container"
  77. defer func() {
  78. By("Deleting the pod")
  79. podClient.Delete(pod.Name, api.NewDeleteOptions(0))
  80. }()
  81. By("Creating the pod")
  82. podClient.CreateSync(pod)
  83. Eventually(func() (string, error) {
  84. return framework.GetPodLogs(f.Client, f.Namespace.Name, podName, containerName)
  85. },
  86. podLogTimeout, framework.Poll).Should(ContainSubstring("key1=\"value1\"\n"))
  87. //modify labels
  88. podClient.Update(podName, func(pod *api.Pod) {
  89. pod.Labels["key3"] = "value3"
  90. })
  91. Eventually(func() (string, error) {
  92. return framework.GetPodLogs(f.Client, f.Namespace.Name, pod.Name, containerName)
  93. },
  94. podLogTimeout, framework.Poll).Should(ContainSubstring("key3=\"value3\"\n"))
  95. })
  96. It("should update annotations on modification [Conformance] [Flaky]", func() {
  97. annotations := map[string]string{}
  98. annotations["builder"] = "bar"
  99. podName := "annotationupdate" + string(uuid.NewUUID())
  100. pod := downwardAPIVolumePodForUpdateTest(podName, map[string]string{}, annotations, "/etc/annotations")
  101. containerName := "client-container"
  102. defer func() {
  103. By("Deleting the pod")
  104. podClient.Delete(pod.Name, api.NewDeleteOptions(0))
  105. }()
  106. By("Creating the pod")
  107. podClient.CreateSync(pod)
  108. pod, err := podClient.Get(pod.Name)
  109. Expect(err).NotTo(HaveOccurred())
  110. Eventually(func() (string, error) {
  111. return framework.GetPodLogs(f.Client, f.Namespace.Name, pod.Name, containerName)
  112. },
  113. podLogTimeout, framework.Poll).Should(ContainSubstring("builder=\"bar\"\n"))
  114. //modify annotations
  115. podClient.Update(podName, func(pod *api.Pod) {
  116. pod.Annotations["builder"] = "foo"
  117. })
  118. Eventually(func() (string, error) {
  119. return framework.GetPodLogs(f.Client, f.Namespace.Name, pod.Name, containerName)
  120. },
  121. podLogTimeout, framework.Poll).Should(ContainSubstring("builder=\"foo\"\n"))
  122. })
  123. It("should provide container's cpu limit", func() {
  124. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  125. pod := downwardAPIVolumeForContainerResources(podName, "/etc/cpu_limit")
  126. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  127. fmt.Sprintf("2\n"),
  128. })
  129. })
  130. It("should provide container's memory limit", func() {
  131. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  132. pod := downwardAPIVolumeForContainerResources(podName, "/etc/memory_limit")
  133. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  134. fmt.Sprintf("67108864\n"),
  135. })
  136. })
  137. It("should provide container's cpu request", func() {
  138. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  139. pod := downwardAPIVolumeForContainerResources(podName, "/etc/cpu_request")
  140. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  141. fmt.Sprintf("1\n"),
  142. })
  143. })
  144. It("should provide container's memory request", func() {
  145. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  146. pod := downwardAPIVolumeForContainerResources(podName, "/etc/memory_request")
  147. f.TestContainerOutput("downward API volume plugin", pod, 0, []string{
  148. fmt.Sprintf("33554432\n"),
  149. })
  150. })
  151. It("should provide node allocatable (cpu) as default cpu limit if the limit is not set", func() {
  152. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  153. pod := downwardAPIVolumeForDefaultContainerResources(podName, "/etc/cpu_limit")
  154. f.TestContainerOutputRegexp("downward API volume plugin", pod, 0, []string{"[1-9]"})
  155. })
  156. It("should provide node allocatable (memory) as default memory limit if the limit is not set", func() {
  157. podName := "downwardapi-volume-" + string(uuid.NewUUID())
  158. pod := downwardAPIVolumeForDefaultContainerResources(podName, "/etc/memory_limit")
  159. f.TestContainerOutputRegexp("downward API volume plugin", pod, 0, []string{"[1-9]"})
  160. })
  161. })
  162. func downwardAPIVolumePodForModeTest(name, filePath string, itemMode, defaultMode *int32) *api.Pod {
  163. pod := downwardAPIVolumeBasePod(name, nil, nil)
  164. pod.Spec.Containers = []api.Container{
  165. {
  166. Name: "client-container",
  167. Image: "gcr.io/google_containers/mounttest:0.7",
  168. Command: []string{"/mt", "--file_mode=" + filePath},
  169. VolumeMounts: []api.VolumeMount{
  170. {
  171. Name: "podinfo",
  172. MountPath: "/etc",
  173. },
  174. },
  175. },
  176. }
  177. if itemMode != nil {
  178. pod.Spec.Volumes[0].VolumeSource.DownwardAPI.Items[0].Mode = itemMode
  179. }
  180. if defaultMode != nil {
  181. pod.Spec.Volumes[0].VolumeSource.DownwardAPI.DefaultMode = defaultMode
  182. }
  183. return pod
  184. }
  185. func downwardAPIVolumePodForSimpleTest(name string, filePath string) *api.Pod {
  186. pod := downwardAPIVolumeBasePod(name, nil, nil)
  187. pod.Spec.Containers = []api.Container{
  188. {
  189. Name: "client-container",
  190. Image: "gcr.io/google_containers/mounttest:0.6",
  191. Command: []string{"/mt", "--file_content=" + filePath},
  192. VolumeMounts: []api.VolumeMount{
  193. {
  194. Name: "podinfo",
  195. MountPath: "/etc",
  196. ReadOnly: false,
  197. },
  198. },
  199. },
  200. }
  201. return pod
  202. }
  203. func downwardAPIVolumeForContainerResources(name string, filePath string) *api.Pod {
  204. pod := downwardAPIVolumeBasePod(name, nil, nil)
  205. pod.Spec.Containers = downwardAPIVolumeBaseContainers("client-container", filePath)
  206. return pod
  207. }
  208. func downwardAPIVolumeForDefaultContainerResources(name string, filePath string) *api.Pod {
  209. pod := downwardAPIVolumeBasePod(name, nil, nil)
  210. pod.Spec.Containers = downwardAPIVolumeDefaultBaseContainer("client-container", filePath)
  211. return pod
  212. }
  213. func downwardAPIVolumeBaseContainers(name, filePath string) []api.Container {
  214. return []api.Container{
  215. {
  216. Name: name,
  217. Image: "gcr.io/google_containers/mounttest:0.6",
  218. Command: []string{"/mt", "--file_content=" + filePath},
  219. Resources: api.ResourceRequirements{
  220. Requests: api.ResourceList{
  221. api.ResourceCPU: resource.MustParse("250m"),
  222. api.ResourceMemory: resource.MustParse("32Mi"),
  223. },
  224. Limits: api.ResourceList{
  225. api.ResourceCPU: resource.MustParse("1250m"),
  226. api.ResourceMemory: resource.MustParse("64Mi"),
  227. },
  228. },
  229. VolumeMounts: []api.VolumeMount{
  230. {
  231. Name: "podinfo",
  232. MountPath: "/etc",
  233. ReadOnly: false,
  234. },
  235. },
  236. },
  237. }
  238. }
  239. func downwardAPIVolumeDefaultBaseContainer(name, filePath string) []api.Container {
  240. return []api.Container{
  241. {
  242. Name: name,
  243. Image: "gcr.io/google_containers/mounttest:0.6",
  244. Command: []string{"/mt", "--file_content=" + filePath},
  245. VolumeMounts: []api.VolumeMount{
  246. {
  247. Name: "podinfo",
  248. MountPath: "/etc",
  249. },
  250. },
  251. },
  252. }
  253. }
  254. func downwardAPIVolumePodForUpdateTest(name string, labels, annotations map[string]string, filePath string) *api.Pod {
  255. pod := downwardAPIVolumeBasePod(name, labels, annotations)
  256. pod.Spec.Containers = []api.Container{
  257. {
  258. Name: "client-container",
  259. Image: "gcr.io/google_containers/mounttest:0.6",
  260. Command: []string{"/mt", "--break_on_expected_content=false", "--retry_time=120", "--file_content_in_loop=" + filePath},
  261. VolumeMounts: []api.VolumeMount{
  262. {
  263. Name: "podinfo",
  264. MountPath: "/etc",
  265. ReadOnly: false,
  266. },
  267. },
  268. },
  269. }
  270. applyLabelsAndAnnotationsToDownwardAPIPod(labels, annotations, pod)
  271. return pod
  272. }
  273. func downwardAPIVolumeBasePod(name string, labels, annotations map[string]string) *api.Pod {
  274. pod := &api.Pod{
  275. ObjectMeta: api.ObjectMeta{
  276. Name: name,
  277. Labels: labels,
  278. Annotations: annotations,
  279. },
  280. Spec: api.PodSpec{
  281. Volumes: []api.Volume{
  282. {
  283. Name: "podinfo",
  284. VolumeSource: api.VolumeSource{
  285. DownwardAPI: &api.DownwardAPIVolumeSource{
  286. Items: []api.DownwardAPIVolumeFile{
  287. {
  288. Path: "podname",
  289. FieldRef: &api.ObjectFieldSelector{
  290. APIVersion: "v1",
  291. FieldPath: "metadata.name",
  292. },
  293. },
  294. {
  295. Path: "cpu_limit",
  296. ResourceFieldRef: &api.ResourceFieldSelector{
  297. ContainerName: "client-container",
  298. Resource: "limits.cpu",
  299. },
  300. },
  301. {
  302. Path: "cpu_request",
  303. ResourceFieldRef: &api.ResourceFieldSelector{
  304. ContainerName: "client-container",
  305. Resource: "requests.cpu",
  306. },
  307. },
  308. {
  309. Path: "memory_limit",
  310. ResourceFieldRef: &api.ResourceFieldSelector{
  311. ContainerName: "client-container",
  312. Resource: "limits.memory",
  313. },
  314. },
  315. {
  316. Path: "memory_request",
  317. ResourceFieldRef: &api.ResourceFieldSelector{
  318. ContainerName: "client-container",
  319. Resource: "requests.memory",
  320. },
  321. },
  322. },
  323. },
  324. },
  325. },
  326. },
  327. RestartPolicy: api.RestartPolicyNever,
  328. },
  329. }
  330. return pod
  331. }
  332. func applyLabelsAndAnnotationsToDownwardAPIPod(labels, annotations map[string]string, pod *api.Pod) {
  333. if len(labels) > 0 {
  334. pod.Spec.Volumes[0].DownwardAPI.Items = append(pod.Spec.Volumes[0].DownwardAPI.Items, api.DownwardAPIVolumeFile{
  335. Path: "labels",
  336. FieldRef: &api.ObjectFieldSelector{
  337. APIVersion: "v1",
  338. FieldPath: "metadata.labels",
  339. },
  340. })
  341. }
  342. if len(annotations) > 0 {
  343. pod.Spec.Volumes[0].DownwardAPI.Items = append(pod.Spec.Volumes[0].DownwardAPI.Items, api.DownwardAPIVolumeFile{
  344. Path: "annotations",
  345. FieldRef: &api.ObjectFieldSelector{
  346. APIVersion: "v1",
  347. FieldPath: "metadata.annotations",
  348. },
  349. })
  350. }
  351. }
  352. // TODO: add test-webserver example as pointed out in https://github.com/kubernetes/kubernetes/pull/5093#discussion-diff-37606771