PageRenderTime 87ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 1ms

/test/e2e/common/downward_api.go

https://gitlab.com/unofficial-mirrors/kubernetes
Go | 382 lines | 310 code | 31 blank | 41 comment | 0 complexity | 41bca2452020a0a62d03f3f9c92059b6 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. "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. utilversion "k8s.io/kubernetes/pkg/util/version"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. . "github.com/onsi/ginkgo"
  23. )
  24. var (
  25. hostIPVersion = utilversion.MustParseSemantic("v1.8.0")
  26. podUIDVersion = utilversion.MustParseSemantic("v1.8.0")
  27. )
  28. var _ = Describe("[sig-api-machinery] Downward API", func() {
  29. f := framework.NewDefaultFramework("downward-api")
  30. /*
  31. Testname: downwardapi-env-name-namespace-podip
  32. Description: Ensure that downward API can provide pod's name, namespace
  33. and IP address as environment variables.
  34. */
  35. framework.ConformanceIt("should provide pod name, namespace and IP address as env vars ", func() {
  36. podName := "downward-api-" + string(uuid.NewUUID())
  37. env := []v1.EnvVar{
  38. {
  39. Name: "POD_NAME",
  40. ValueFrom: &v1.EnvVarSource{
  41. FieldRef: &v1.ObjectFieldSelector{
  42. APIVersion: "v1",
  43. FieldPath: "metadata.name",
  44. },
  45. },
  46. },
  47. {
  48. Name: "POD_NAMESPACE",
  49. ValueFrom: &v1.EnvVarSource{
  50. FieldRef: &v1.ObjectFieldSelector{
  51. APIVersion: "v1",
  52. FieldPath: "metadata.namespace",
  53. },
  54. },
  55. },
  56. {
  57. Name: "POD_IP",
  58. ValueFrom: &v1.EnvVarSource{
  59. FieldRef: &v1.ObjectFieldSelector{
  60. APIVersion: "v1",
  61. FieldPath: "status.podIP",
  62. },
  63. },
  64. },
  65. }
  66. expectations := []string{
  67. fmt.Sprintf("POD_NAME=%v", podName),
  68. fmt.Sprintf("POD_NAMESPACE=%v", f.Namespace.Name),
  69. "POD_IP=(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)",
  70. }
  71. testDownwardAPI(f, podName, env, expectations)
  72. })
  73. /*
  74. Testname: downwardapi-env-host-ip
  75. Description: Ensure that downward API can provide an IP address for
  76. host node as an environment variable.
  77. */
  78. framework.ConformanceIt("should provide host IP as an env var ", func() {
  79. framework.SkipUnlessServerVersionGTE(hostIPVersion, f.ClientSet.Discovery())
  80. podName := "downward-api-" + string(uuid.NewUUID())
  81. env := []v1.EnvVar{
  82. {
  83. Name: "HOST_IP",
  84. ValueFrom: &v1.EnvVarSource{
  85. FieldRef: &v1.ObjectFieldSelector{
  86. APIVersion: "v1",
  87. FieldPath: "status.hostIP",
  88. },
  89. },
  90. },
  91. }
  92. expectations := []string{
  93. "HOST_IP=(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)\\.(?:\\d+)",
  94. }
  95. testDownwardAPI(f, podName, env, expectations)
  96. })
  97. /*
  98. Testname: downwardapi-env-limits-requests
  99. Description: Ensure that downward API can provide CPU/memory limit
  100. and CPU/memory request as environment variables.
  101. */
  102. framework.ConformanceIt("should provide container's limits.cpu/memory and requests.cpu/memory as env vars ", func() {
  103. podName := "downward-api-" + string(uuid.NewUUID())
  104. env := []v1.EnvVar{
  105. {
  106. Name: "CPU_LIMIT",
  107. ValueFrom: &v1.EnvVarSource{
  108. ResourceFieldRef: &v1.ResourceFieldSelector{
  109. Resource: "limits.cpu",
  110. },
  111. },
  112. },
  113. {
  114. Name: "MEMORY_LIMIT",
  115. ValueFrom: &v1.EnvVarSource{
  116. ResourceFieldRef: &v1.ResourceFieldSelector{
  117. Resource: "limits.memory",
  118. },
  119. },
  120. },
  121. {
  122. Name: "CPU_REQUEST",
  123. ValueFrom: &v1.EnvVarSource{
  124. ResourceFieldRef: &v1.ResourceFieldSelector{
  125. Resource: "requests.cpu",
  126. },
  127. },
  128. },
  129. {
  130. Name: "MEMORY_REQUEST",
  131. ValueFrom: &v1.EnvVarSource{
  132. ResourceFieldRef: &v1.ResourceFieldSelector{
  133. Resource: "requests.memory",
  134. },
  135. },
  136. },
  137. }
  138. expectations := []string{
  139. "CPU_LIMIT=2",
  140. "MEMORY_LIMIT=67108864",
  141. "CPU_REQUEST=1",
  142. "MEMORY_REQUEST=33554432",
  143. }
  144. testDownwardAPI(f, podName, env, expectations)
  145. })
  146. /*
  147. Testname: downwardapi-env-default-allocatable
  148. Description: Ensure that downward API can provide default node
  149. allocatable values for CPU and memory as environment variables if CPU
  150. and memory limits are not specified for a container.
  151. */
  152. framework.ConformanceIt("should provide default limits.cpu/memory from node allocatable ", func() {
  153. podName := "downward-api-" + string(uuid.NewUUID())
  154. env := []v1.EnvVar{
  155. {
  156. Name: "CPU_LIMIT",
  157. ValueFrom: &v1.EnvVarSource{
  158. ResourceFieldRef: &v1.ResourceFieldSelector{
  159. Resource: "limits.cpu",
  160. },
  161. },
  162. },
  163. {
  164. Name: "MEMORY_LIMIT",
  165. ValueFrom: &v1.EnvVarSource{
  166. ResourceFieldRef: &v1.ResourceFieldSelector{
  167. Resource: "limits.memory",
  168. },
  169. },
  170. },
  171. }
  172. expectations := []string{
  173. "CPU_LIMIT=[1-9]",
  174. "MEMORY_LIMIT=[1-9]",
  175. }
  176. pod := &v1.Pod{
  177. ObjectMeta: metav1.ObjectMeta{
  178. Name: podName,
  179. Labels: map[string]string{"name": podName},
  180. },
  181. Spec: v1.PodSpec{
  182. Containers: []v1.Container{
  183. {
  184. Name: "dapi-container",
  185. Image: busyboxImage,
  186. Command: []string{"sh", "-c", "env"},
  187. Env: env,
  188. },
  189. },
  190. RestartPolicy: v1.RestartPolicyNever,
  191. },
  192. }
  193. testDownwardAPIUsingPod(f, pod, env, expectations)
  194. })
  195. /*
  196. Testname: downwardapi-env-pod-uid
  197. Description: Ensure that downward API can provide pod UID as an
  198. environment variable.
  199. */
  200. framework.ConformanceIt("should provide pod UID as env vars ", func() {
  201. framework.SkipUnlessServerVersionGTE(podUIDVersion, f.ClientSet.Discovery())
  202. podName := "downward-api-" + string(uuid.NewUUID())
  203. env := []v1.EnvVar{
  204. {
  205. Name: "POD_UID",
  206. ValueFrom: &v1.EnvVarSource{
  207. FieldRef: &v1.ObjectFieldSelector{
  208. APIVersion: "v1",
  209. FieldPath: "metadata.uid",
  210. },
  211. },
  212. },
  213. }
  214. expectations := []string{
  215. "POD_UID=[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}",
  216. }
  217. testDownwardAPI(f, podName, env, expectations)
  218. })
  219. })
  220. var _ = framework.KubeDescribe("Downward API [Serial] [Disruptive]", func() {
  221. f := framework.NewDefaultFramework("downward-api")
  222. Context("Downward API tests for local ephemeral storage", func() {
  223. BeforeEach(func() {
  224. framework.SkipUnlessLocalEphemeralStorageEnabled()
  225. })
  226. It("should provide container's limits.ephemeral-storage and requests.ephemeral-storage as env vars", func() {
  227. podName := "downward-api-" + string(uuid.NewUUID())
  228. env := []v1.EnvVar{
  229. {
  230. Name: "EPHEMERAL_STORAGE_LIMIT",
  231. ValueFrom: &v1.EnvVarSource{
  232. ResourceFieldRef: &v1.ResourceFieldSelector{
  233. Resource: "limits.ephemeral-storage",
  234. },
  235. },
  236. },
  237. {
  238. Name: "EPHEMERAL_STORAGE_REQUEST",
  239. ValueFrom: &v1.EnvVarSource{
  240. ResourceFieldRef: &v1.ResourceFieldSelector{
  241. Resource: "requests.ephemeral-storage",
  242. },
  243. },
  244. },
  245. }
  246. expectations := []string{
  247. fmt.Sprintf("EPHEMERAL_STORAGE_LIMIT=%d", 64*1024*1024),
  248. fmt.Sprintf("EPHEMERAL_STORAGE_REQUEST=%d", 32*1024*1024),
  249. }
  250. testDownwardAPIForEphemeralStorage(f, podName, env, expectations)
  251. })
  252. It("should provide default limits.ephemeral-storage from node allocatable", func() {
  253. podName := "downward-api-" + string(uuid.NewUUID())
  254. env := []v1.EnvVar{
  255. {
  256. Name: "EPHEMERAL_STORAGE_LIMIT",
  257. ValueFrom: &v1.EnvVarSource{
  258. ResourceFieldRef: &v1.ResourceFieldSelector{
  259. Resource: "limits.ephemeral-storage",
  260. },
  261. },
  262. },
  263. }
  264. expectations := []string{
  265. "EPHEMERAL_STORAGE_LIMIT=[1-9]",
  266. }
  267. pod := &v1.Pod{
  268. ObjectMeta: metav1.ObjectMeta{
  269. Name: podName,
  270. Labels: map[string]string{"name": podName},
  271. },
  272. Spec: v1.PodSpec{
  273. Containers: []v1.Container{
  274. {
  275. Name: "dapi-container",
  276. Image: busyboxImage,
  277. Command: []string{"sh", "-c", "env"},
  278. Env: env,
  279. },
  280. },
  281. RestartPolicy: v1.RestartPolicyNever,
  282. },
  283. }
  284. testDownwardAPIUsingPod(f, pod, env, expectations)
  285. })
  286. })
  287. })
  288. func testDownwardAPI(f *framework.Framework, podName string, env []v1.EnvVar, expectations []string) {
  289. pod := &v1.Pod{
  290. ObjectMeta: metav1.ObjectMeta{
  291. Name: podName,
  292. Labels: map[string]string{"name": podName},
  293. },
  294. Spec: v1.PodSpec{
  295. Containers: []v1.Container{
  296. {
  297. Name: "dapi-container",
  298. Image: busyboxImage,
  299. Command: []string{"sh", "-c", "env"},
  300. Resources: v1.ResourceRequirements{
  301. Requests: v1.ResourceList{
  302. v1.ResourceCPU: resource.MustParse("250m"),
  303. v1.ResourceMemory: resource.MustParse("32Mi"),
  304. },
  305. Limits: v1.ResourceList{
  306. v1.ResourceCPU: resource.MustParse("1250m"),
  307. v1.ResourceMemory: resource.MustParse("64Mi"),
  308. },
  309. },
  310. Env: env,
  311. },
  312. },
  313. RestartPolicy: v1.RestartPolicyNever,
  314. },
  315. }
  316. testDownwardAPIUsingPod(f, pod, env, expectations)
  317. }
  318. func testDownwardAPIForEphemeralStorage(f *framework.Framework, podName string, env []v1.EnvVar, expectations []string) {
  319. pod := &v1.Pod{
  320. ObjectMeta: metav1.ObjectMeta{
  321. Name: podName,
  322. Labels: map[string]string{"name": podName},
  323. },
  324. Spec: v1.PodSpec{
  325. Containers: []v1.Container{
  326. {
  327. Name: "dapi-container",
  328. Image: busyboxImage,
  329. Command: []string{"sh", "-c", "env"},
  330. Resources: v1.ResourceRequirements{
  331. Requests: v1.ResourceList{
  332. v1.ResourceEphemeralStorage: resource.MustParse("32Mi"),
  333. },
  334. Limits: v1.ResourceList{
  335. v1.ResourceEphemeralStorage: resource.MustParse("64Mi"),
  336. },
  337. },
  338. Env: env,
  339. },
  340. },
  341. RestartPolicy: v1.RestartPolicyNever,
  342. },
  343. }
  344. testDownwardAPIUsingPod(f, pod, env, expectations)
  345. }
  346. func testDownwardAPIUsingPod(f *framework.Framework, pod *v1.Pod, env []v1.EnvVar, expectations []string) {
  347. f.TestContainerOutputRegexp("downward api env vars", pod, 0, expectations)
  348. }