lifecycle_hook.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "strings"
  17. "time"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/intstr"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. imageutils "k8s.io/kubernetes/test/utils/image"
  23. "github.com/onsi/ginkgo"
  24. "github.com/onsi/gomega"
  25. )
  26. var _ = framework.KubeDescribe("Container Lifecycle Hook", func() {
  27. f := framework.NewDefaultFramework("container-lifecycle-hook")
  28. var podClient *framework.PodClient
  29. const (
  30. podCheckInterval = 1 * time.Second
  31. postStartWaitTimeout = 2 * time.Minute
  32. preStopWaitTimeout = 30 * time.Second
  33. )
  34. ginkgo.Context("when create a pod with lifecycle hook", func() {
  35. var targetIP, targetURL string
  36. podHandleHookRequest := &v1.Pod{
  37. ObjectMeta: metav1.ObjectMeta{
  38. Name: "pod-handle-http-request",
  39. },
  40. Spec: v1.PodSpec{
  41. Containers: []v1.Container{
  42. {
  43. Name: "pod-handle-http-request",
  44. Image: imageutils.GetE2EImage(imageutils.Agnhost),
  45. Args: []string{"netexec"},
  46. Ports: []v1.ContainerPort{
  47. {
  48. ContainerPort: 8080,
  49. Protocol: v1.ProtocolTCP,
  50. },
  51. },
  52. },
  53. },
  54. },
  55. }
  56. ginkgo.BeforeEach(func() {
  57. podClient = f.PodClient()
  58. ginkgo.By("create the container to handle the HTTPGet hook request.")
  59. newPod := podClient.CreateSync(podHandleHookRequest)
  60. targetIP = newPod.Status.PodIP
  61. targetURL = targetIP
  62. if strings.Contains(targetIP, ":") {
  63. targetURL = fmt.Sprintf("[%s]", targetIP)
  64. }
  65. })
  66. testPodWithHook := func(podWithHook *v1.Pod) {
  67. ginkgo.By("create the pod with lifecycle hook")
  68. podClient.CreateSync(podWithHook)
  69. if podWithHook.Spec.Containers[0].Lifecycle.PostStart != nil {
  70. ginkgo.By("check poststart hook")
  71. gomega.Eventually(func() error {
  72. return podClient.MatchContainerOutput(podHandleHookRequest.Name, podHandleHookRequest.Spec.Containers[0].Name,
  73. `GET /echo\?msg=poststart`)
  74. }, postStartWaitTimeout, podCheckInterval).Should(gomega.BeNil())
  75. }
  76. ginkgo.By("delete the pod with lifecycle hook")
  77. podClient.DeleteSync(podWithHook.Name, metav1.NewDeleteOptions(15), framework.DefaultPodDeletionTimeout)
  78. if podWithHook.Spec.Containers[0].Lifecycle.PreStop != nil {
  79. ginkgo.By("check prestop hook")
  80. gomega.Eventually(func() error {
  81. return podClient.MatchContainerOutput(podHandleHookRequest.Name, podHandleHookRequest.Spec.Containers[0].Name,
  82. `GET /echo\?msg=prestop`)
  83. }, preStopWaitTimeout, podCheckInterval).Should(gomega.BeNil())
  84. }
  85. }
  86. /*
  87. Release : v1.9
  88. Testname: Pod Lifecycle, post start exec hook
  89. Description: When a post start handler is specified in the container lifecycle using a 'Exec' action, then the handler MUST be invoked after the start of the container. A server pod is created that will serve http requests, create a second pod with a container lifecycle specifying a post start that invokes the server pod using ExecAction to validate that the post start is executed.
  90. */
  91. framework.ConformanceIt("should execute poststart exec hook properly [NodeConformance]", func() {
  92. lifecycle := &v1.Lifecycle{
  93. PostStart: &v1.Handler{
  94. Exec: &v1.ExecAction{
  95. Command: []string{"sh", "-c", "curl http://" + targetURL + ":8080/echo?msg=poststart"},
  96. },
  97. },
  98. }
  99. podWithHook := getPodWithHook("pod-with-poststart-exec-hook", imageutils.GetE2EImage(imageutils.Agnhost), lifecycle)
  100. testPodWithHook(podWithHook)
  101. })
  102. /*
  103. Release : v1.9
  104. Testname: Pod Lifecycle, prestop exec hook
  105. Description: When a pre-stop handler is specified in the container lifecycle using a 'Exec' action, then the handler MUST be invoked before the container is terminated. A server pod is created that will serve http requests, create a second pod with a container lifecycle specifying a pre-stop that invokes the server pod using ExecAction to validate that the pre-stop is executed.
  106. */
  107. framework.ConformanceIt("should execute prestop exec hook properly [NodeConformance]", func() {
  108. lifecycle := &v1.Lifecycle{
  109. PreStop: &v1.Handler{
  110. Exec: &v1.ExecAction{
  111. Command: []string{"sh", "-c", "curl http://" + targetURL + ":8080/echo?msg=prestop"},
  112. },
  113. },
  114. }
  115. podWithHook := getPodWithHook("pod-with-prestop-exec-hook", imageutils.GetE2EImage(imageutils.Agnhost), lifecycle)
  116. testPodWithHook(podWithHook)
  117. })
  118. /*
  119. Release : v1.9
  120. Testname: Pod Lifecycle, post start http hook
  121. Description: When a post start handler is specified in the container lifecycle using a HttpGet action, then the handler MUST be invoked after the start of the container. A server pod is created that will serve http requests, create a second pod with a container lifecycle specifying a post start that invokes the server pod to validate that the post start is executed.
  122. */
  123. framework.ConformanceIt("should execute poststart http hook properly [NodeConformance]", func() {
  124. lifecycle := &v1.Lifecycle{
  125. PostStart: &v1.Handler{
  126. HTTPGet: &v1.HTTPGetAction{
  127. Path: "/echo?msg=poststart",
  128. Host: targetIP,
  129. Port: intstr.FromInt(8080),
  130. },
  131. },
  132. }
  133. podWithHook := getPodWithHook("pod-with-poststart-http-hook", imageutils.GetPauseImageName(), lifecycle)
  134. testPodWithHook(podWithHook)
  135. })
  136. /*
  137. Release : v1.9
  138. Testname: Pod Lifecycle, prestop http hook
  139. Description: When a pre-stop handler is specified in the container lifecycle using a 'HttpGet' action, then the handler MUST be invoked before the container is terminated. A server pod is created that will serve http requests, create a second pod with a container lifecycle specifying a pre-stop that invokes the server pod to validate that the pre-stop is executed.
  140. */
  141. framework.ConformanceIt("should execute prestop http hook properly [NodeConformance]", func() {
  142. lifecycle := &v1.Lifecycle{
  143. PreStop: &v1.Handler{
  144. HTTPGet: &v1.HTTPGetAction{
  145. Path: "/echo?msg=prestop",
  146. Host: targetIP,
  147. Port: intstr.FromInt(8080),
  148. },
  149. },
  150. }
  151. podWithHook := getPodWithHook("pod-with-prestop-http-hook", imageutils.GetPauseImageName(), lifecycle)
  152. testPodWithHook(podWithHook)
  153. })
  154. })
  155. })
  156. func getPodWithHook(name string, image string, lifecycle *v1.Lifecycle) *v1.Pod {
  157. return &v1.Pod{
  158. ObjectMeta: metav1.ObjectMeta{
  159. Name: name,
  160. },
  161. Spec: v1.PodSpec{
  162. Containers: []v1.Container{
  163. {
  164. Name: name,
  165. Image: image,
  166. Lifecycle: lifecycle,
  167. },
  168. },
  169. },
  170. }
  171. }