expansion.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/uuid"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  23. imageutils "k8s.io/kubernetes/test/utils/image"
  24. . "github.com/onsi/ginkgo"
  25. . "github.com/onsi/gomega"
  26. )
  27. // These tests exercise the Kubernetes expansion syntax $(VAR).
  28. // For more information, see:
  29. // https://github.com/kubernetes/community/blob/master/contributors/design-proposals/node/expansion.md
  30. var _ = framework.KubeDescribe("Variable Expansion", func() {
  31. f := framework.NewDefaultFramework("var-expansion")
  32. /*
  33. Release : v1.9
  34. Testname: Environment variables, expansion
  35. Description: Create a Pod with environment variables. Environment variables defined using previously defined environment variables MUST expand to proper values.
  36. */
  37. framework.ConformanceIt("should allow composing env vars into new env vars [NodeConformance]", func() {
  38. podName := "var-expansion-" + string(uuid.NewUUID())
  39. pod := &v1.Pod{
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: podName,
  42. Labels: map[string]string{"name": podName},
  43. },
  44. Spec: v1.PodSpec{
  45. Containers: []v1.Container{
  46. {
  47. Name: "dapi-container",
  48. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  49. Command: []string{"sh", "-c", "env"},
  50. Env: []v1.EnvVar{
  51. {
  52. Name: "FOO",
  53. Value: "foo-value",
  54. },
  55. {
  56. Name: "BAR",
  57. Value: "bar-value",
  58. },
  59. {
  60. Name: "FOOBAR",
  61. Value: "$(FOO);;$(BAR)",
  62. },
  63. },
  64. },
  65. },
  66. RestartPolicy: v1.RestartPolicyNever,
  67. },
  68. }
  69. f.TestContainerOutput("env composition", pod, 0, []string{
  70. "FOO=foo-value",
  71. "BAR=bar-value",
  72. "FOOBAR=foo-value;;bar-value",
  73. })
  74. })
  75. /*
  76. Release : v1.9
  77. Testname: Environment variables, command expansion
  78. Description: Create a Pod with environment variables and container command using them. Container command using the defined environment variables MUST expand to proper values.
  79. */
  80. framework.ConformanceIt("should allow substituting values in a container's command [NodeConformance]", func() {
  81. podName := "var-expansion-" + string(uuid.NewUUID())
  82. pod := &v1.Pod{
  83. ObjectMeta: metav1.ObjectMeta{
  84. Name: podName,
  85. Labels: map[string]string{"name": podName},
  86. },
  87. Spec: v1.PodSpec{
  88. Containers: []v1.Container{
  89. {
  90. Name: "dapi-container",
  91. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  92. Command: []string{"sh", "-c", "TEST_VAR=wrong echo \"$(TEST_VAR)\""},
  93. Env: []v1.EnvVar{
  94. {
  95. Name: "TEST_VAR",
  96. Value: "test-value",
  97. },
  98. },
  99. },
  100. },
  101. RestartPolicy: v1.RestartPolicyNever,
  102. },
  103. }
  104. f.TestContainerOutput("substitution in container's command", pod, 0, []string{
  105. "test-value",
  106. })
  107. })
  108. /*
  109. Release : v1.9
  110. Testname: Environment variables, command argument expansion
  111. Description: Create a Pod with environment variables and container command arguments using them. Container command arguments using the defined environment variables MUST expand to proper values.
  112. */
  113. framework.ConformanceIt("should allow substituting values in a container's args [NodeConformance]", func() {
  114. podName := "var-expansion-" + string(uuid.NewUUID())
  115. pod := &v1.Pod{
  116. ObjectMeta: metav1.ObjectMeta{
  117. Name: podName,
  118. Labels: map[string]string{"name": podName},
  119. },
  120. Spec: v1.PodSpec{
  121. Containers: []v1.Container{
  122. {
  123. Name: "dapi-container",
  124. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  125. Command: []string{"sh", "-c"},
  126. Args: []string{"TEST_VAR=wrong echo \"$(TEST_VAR)\""},
  127. Env: []v1.EnvVar{
  128. {
  129. Name: "TEST_VAR",
  130. Value: "test-value",
  131. },
  132. },
  133. },
  134. },
  135. RestartPolicy: v1.RestartPolicyNever,
  136. },
  137. }
  138. f.TestContainerOutput("substitution in container's args", pod, 0, []string{
  139. "test-value",
  140. })
  141. })
  142. /*
  143. Testname: var-expansion-subpath
  144. Description: Make sure a container's subpath can be set using an
  145. expansion of environment variables.
  146. */
  147. It("should allow substituting values in a volume subpath [sig-storage][NodeFeature:VolumeSubpathEnvExpansion]", func() {
  148. podName := "var-expansion-" + string(uuid.NewUUID())
  149. pod := &v1.Pod{
  150. ObjectMeta: metav1.ObjectMeta{
  151. Name: podName,
  152. Labels: map[string]string{"name": podName},
  153. },
  154. Spec: v1.PodSpec{
  155. Containers: []v1.Container{
  156. {
  157. Name: "dapi-container",
  158. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  159. Command: []string{"sh", "-c", "test -d /testcontainer/" + podName + ";echo $?"},
  160. Env: []v1.EnvVar{
  161. {
  162. Name: "POD_NAME",
  163. Value: podName,
  164. },
  165. },
  166. VolumeMounts: []v1.VolumeMount{
  167. {
  168. Name: "workdir1",
  169. MountPath: "/logscontainer",
  170. SubPathExpr: "$(POD_NAME)",
  171. },
  172. {
  173. Name: "workdir2",
  174. MountPath: "/testcontainer",
  175. },
  176. },
  177. },
  178. },
  179. RestartPolicy: v1.RestartPolicyNever,
  180. Volumes: []v1.Volume{
  181. {
  182. Name: "workdir1",
  183. VolumeSource: v1.VolumeSource{
  184. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  185. },
  186. },
  187. {
  188. Name: "workdir2",
  189. VolumeSource: v1.VolumeSource{
  190. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  191. },
  192. },
  193. },
  194. },
  195. }
  196. f.TestContainerOutput("substitution in volume subpath", pod, 0, []string{
  197. "0",
  198. })
  199. })
  200. /*
  201. Testname: var-expansion-subpath-with-backticks
  202. Description: Make sure a container's subpath can not be set using an
  203. expansion of environment variables when backticks are supplied.
  204. */
  205. It("should fail substituting values in a volume subpath with backticks [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]", func() {
  206. podName := "var-expansion-" + string(uuid.NewUUID())
  207. pod := &v1.Pod{
  208. ObjectMeta: metav1.ObjectMeta{
  209. Name: podName,
  210. Labels: map[string]string{"name": podName},
  211. },
  212. Spec: v1.PodSpec{
  213. Containers: []v1.Container{
  214. {
  215. Name: "dapi-container",
  216. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  217. Env: []v1.EnvVar{
  218. {
  219. Name: "POD_NAME",
  220. Value: "..",
  221. },
  222. },
  223. VolumeMounts: []v1.VolumeMount{
  224. {
  225. Name: "workdir1",
  226. MountPath: "/logscontainer",
  227. SubPathExpr: "$(POD_NAME)",
  228. },
  229. },
  230. },
  231. },
  232. RestartPolicy: v1.RestartPolicyNever,
  233. Volumes: []v1.Volume{
  234. {
  235. Name: "workdir1",
  236. VolumeSource: v1.VolumeSource{
  237. EmptyDir: &v1.EmptyDirVolumeSource{},
  238. },
  239. },
  240. },
  241. },
  242. }
  243. // Pod should fail
  244. testPodFailSubpath(f, pod)
  245. })
  246. /*
  247. Testname: var-expansion-subpath-with-absolute-path
  248. Description: Make sure a container's subpath can not be set using an
  249. expansion of environment variables when absolute path is supplied.
  250. */
  251. It("should fail substituting values in a volume subpath with absolute path [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]", func() {
  252. podName := "var-expansion-" + string(uuid.NewUUID())
  253. pod := &v1.Pod{
  254. ObjectMeta: metav1.ObjectMeta{
  255. Name: podName,
  256. Labels: map[string]string{"name": podName},
  257. },
  258. Spec: v1.PodSpec{
  259. Containers: []v1.Container{
  260. {
  261. Name: "dapi-container",
  262. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  263. Env: []v1.EnvVar{
  264. {
  265. Name: "POD_NAME",
  266. Value: "/tmp",
  267. },
  268. },
  269. VolumeMounts: []v1.VolumeMount{
  270. {
  271. Name: "workdir1",
  272. MountPath: "/logscontainer",
  273. SubPathExpr: "$(POD_NAME)",
  274. },
  275. },
  276. },
  277. },
  278. RestartPolicy: v1.RestartPolicyNever,
  279. Volumes: []v1.Volume{
  280. {
  281. Name: "workdir1",
  282. VolumeSource: v1.VolumeSource{
  283. EmptyDir: &v1.EmptyDirVolumeSource{},
  284. },
  285. },
  286. },
  287. },
  288. }
  289. // Pod should fail
  290. testPodFailSubpath(f, pod)
  291. })
  292. /*
  293. Testname: var-expansion-subpath-ready-from-failed-state
  294. Description: Verify that a failing subpath expansion can be modified during the lifecycle of a container.
  295. */
  296. It("should verify that a failing subpath expansion can be modified during the lifecycle of a container [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]", func() {
  297. podName := "var-expansion-" + string(uuid.NewUUID())
  298. containerName := "dapi-container"
  299. pod := &v1.Pod{
  300. ObjectMeta: metav1.ObjectMeta{
  301. Name: podName,
  302. Labels: map[string]string{"name": podName},
  303. Annotations: map[string]string{"notmysubpath": "mypath"},
  304. },
  305. Spec: v1.PodSpec{
  306. Containers: []v1.Container{
  307. {
  308. Name: containerName,
  309. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  310. Command: []string{"sh", "-c", "tail -f /dev/null"},
  311. Env: []v1.EnvVar{
  312. {
  313. Name: "POD_NAME",
  314. Value: "foo",
  315. },
  316. {
  317. Name: "ANNOTATION",
  318. ValueFrom: &v1.EnvVarSource{
  319. FieldRef: &v1.ObjectFieldSelector{
  320. APIVersion: "v1",
  321. FieldPath: "metadata.annotations['mysubpath']",
  322. },
  323. },
  324. },
  325. },
  326. VolumeMounts: []v1.VolumeMount{
  327. {
  328. Name: "workdir1",
  329. MountPath: "/subpath_mount",
  330. SubPathExpr: "$(ANNOTATION)/$(POD_NAME)",
  331. },
  332. {
  333. Name: "workdir2",
  334. MountPath: "/volume_mount",
  335. },
  336. },
  337. },
  338. },
  339. Volumes: []v1.Volume{
  340. {
  341. Name: "workdir1",
  342. VolumeSource: v1.VolumeSource{
  343. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  344. },
  345. },
  346. {
  347. Name: "workdir2",
  348. VolumeSource: v1.VolumeSource{
  349. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  350. },
  351. },
  352. },
  353. },
  354. }
  355. By("creating the pod with failed condition")
  356. var podClient *framework.PodClient
  357. podClient = f.PodClient()
  358. pod = podClient.Create(pod)
  359. err := framework.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  360. framework.ExpectError(err, "while waiting for pod to be running")
  361. By("updating the pod")
  362. podClient.Update(podName, func(pod *v1.Pod) {
  363. pod.ObjectMeta.Annotations = map[string]string{"mysubpath": "mypath"}
  364. })
  365. By("waiting for pod running")
  366. err = framework.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  367. framework.ExpectNoError(err, "while waiting for pod to be running")
  368. By("deleting the pod gracefully")
  369. err = framework.DeletePodWithWait(f, f.ClientSet, pod)
  370. framework.ExpectNoError(err, "failed to delete pod")
  371. })
  372. /*
  373. Testname: var-expansion-subpath-test-writes
  374. Description: Verify that a subpath expansion can be used to write files into subpaths.
  375. 1. valid subpathexpr starts a container running
  376. 2. test for valid subpath writes
  377. 3. successful expansion of the subpathexpr isn't required for volume cleanup
  378. */
  379. It("should succeed in writing subpaths in container [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]", func() {
  380. podName := "var-expansion-" + string(uuid.NewUUID())
  381. containerName := "dapi-container"
  382. pod := &v1.Pod{
  383. ObjectMeta: metav1.ObjectMeta{
  384. Name: podName,
  385. Labels: map[string]string{"name": podName},
  386. Annotations: map[string]string{"mysubpath": "mypath"},
  387. },
  388. Spec: v1.PodSpec{
  389. Containers: []v1.Container{
  390. {
  391. Name: containerName,
  392. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  393. Command: []string{"sh", "-c", "tail -f /dev/null"},
  394. Env: []v1.EnvVar{
  395. {
  396. Name: "POD_NAME",
  397. Value: "foo",
  398. },
  399. {
  400. Name: "ANNOTATION",
  401. ValueFrom: &v1.EnvVarSource{
  402. FieldRef: &v1.ObjectFieldSelector{
  403. APIVersion: "v1",
  404. FieldPath: "metadata.annotations['mysubpath']",
  405. },
  406. },
  407. },
  408. },
  409. VolumeMounts: []v1.VolumeMount{
  410. {
  411. Name: "workdir1",
  412. MountPath: "/subpath_mount",
  413. SubPathExpr: "$(ANNOTATION)/$(POD_NAME)",
  414. },
  415. {
  416. Name: "workdir2",
  417. MountPath: "/volume_mount",
  418. },
  419. },
  420. },
  421. },
  422. RestartPolicy: v1.RestartPolicyNever,
  423. Volumes: []v1.Volume{
  424. {
  425. Name: "workdir1",
  426. VolumeSource: v1.VolumeSource{
  427. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  428. },
  429. },
  430. {
  431. Name: "workdir2",
  432. VolumeSource: v1.VolumeSource{
  433. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  434. },
  435. },
  436. },
  437. },
  438. }
  439. By("creating the pod")
  440. var podClient *framework.PodClient
  441. podClient = f.PodClient()
  442. pod = podClient.Create(pod)
  443. By("waiting for pod running")
  444. err := framework.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  445. framework.ExpectNoError(err, "while waiting for pod to be running")
  446. By("creating a file in subpath")
  447. cmd := "touch /volume_mount/mypath/foo/test.log"
  448. _, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
  449. if err != nil {
  450. framework.Failf("expected to be able to write to subpath")
  451. }
  452. By("test for file in mounted path")
  453. cmd = "test -f /subpath_mount/test.log"
  454. _, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
  455. if err != nil {
  456. framework.Failf("expected to be able to verify file")
  457. }
  458. By("updating the annotation value")
  459. podClient.Update(podName, func(pod *v1.Pod) {
  460. pod.ObjectMeta.Annotations["mysubpath"] = "mynewpath"
  461. })
  462. By("waiting for annotated pod running")
  463. err = framework.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  464. framework.ExpectNoError(err, "while waiting for annotated pod to be running")
  465. By("deleting the pod gracefully")
  466. err = framework.DeletePodWithWait(f, f.ClientSet, pod)
  467. framework.ExpectNoError(err, "failed to delete pod")
  468. })
  469. /*
  470. Testname: var-expansion-subpath-lifecycle
  471. Description: Verify should not change the subpath mount on a container restart if the environment variable changes
  472. 1. valid subpathexpr starts a container running
  473. 2. test for valid subpath writes
  474. 3. container restarts
  475. 4. delete cleanly
  476. */
  477. It("should not change the subpath mount on a container restart if the environment variable changes [sig-storage][NodeFeature:VolumeSubpathEnvExpansion][Slow]", func() {
  478. suffix := string(uuid.NewUUID())
  479. podName := fmt.Sprintf("var-expansion-%s", suffix)
  480. containerName := "dapi-container"
  481. pod := &v1.Pod{
  482. ObjectMeta: metav1.ObjectMeta{
  483. Name: podName,
  484. Labels: map[string]string{"name": podName},
  485. Annotations: map[string]string{"mysubpath": "foo"},
  486. },
  487. Spec: v1.PodSpec{
  488. InitContainers: []v1.Container{
  489. {
  490. Name: fmt.Sprintf("init-volume-%s", suffix),
  491. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  492. Command: []string{"sh", "-c", "mkdir -p /volume_mount/foo; touch /volume_mount/foo/test.log"},
  493. VolumeMounts: []v1.VolumeMount{
  494. {
  495. Name: "workdir1",
  496. MountPath: "/subpath_mount",
  497. },
  498. {
  499. Name: "workdir2",
  500. MountPath: "/volume_mount",
  501. },
  502. },
  503. },
  504. },
  505. Containers: []v1.Container{
  506. {
  507. Name: containerName,
  508. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  509. Command: []string{"/bin/sh", "-ec", "sleep 100000"},
  510. Env: []v1.EnvVar{
  511. {
  512. Name: "POD_NAME",
  513. ValueFrom: &v1.EnvVarSource{
  514. FieldRef: &v1.ObjectFieldSelector{
  515. APIVersion: "v1",
  516. FieldPath: "metadata.annotations['mysubpath']",
  517. },
  518. },
  519. },
  520. },
  521. VolumeMounts: []v1.VolumeMount{
  522. {
  523. Name: "workdir1",
  524. MountPath: "/subpath_mount",
  525. SubPathExpr: "$(POD_NAME)",
  526. },
  527. {
  528. Name: "workdir2",
  529. MountPath: "/volume_mount",
  530. },
  531. },
  532. },
  533. },
  534. RestartPolicy: v1.RestartPolicyOnFailure,
  535. Volumes: []v1.Volume{
  536. {
  537. Name: "workdir1",
  538. VolumeSource: v1.VolumeSource{
  539. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  540. },
  541. },
  542. {
  543. Name: "workdir2",
  544. VolumeSource: v1.VolumeSource{
  545. HostPath: &v1.HostPathVolumeSource{Path: "/tmp"},
  546. },
  547. },
  548. },
  549. },
  550. }
  551. // Add liveness probe to subpath container
  552. pod.Spec.Containers[0].LivenessProbe = &v1.Probe{
  553. Handler: v1.Handler{
  554. Exec: &v1.ExecAction{
  555. Command: []string{"cat", "/subpath_mount/test.log"},
  556. },
  557. },
  558. InitialDelaySeconds: 1,
  559. FailureThreshold: 1,
  560. PeriodSeconds: 2,
  561. }
  562. // Start pod
  563. By(fmt.Sprintf("Creating pod %s", pod.Name))
  564. var podClient *framework.PodClient
  565. podClient = f.PodClient()
  566. pod = podClient.Create(pod)
  567. defer func() {
  568. framework.DeletePodWithWait(f, f.ClientSet, pod)
  569. }()
  570. err := framework.WaitForPodRunningInNamespace(f.ClientSet, pod)
  571. Expect(err).ToNot(HaveOccurred(), "while waiting for pod to be running")
  572. By("updating the pod")
  573. podClient.Update(podName, func(pod *v1.Pod) {
  574. pod.ObjectMeta.Annotations = map[string]string{"mysubpath": "newsubpath"}
  575. })
  576. By("waiting for pod and container restart")
  577. waitForPodContainerRestart(f, pod, "/volume_mount/foo/test.log")
  578. By("test for subpath mounted with old value")
  579. cmd := "test -f /volume_mount/foo/test.log"
  580. _, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
  581. if err != nil {
  582. framework.Failf("expected to be able to verify old file exists")
  583. }
  584. cmd = "test ! -f /volume_mount/newsubpath/test.log"
  585. _, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
  586. if err != nil {
  587. framework.Failf("expected to be able to verify new file does not exist")
  588. }
  589. })
  590. })
  591. func testPodFailSubpath(f *framework.Framework, pod *v1.Pod) {
  592. var podClient *framework.PodClient
  593. podClient = f.PodClient()
  594. pod = podClient.Create(pod)
  595. defer func() {
  596. framework.DeletePodWithWait(f, f.ClientSet, pod)
  597. }()
  598. err := framework.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  599. framework.ExpectError(err, "while waiting for pod to be running")
  600. }
  601. // Tests that the existing subpath mount is detected when a container restarts
  602. func waitForPodContainerRestart(f *framework.Framework, pod *v1.Pod, volumeMount string) {
  603. By("Failing liveness probe")
  604. stdout, stderr, err := f.ExecShellInPodWithFullOutput(pod.Name, fmt.Sprintf("rm %v", volumeMount))
  605. e2elog.Logf("Pod exec output: %v / %v", stdout, stderr)
  606. Expect(err).ToNot(HaveOccurred(), "while failing liveness probe")
  607. // Check that container has restarted
  608. By("Waiting for container to restart")
  609. restarts := int32(0)
  610. err = wait.PollImmediate(10*time.Second, 2*time.Minute, func() (bool, error) {
  611. pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(pod.Name, metav1.GetOptions{})
  612. if err != nil {
  613. return false, err
  614. }
  615. for _, status := range pod.Status.ContainerStatuses {
  616. if status.Name == pod.Spec.Containers[0].Name {
  617. e2elog.Logf("Container %v, restarts: %v", status.Name, status.RestartCount)
  618. restarts = status.RestartCount
  619. if restarts > 0 {
  620. e2elog.Logf("Container has restart count: %v", restarts)
  621. return true, nil
  622. }
  623. }
  624. }
  625. return false, nil
  626. })
  627. Expect(err).ToNot(HaveOccurred(), "while waiting for container to restart")
  628. // Fix liveness probe
  629. By("Rewriting the file")
  630. stdout, _, err = f.ExecShellInPodWithFullOutput(pod.Name, fmt.Sprintf("echo test-after > %v", volumeMount))
  631. e2elog.Logf("Pod exec output: %v", stdout)
  632. Expect(err).ToNot(HaveOccurred(), "while rewriting the probe file")
  633. // Wait for container restarts to stabilize
  634. By("Waiting for container to stop restarting")
  635. stableCount := int(0)
  636. stableThreshold := int(time.Minute / framework.Poll)
  637. err = wait.PollImmediate(framework.Poll, 2*time.Minute, func() (bool, error) {
  638. pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(pod.Name, metav1.GetOptions{})
  639. if err != nil {
  640. return false, err
  641. }
  642. for _, status := range pod.Status.ContainerStatuses {
  643. if status.Name == pod.Spec.Containers[0].Name {
  644. if status.RestartCount == restarts {
  645. stableCount++
  646. if stableCount > stableThreshold {
  647. e2elog.Logf("Container restart has stabilized")
  648. return true, nil
  649. }
  650. } else {
  651. restarts = status.RestartCount
  652. stableCount = 0
  653. e2elog.Logf("Container has restart count: %v", restarts)
  654. }
  655. break
  656. }
  657. }
  658. return false, nil
  659. })
  660. Expect(err).ToNot(HaveOccurred(), "while waiting for container to stabilize")
  661. }