startup_probe_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright 2019 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 e2enode
  14. import (
  15. "context"
  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/kubernetes/pkg/features"
  21. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  22. "k8s.io/kubernetes/test/e2e/common"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. testutils "k8s.io/kubernetes/test/utils"
  25. imageutils "k8s.io/kubernetes/test/utils/image"
  26. "github.com/onsi/ginkgo"
  27. )
  28. const (
  29. defaultObservationTimeout = time.Minute * 4
  30. )
  31. var _ = framework.KubeDescribe("StartupProbe [Serial] [Disruptive]", func() {
  32. f := framework.NewDefaultFramework("startup-probe-test")
  33. var podClient *framework.PodClient
  34. ginkgo.BeforeEach(func() {
  35. podClient = f.PodClient()
  36. })
  37. /*
  38. These tests are located here as they require tempSetCurrentKubeletConfig to enable the feature gate for startupProbe.
  39. Once the feature gate has been removed, these tests should come back to test/e2e/common/container_probe.go.
  40. */
  41. ginkgo.Context("when a container has a startup probe", func() {
  42. tempSetCurrentKubeletConfig(f, func(initialConfig *kubeletconfig.KubeletConfiguration) {
  43. if initialConfig.FeatureGates == nil {
  44. initialConfig.FeatureGates = make(map[string]bool)
  45. }
  46. initialConfig.FeatureGates[string(features.StartupProbe)] = true
  47. })
  48. /*
  49. Release : v1.16
  50. Testname: Pod startup probe restart
  51. Description: A Pod is created with a failing startup probe. The Pod MUST be killed and restarted incrementing restart count to 1, even if liveness would succeed.
  52. */
  53. ginkgo.It("should be restarted startup probe fails", func() {
  54. cmd := []string{"/bin/sh", "-c", "sleep 600"}
  55. livenessProbe := &v1.Probe{
  56. Handler: v1.Handler{
  57. Exec: &v1.ExecAction{
  58. Command: []string{"/bin/true"},
  59. },
  60. },
  61. InitialDelaySeconds: 15,
  62. FailureThreshold: 1,
  63. }
  64. startupProbe := &v1.Probe{
  65. Handler: v1.Handler{
  66. Exec: &v1.ExecAction{
  67. Command: []string{"/bin/false"},
  68. },
  69. },
  70. InitialDelaySeconds: 15,
  71. FailureThreshold: 3,
  72. }
  73. pod := startupPodSpec(startupProbe, nil, livenessProbe, cmd)
  74. common.RunLivenessTest(f, pod, 1, defaultObservationTimeout)
  75. })
  76. /*
  77. Release : v1.16
  78. Testname: Pod liveness probe delayed (long) by startup probe
  79. Description: A Pod is created with failing liveness and startup probes. Liveness probe MUST NOT fail until startup probe expires.
  80. */
  81. ginkgo.It("should *not* be restarted by liveness probe because startup probe delays it", func() {
  82. cmd := []string{"/bin/sh", "-c", "sleep 600"}
  83. livenessProbe := &v1.Probe{
  84. Handler: v1.Handler{
  85. Exec: &v1.ExecAction{
  86. Command: []string{"/bin/false"},
  87. },
  88. },
  89. InitialDelaySeconds: 15,
  90. FailureThreshold: 1,
  91. }
  92. startupProbe := &v1.Probe{
  93. Handler: v1.Handler{
  94. Exec: &v1.ExecAction{
  95. Command: []string{"/bin/false"},
  96. },
  97. },
  98. InitialDelaySeconds: 15,
  99. FailureThreshold: 60,
  100. }
  101. pod := startupPodSpec(startupProbe, nil, livenessProbe, cmd)
  102. common.RunLivenessTest(f, pod, 0, defaultObservationTimeout)
  103. })
  104. /*
  105. Release : v1.16
  106. Testname: Pod liveness probe fails after startup success
  107. Description: A Pod is created with failing liveness probe and delayed startup probe that uses 'exec' command to cat /temp/health file. The Container is started by creating /tmp/startup after 10 seconds, triggering liveness probe to fail. The Pod MUST now be killed and restarted incrementing restart count to 1.
  108. */
  109. ginkgo.It("should be restarted by liveness probe after startup probe enables it", func() {
  110. cmd := []string{"/bin/sh", "-c", "sleep 10; echo ok >/tmp/startup; sleep 600"}
  111. livenessProbe := &v1.Probe{
  112. Handler: v1.Handler{
  113. Exec: &v1.ExecAction{
  114. Command: []string{"/bin/false"},
  115. },
  116. },
  117. InitialDelaySeconds: 15,
  118. FailureThreshold: 1,
  119. }
  120. startupProbe := &v1.Probe{
  121. Handler: v1.Handler{
  122. Exec: &v1.ExecAction{
  123. Command: []string{"cat", "/tmp/startup"},
  124. },
  125. },
  126. InitialDelaySeconds: 15,
  127. FailureThreshold: 60,
  128. }
  129. pod := startupPodSpec(startupProbe, nil, livenessProbe, cmd)
  130. common.RunLivenessTest(f, pod, 1, defaultObservationTimeout)
  131. })
  132. /*
  133. Release : v1.16
  134. Testname: Pod readiness probe, delayed by startup probe
  135. Description: A Pod is created with startup and readiness probes. The Container is started by creating /tmp/startup after 45 seconds, delaying the ready state by this amount of time. This is similar to the "Pod readiness probe, with initial delay" test.
  136. */
  137. ginkgo.It("should not be ready until startupProbe succeeds", func() {
  138. cmd := []string{"/bin/sh", "-c", "echo ok >/tmp/health; sleep 45; echo ok >/tmp/startup; sleep 600"}
  139. readinessProbe := &v1.Probe{
  140. Handler: v1.Handler{
  141. Exec: &v1.ExecAction{
  142. Command: []string{"cat", "/tmp/health"},
  143. },
  144. },
  145. InitialDelaySeconds: 0,
  146. }
  147. startupProbe := &v1.Probe{
  148. Handler: v1.Handler{
  149. Exec: &v1.ExecAction{
  150. Command: []string{"cat", "/tmp/startup"},
  151. },
  152. },
  153. InitialDelaySeconds: 0,
  154. FailureThreshold: 60,
  155. }
  156. p := podClient.Create(startupPodSpec(startupProbe, readinessProbe, nil, cmd))
  157. p, err := podClient.Get(context.TODO(), p.Name, metav1.GetOptions{})
  158. framework.ExpectNoError(err)
  159. f.WaitForPodReady(p.Name)
  160. p, err = podClient.Get(context.TODO(), p.Name, metav1.GetOptions{})
  161. framework.ExpectNoError(err)
  162. isReady, err := testutils.PodRunningReady(p)
  163. framework.ExpectNoError(err)
  164. framework.ExpectEqual(isReady, true, "pod should be ready")
  165. // We assume the pod became ready when the container became ready. This
  166. // is true for a single container pod.
  167. readyTime, err := common.GetTransitionTimeForReadyCondition(p)
  168. framework.ExpectNoError(err)
  169. startedTime, err := common.GetContainerStartedTime(p, "busybox")
  170. framework.ExpectNoError(err)
  171. framework.Logf("Container started at %v, pod became ready at %v", startedTime, readyTime)
  172. if readyTime.Sub(startedTime) < 40*time.Second {
  173. framework.Failf("Pod became ready before startupProbe succeeded")
  174. }
  175. })
  176. })
  177. })
  178. func startupPodSpec(startupProbe, readinessProbe, livenessProbe *v1.Probe, cmd []string) *v1.Pod {
  179. return &v1.Pod{
  180. ObjectMeta: metav1.ObjectMeta{
  181. Name: "startup-" + string(uuid.NewUUID()),
  182. Labels: map[string]string{"test": "startup"},
  183. },
  184. Spec: v1.PodSpec{
  185. Containers: []v1.Container{
  186. {
  187. Name: "busybox",
  188. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  189. Command: cmd,
  190. LivenessProbe: livenessProbe,
  191. ReadinessProbe: readinessProbe,
  192. StartupProbe: startupProbe,
  193. },
  194. },
  195. },
  196. }
  197. }