downward_api.go 11 KB

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