PageRenderTime 3448ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/k8s.io/kubernetes/test/e2e/downward_api.go

https://gitlab.com/github-cloud-corporation/bootkube
Go | 211 lines | 178 code | 18 blank | 15 comment | 0 complexity | c4423e8c69382f2e9df351c708ccd5cc MD5 | raw file
  1. /*
  2. Copyright 2015 The Kubernetes Authors All rights reserved.
  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 e2e
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/resource"
  18. "k8s.io/kubernetes/pkg/util"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. . "github.com/onsi/ginkgo"
  21. )
  22. var _ = framework.KubeDescribe("Downward API", func() {
  23. f := framework.NewDefaultFramework("downward-api")
  24. It("should provide pod name and namespace as env vars [Conformance]", func() {
  25. podName := "downward-api-" + string(util.NewUUID())
  26. env := []api.EnvVar{
  27. {
  28. Name: "POD_NAME",
  29. ValueFrom: &api.EnvVarSource{
  30. FieldRef: &api.ObjectFieldSelector{
  31. APIVersion: "v1",
  32. FieldPath: "metadata.name",
  33. },
  34. },
  35. },
  36. {
  37. Name: "POD_NAMESPACE",
  38. ValueFrom: &api.EnvVarSource{
  39. FieldRef: &api.ObjectFieldSelector{
  40. APIVersion: "v1",
  41. FieldPath: "metadata.namespace",
  42. },
  43. },
  44. },
  45. }
  46. expectations := []string{
  47. fmt.Sprintf("POD_NAME=%v", podName),
  48. fmt.Sprintf("POD_NAMESPACE=%v", f.Namespace.Name),
  49. }
  50. testDownwardAPI(f, podName, env, expectations)
  51. })
  52. It("should provide pod IP as an env var", func() {
  53. podName := "downward-api-" + string(util.NewUUID())
  54. env := []api.EnvVar{
  55. {
  56. Name: "POD_IP",
  57. ValueFrom: &api.EnvVarSource{
  58. FieldRef: &api.ObjectFieldSelector{
  59. APIVersion: "v1",
  60. FieldPath: "status.podIP",
  61. },
  62. },
  63. },
  64. }
  65. expectations := []string{
  66. "POD_IP=(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)",
  67. }
  68. testDownwardAPI(f, podName, env, expectations)
  69. })
  70. It("should provide container's limits.cpu/memory and requests.cpu/memory as env vars", func() {
  71. podName := "downward-api-" + string(util.NewUUID())
  72. env := []api.EnvVar{
  73. {
  74. Name: "CPU_LIMIT",
  75. ValueFrom: &api.EnvVarSource{
  76. ResourceFieldRef: &api.ResourceFieldSelector{
  77. Resource: "limits.cpu",
  78. },
  79. },
  80. },
  81. {
  82. Name: "MEMORY_LIMIT",
  83. ValueFrom: &api.EnvVarSource{
  84. ResourceFieldRef: &api.ResourceFieldSelector{
  85. Resource: "limits.memory",
  86. },
  87. },
  88. },
  89. {
  90. Name: "CPU_REQUEST",
  91. ValueFrom: &api.EnvVarSource{
  92. ResourceFieldRef: &api.ResourceFieldSelector{
  93. Resource: "requests.cpu",
  94. },
  95. },
  96. },
  97. {
  98. Name: "MEMORY_REQUEST",
  99. ValueFrom: &api.EnvVarSource{
  100. ResourceFieldRef: &api.ResourceFieldSelector{
  101. Resource: "requests.memory",
  102. },
  103. },
  104. },
  105. }
  106. expectations := []string{
  107. fmt.Sprintf("CPU_LIMIT=2"),
  108. fmt.Sprintf("MEMORY_LIMIT=67108864"),
  109. fmt.Sprintf("CPU_REQUEST=1"),
  110. fmt.Sprintf("MEMORY_REQUEST=33554432"),
  111. }
  112. testDownwardAPI(f, podName, env, expectations)
  113. })
  114. It("should provide default limits.cpu/memory from node capacity", func() {
  115. podName := "downward-api-" + string(util.NewUUID())
  116. env := []api.EnvVar{
  117. {
  118. Name: "CPU_LIMIT",
  119. ValueFrom: &api.EnvVarSource{
  120. ResourceFieldRef: &api.ResourceFieldSelector{
  121. Resource: "limits.cpu",
  122. },
  123. },
  124. },
  125. {
  126. Name: "MEMORY_LIMIT",
  127. ValueFrom: &api.EnvVarSource{
  128. ResourceFieldRef: &api.ResourceFieldSelector{
  129. Resource: "limits.memory",
  130. },
  131. },
  132. },
  133. }
  134. expectations := []string{
  135. fmt.Sprintf("CPU_LIMIT=[1-9]"),
  136. fmt.Sprintf("MEMORY_LIMIT=[1-9]"),
  137. }
  138. pod := &api.Pod{
  139. ObjectMeta: api.ObjectMeta{
  140. Name: podName,
  141. Labels: map[string]string{"name": podName},
  142. },
  143. Spec: api.PodSpec{
  144. Containers: []api.Container{
  145. {
  146. Name: "dapi-container",
  147. Image: "gcr.io/google_containers/busybox:1.24",
  148. Command: []string{"sh", "-c", "env"},
  149. Env: env,
  150. },
  151. },
  152. RestartPolicy: api.RestartPolicyNever,
  153. },
  154. }
  155. testDownwardAPIUsingPod(f, pod, env, expectations)
  156. })
  157. })
  158. func testDownwardAPI(f *framework.Framework, podName string, env []api.EnvVar, expectations []string) {
  159. pod := &api.Pod{
  160. ObjectMeta: api.ObjectMeta{
  161. Name: podName,
  162. Labels: map[string]string{"name": podName},
  163. },
  164. Spec: api.PodSpec{
  165. Containers: []api.Container{
  166. {
  167. Name: "dapi-container",
  168. Image: "gcr.io/google_containers/busybox:1.24",
  169. Command: []string{"sh", "-c", "env"},
  170. Resources: api.ResourceRequirements{
  171. Requests: api.ResourceList{
  172. api.ResourceCPU: resource.MustParse("250m"),
  173. api.ResourceMemory: resource.MustParse("32Mi"),
  174. },
  175. Limits: api.ResourceList{
  176. api.ResourceCPU: resource.MustParse("1250m"),
  177. api.ResourceMemory: resource.MustParse("64Mi"),
  178. },
  179. },
  180. Env: env,
  181. },
  182. },
  183. RestartPolicy: api.RestartPolicyNever,
  184. },
  185. }
  186. testDownwardAPIUsingPod(f, pod, env, expectations)
  187. }
  188. func testDownwardAPIUsingPod(f *framework.Framework, pod *api.Pod, env []api.EnvVar, expectations []string) {
  189. f.TestContainerOutputRegexp("downward api env vars", pod, 0, expectations)
  190. }