expansion.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. "context"
  16. "fmt"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/uuid"
  21. "k8s.io/apimachinery/pkg/util/wait"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  24. imageutils "k8s.io/kubernetes/test/utils/image"
  25. "github.com/onsi/ginkgo"
  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. ginkgo.It("should allow substituting values in a volume subpath [sig-storage]", 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: "workdir1",
  174. MountPath: "/testcontainer",
  175. },
  176. },
  177. },
  178. },
  179. RestartPolicy: v1.RestartPolicyNever,
  180. Volumes: []v1.Volume{
  181. {
  182. Name: "workdir1",
  183. VolumeSource: v1.VolumeSource{
  184. EmptyDir: &v1.EmptyDirVolumeSource{},
  185. },
  186. },
  187. },
  188. },
  189. }
  190. f.TestContainerOutput("substitution in volume subpath", pod, 0, []string{
  191. "0",
  192. })
  193. })
  194. /*
  195. Testname: var-expansion-subpath-with-backticks
  196. Description: Make sure a container's subpath can not be set using an
  197. expansion of environment variables when backticks are supplied.
  198. */
  199. ginkgo.It("should fail substituting values in a volume subpath with backticks [sig-storage][Slow]", func() {
  200. podName := "var-expansion-" + string(uuid.NewUUID())
  201. pod := &v1.Pod{
  202. ObjectMeta: metav1.ObjectMeta{
  203. Name: podName,
  204. Labels: map[string]string{"name": podName},
  205. },
  206. Spec: v1.PodSpec{
  207. Containers: []v1.Container{
  208. {
  209. Name: "dapi-container",
  210. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  211. Env: []v1.EnvVar{
  212. {
  213. Name: "POD_NAME",
  214. Value: "..",
  215. },
  216. },
  217. VolumeMounts: []v1.VolumeMount{
  218. {
  219. Name: "workdir1",
  220. MountPath: "/logscontainer",
  221. SubPathExpr: "$(POD_NAME)",
  222. },
  223. },
  224. },
  225. },
  226. RestartPolicy: v1.RestartPolicyNever,
  227. Volumes: []v1.Volume{
  228. {
  229. Name: "workdir1",
  230. VolumeSource: v1.VolumeSource{
  231. EmptyDir: &v1.EmptyDirVolumeSource{},
  232. },
  233. },
  234. },
  235. },
  236. }
  237. // Pod should fail
  238. testPodFailSubpath(f, pod)
  239. })
  240. /*
  241. Testname: var-expansion-subpath-with-absolute-path
  242. Description: Make sure a container's subpath can not be set using an
  243. expansion of environment variables when absolute path is supplied.
  244. */
  245. ginkgo.It("should fail substituting values in a volume subpath with absolute path [sig-storage][Slow]", func() {
  246. podName := "var-expansion-" + string(uuid.NewUUID())
  247. pod := &v1.Pod{
  248. ObjectMeta: metav1.ObjectMeta{
  249. Name: podName,
  250. Labels: map[string]string{"name": podName},
  251. },
  252. Spec: v1.PodSpec{
  253. Containers: []v1.Container{
  254. {
  255. Name: "dapi-container",
  256. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  257. Env: []v1.EnvVar{
  258. {
  259. Name: "POD_NAME",
  260. Value: "/tmp",
  261. },
  262. },
  263. VolumeMounts: []v1.VolumeMount{
  264. {
  265. Name: "workdir1",
  266. MountPath: "/logscontainer",
  267. SubPathExpr: "$(POD_NAME)",
  268. },
  269. },
  270. },
  271. },
  272. RestartPolicy: v1.RestartPolicyNever,
  273. Volumes: []v1.Volume{
  274. {
  275. Name: "workdir1",
  276. VolumeSource: v1.VolumeSource{
  277. EmptyDir: &v1.EmptyDirVolumeSource{},
  278. },
  279. },
  280. },
  281. },
  282. }
  283. // Pod should fail
  284. testPodFailSubpath(f, pod)
  285. })
  286. /*
  287. Testname: var-expansion-subpath-ready-from-failed-state
  288. Description: Verify that a failing subpath expansion can be modified during the lifecycle of a container.
  289. */
  290. ginkgo.It("should verify that a failing subpath expansion can be modified during the lifecycle of a container [sig-storage][Slow]", func() {
  291. podName := "var-expansion-" + string(uuid.NewUUID())
  292. containerName := "dapi-container"
  293. pod := &v1.Pod{
  294. ObjectMeta: metav1.ObjectMeta{
  295. Name: podName,
  296. Labels: map[string]string{"name": podName},
  297. Annotations: map[string]string{"notmysubpath": "mypath"},
  298. },
  299. Spec: v1.PodSpec{
  300. Containers: []v1.Container{
  301. {
  302. Name: containerName,
  303. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  304. Command: []string{"sh", "-c", "tail -f /dev/null"},
  305. Env: []v1.EnvVar{
  306. {
  307. Name: "POD_NAME",
  308. Value: "foo",
  309. },
  310. {
  311. Name: "ANNOTATION",
  312. ValueFrom: &v1.EnvVarSource{
  313. FieldRef: &v1.ObjectFieldSelector{
  314. APIVersion: "v1",
  315. FieldPath: "metadata.annotations['mysubpath']",
  316. },
  317. },
  318. },
  319. },
  320. VolumeMounts: []v1.VolumeMount{
  321. {
  322. Name: "workdir1",
  323. MountPath: "/subpath_mount",
  324. SubPathExpr: "$(ANNOTATION)/$(POD_NAME)",
  325. },
  326. {
  327. Name: "workdir1",
  328. MountPath: "/volume_mount",
  329. },
  330. },
  331. },
  332. },
  333. Volumes: []v1.Volume{
  334. {
  335. Name: "workdir1",
  336. VolumeSource: v1.VolumeSource{
  337. EmptyDir: &v1.EmptyDirVolumeSource{},
  338. },
  339. },
  340. },
  341. },
  342. }
  343. ginkgo.By("creating the pod with failed condition")
  344. var podClient *framework.PodClient = f.PodClient()
  345. pod = podClient.Create(pod)
  346. err := e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  347. framework.ExpectError(err, "while waiting for pod to be running")
  348. ginkgo.By("updating the pod")
  349. podClient.Update(podName, func(pod *v1.Pod) {
  350. pod.ObjectMeta.Annotations = map[string]string{"mysubpath": "mypath"}
  351. })
  352. ginkgo.By("waiting for pod running")
  353. err = e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  354. framework.ExpectNoError(err, "while waiting for pod to be running")
  355. ginkgo.By("deleting the pod gracefully")
  356. err = e2epod.DeletePodWithWait(f.ClientSet, pod)
  357. framework.ExpectNoError(err, "failed to delete pod")
  358. })
  359. /*
  360. Testname: var-expansion-subpath-test-writes
  361. Description: Verify that a subpath expansion can be used to write files into subpaths.
  362. 1. valid subpathexpr starts a container running
  363. 2. test for valid subpath writes
  364. 3. successful expansion of the subpathexpr isn't required for volume cleanup
  365. */
  366. ginkgo.It("should succeed in writing subpaths in container [sig-storage][Slow]", func() {
  367. podName := "var-expansion-" + string(uuid.NewUUID())
  368. containerName := "dapi-container"
  369. pod := &v1.Pod{
  370. ObjectMeta: metav1.ObjectMeta{
  371. Name: podName,
  372. Labels: map[string]string{"name": podName},
  373. Annotations: map[string]string{"mysubpath": "mypath"},
  374. },
  375. Spec: v1.PodSpec{
  376. Containers: []v1.Container{
  377. {
  378. Name: containerName,
  379. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  380. Command: []string{"sh", "-c", "tail -f /dev/null"},
  381. Env: []v1.EnvVar{
  382. {
  383. Name: "POD_NAME",
  384. Value: "foo",
  385. },
  386. {
  387. Name: "ANNOTATION",
  388. ValueFrom: &v1.EnvVarSource{
  389. FieldRef: &v1.ObjectFieldSelector{
  390. APIVersion: "v1",
  391. FieldPath: "metadata.annotations['mysubpath']",
  392. },
  393. },
  394. },
  395. },
  396. VolumeMounts: []v1.VolumeMount{
  397. {
  398. Name: "workdir1",
  399. MountPath: "/subpath_mount",
  400. SubPathExpr: "$(ANNOTATION)/$(POD_NAME)",
  401. },
  402. {
  403. Name: "workdir1",
  404. MountPath: "/volume_mount",
  405. },
  406. },
  407. },
  408. },
  409. RestartPolicy: v1.RestartPolicyNever,
  410. Volumes: []v1.Volume{
  411. {
  412. Name: "workdir1",
  413. VolumeSource: v1.VolumeSource{
  414. EmptyDir: &v1.EmptyDirVolumeSource{},
  415. },
  416. },
  417. },
  418. },
  419. }
  420. ginkgo.By("creating the pod")
  421. var podClient *framework.PodClient = f.PodClient()
  422. pod = podClient.Create(pod)
  423. ginkgo.By("waiting for pod running")
  424. err := e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  425. framework.ExpectNoError(err, "while waiting for pod to be running")
  426. ginkgo.By("creating a file in subpath")
  427. cmd := "touch /volume_mount/mypath/foo/test.log"
  428. _, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
  429. if err != nil {
  430. framework.Failf("expected to be able to write to subpath")
  431. }
  432. ginkgo.By("test for file in mounted path")
  433. cmd = "test -f /subpath_mount/test.log"
  434. _, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
  435. if err != nil {
  436. framework.Failf("expected to be able to verify file")
  437. }
  438. ginkgo.By("updating the annotation value")
  439. podClient.Update(podName, func(pod *v1.Pod) {
  440. pod.ObjectMeta.Annotations["mysubpath"] = "mynewpath"
  441. })
  442. ginkgo.By("waiting for annotated pod running")
  443. err = e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  444. framework.ExpectNoError(err, "while waiting for annotated pod to be running")
  445. ginkgo.By("deleting the pod gracefully")
  446. err = e2epod.DeletePodWithWait(f.ClientSet, pod)
  447. framework.ExpectNoError(err, "failed to delete pod")
  448. })
  449. /*
  450. Testname: var-expansion-subpath-lifecycle
  451. Description: Verify should not change the subpath mount on a container restart if the environment variable changes
  452. 1. valid subpathexpr starts a container running
  453. 2. test for valid subpath writes
  454. 3. container restarts
  455. 4. delete cleanly
  456. */
  457. ginkgo.It("should not change the subpath mount on a container restart if the environment variable changes [sig-storage][Slow]", func() {
  458. suffix := string(uuid.NewUUID())
  459. podName := fmt.Sprintf("var-expansion-%s", suffix)
  460. containerName := "dapi-container"
  461. pod := &v1.Pod{
  462. ObjectMeta: metav1.ObjectMeta{
  463. Name: podName,
  464. Labels: map[string]string{"name": podName},
  465. Annotations: map[string]string{"mysubpath": "foo"},
  466. },
  467. Spec: v1.PodSpec{
  468. InitContainers: []v1.Container{
  469. {
  470. Name: fmt.Sprintf("init-volume-%s", suffix),
  471. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  472. Command: []string{"sh", "-c", "mkdir -p /volume_mount/foo; touch /volume_mount/foo/test.log"},
  473. VolumeMounts: []v1.VolumeMount{
  474. {
  475. Name: "workdir1",
  476. MountPath: "/subpath_mount",
  477. },
  478. {
  479. Name: "workdir1",
  480. MountPath: "/volume_mount",
  481. },
  482. },
  483. },
  484. },
  485. Containers: []v1.Container{
  486. {
  487. Name: containerName,
  488. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  489. Command: []string{"/bin/sh", "-ec", "sleep 100000"},
  490. Env: []v1.EnvVar{
  491. {
  492. Name: "POD_NAME",
  493. ValueFrom: &v1.EnvVarSource{
  494. FieldRef: &v1.ObjectFieldSelector{
  495. APIVersion: "v1",
  496. FieldPath: "metadata.annotations['mysubpath']",
  497. },
  498. },
  499. },
  500. },
  501. VolumeMounts: []v1.VolumeMount{
  502. {
  503. Name: "workdir1",
  504. MountPath: "/subpath_mount",
  505. SubPathExpr: "$(POD_NAME)",
  506. },
  507. {
  508. Name: "workdir1",
  509. MountPath: "/volume_mount",
  510. },
  511. },
  512. },
  513. },
  514. RestartPolicy: v1.RestartPolicyOnFailure,
  515. Volumes: []v1.Volume{
  516. {
  517. Name: "workdir1",
  518. VolumeSource: v1.VolumeSource{
  519. EmptyDir: &v1.EmptyDirVolumeSource{},
  520. },
  521. },
  522. },
  523. },
  524. }
  525. // Add liveness probe to subpath container
  526. pod.Spec.Containers[0].LivenessProbe = &v1.Probe{
  527. Handler: v1.Handler{
  528. Exec: &v1.ExecAction{
  529. Command: []string{"cat", "/subpath_mount/test.log"},
  530. },
  531. },
  532. InitialDelaySeconds: 1,
  533. FailureThreshold: 1,
  534. PeriodSeconds: 2,
  535. }
  536. // Start pod
  537. ginkgo.By(fmt.Sprintf("Creating pod %s", pod.Name))
  538. var podClient *framework.PodClient = f.PodClient()
  539. pod = podClient.Create(pod)
  540. defer func() {
  541. e2epod.DeletePodWithWait(f.ClientSet, pod)
  542. }()
  543. err := e2epod.WaitForPodRunningInNamespace(f.ClientSet, pod)
  544. framework.ExpectNoError(err, "while waiting for pod to be running")
  545. ginkgo.By("updating the pod")
  546. podClient.Update(podName, func(pod *v1.Pod) {
  547. pod.ObjectMeta.Annotations = map[string]string{"mysubpath": "newsubpath"}
  548. })
  549. ginkgo.By("waiting for pod and container restart")
  550. waitForPodContainerRestart(f, pod, "/volume_mount/foo/test.log")
  551. ginkgo.By("test for subpath mounted with old value")
  552. cmd := "test -f /volume_mount/foo/test.log"
  553. _, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
  554. if err != nil {
  555. framework.Failf("expected to be able to verify old file exists")
  556. }
  557. cmd = "test ! -f /volume_mount/newsubpath/test.log"
  558. _, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
  559. if err != nil {
  560. framework.Failf("expected to be able to verify new file does not exist")
  561. }
  562. })
  563. })
  564. func testPodFailSubpath(f *framework.Framework, pod *v1.Pod) {
  565. var podClient *framework.PodClient = f.PodClient()
  566. pod = podClient.Create(pod)
  567. defer func() {
  568. e2epod.DeletePodWithWait(f.ClientSet, pod)
  569. }()
  570. err := e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
  571. framework.ExpectError(err, "while waiting for pod to be running")
  572. }
  573. // Tests that the existing subpath mount is detected when a container restarts
  574. func waitForPodContainerRestart(f *framework.Framework, pod *v1.Pod, volumeMount string) {
  575. ginkgo.By("Failing liveness probe")
  576. stdout, stderr, err := f.ExecShellInPodWithFullOutput(pod.Name, fmt.Sprintf("rm %v", volumeMount))
  577. framework.Logf("Pod exec output: %v / %v", stdout, stderr)
  578. framework.ExpectNoError(err, "while failing liveness probe")
  579. // Check that container has restarted
  580. ginkgo.By("Waiting for container to restart")
  581. restarts := int32(0)
  582. err = wait.PollImmediate(10*time.Second, 2*time.Minute, func() (bool, error) {
  583. pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(context.TODO(), pod.Name, metav1.GetOptions{})
  584. if err != nil {
  585. return false, err
  586. }
  587. for _, status := range pod.Status.ContainerStatuses {
  588. if status.Name == pod.Spec.Containers[0].Name {
  589. framework.Logf("Container %v, restarts: %v", status.Name, status.RestartCount)
  590. restarts = status.RestartCount
  591. if restarts > 0 {
  592. framework.Logf("Container has restart count: %v", restarts)
  593. return true, nil
  594. }
  595. }
  596. }
  597. return false, nil
  598. })
  599. framework.ExpectNoError(err, "while waiting for container to restart")
  600. // Fix liveness probe
  601. ginkgo.By("Rewriting the file")
  602. stdout, _, err = f.ExecShellInPodWithFullOutput(pod.Name, fmt.Sprintf("echo test-after > %v", volumeMount))
  603. framework.Logf("Pod exec output: %v", stdout)
  604. framework.ExpectNoError(err, "while rewriting the probe file")
  605. // Wait for container restarts to stabilize
  606. ginkgo.By("Waiting for container to stop restarting")
  607. stableCount := int(0)
  608. stableThreshold := int(time.Minute / framework.Poll)
  609. err = wait.PollImmediate(framework.Poll, 2*time.Minute, func() (bool, error) {
  610. pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(context.TODO(), pod.Name, metav1.GetOptions{})
  611. if err != nil {
  612. return false, err
  613. }
  614. for _, status := range pod.Status.ContainerStatuses {
  615. if status.Name == pod.Spec.Containers[0].Name {
  616. if status.RestartCount == restarts {
  617. stableCount++
  618. if stableCount > stableThreshold {
  619. framework.Logf("Container restart has stabilized")
  620. return true, nil
  621. }
  622. } else {
  623. restarts = status.RestartCount
  624. stableCount = 0
  625. framework.Logf("Container has restart count: %v", restarts)
  626. }
  627. break
  628. }
  629. }
  630. return false, nil
  631. })
  632. framework.ExpectNoError(err, "while waiting for container to stabilize")
  633. }