mirror_pod_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 e2enode
  14. import (
  15. "context"
  16. goerrors "errors"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "time"
  21. v1 "k8s.io/api/core/v1"
  22. apiequality "k8s.io/apimachinery/pkg/api/equality"
  23. apierrors "k8s.io/apimachinery/pkg/api/errors"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/types"
  26. "k8s.io/apimachinery/pkg/util/uuid"
  27. clientset "k8s.io/client-go/kubernetes"
  28. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  29. "k8s.io/kubernetes/test/e2e/framework"
  30. imageutils "k8s.io/kubernetes/test/utils/image"
  31. "github.com/google/go-cmp/cmp"
  32. "github.com/onsi/ginkgo"
  33. "github.com/onsi/gomega"
  34. )
  35. var _ = framework.KubeDescribe("MirrorPod", func() {
  36. f := framework.NewDefaultFramework("mirror-pod")
  37. ginkgo.Context("when create a mirror pod ", func() {
  38. var ns, podPath, staticPodName, mirrorPodName string
  39. ginkgo.BeforeEach(func() {
  40. ns = f.Namespace.Name
  41. staticPodName = "static-pod-" + string(uuid.NewUUID())
  42. mirrorPodName = staticPodName + "-" + framework.TestContext.NodeName
  43. podPath = framework.TestContext.KubeletConfig.StaticPodPath
  44. ginkgo.By("create the static pod")
  45. err := createStaticPod(podPath, staticPodName, ns,
  46. imageutils.GetE2EImage(imageutils.Nginx), v1.RestartPolicyAlways)
  47. framework.ExpectNoError(err)
  48. ginkgo.By("wait for the mirror pod to be running")
  49. gomega.Eventually(func() error {
  50. return checkMirrorPodRunning(f.ClientSet, mirrorPodName, ns)
  51. }, 2*time.Minute, time.Second*4).Should(gomega.BeNil())
  52. })
  53. /*
  54. Release : v1.9
  55. Testname: Mirror Pod, update
  56. Description: Updating a static Pod MUST recreate an updated mirror Pod. Create a static pod, verify that a mirror pod is created. Update the static pod by changing the container image, the mirror pod MUST be re-created and updated with the new image.
  57. */
  58. ginkgo.It("should be updated when static pod updated [NodeConformance]", func() {
  59. ginkgo.By("get mirror pod uid")
  60. pod, err := f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{})
  61. framework.ExpectNoError(err)
  62. uid := pod.UID
  63. ginkgo.By("update the static pod container image")
  64. image := imageutils.GetPauseImageName()
  65. err = createStaticPod(podPath, staticPodName, ns, image, v1.RestartPolicyAlways)
  66. framework.ExpectNoError(err)
  67. ginkgo.By("wait for the mirror pod to be updated")
  68. gomega.Eventually(func() error {
  69. return checkMirrorPodRecreatedAndRunning(f.ClientSet, mirrorPodName, ns, uid)
  70. }, 2*time.Minute, time.Second*4).Should(gomega.BeNil())
  71. ginkgo.By("check the mirror pod container image is updated")
  72. pod, err = f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{})
  73. framework.ExpectNoError(err)
  74. framework.ExpectEqual(len(pod.Spec.Containers), 1)
  75. framework.ExpectEqual(pod.Spec.Containers[0].Image, image)
  76. })
  77. /*
  78. Release : v1.9
  79. Testname: Mirror Pod, delete
  80. Description: When a mirror-Pod is deleted then the mirror pod MUST be re-created. Create a static pod, verify that a mirror pod is created. Delete the mirror pod, the mirror pod MUST be re-created and running.
  81. */
  82. ginkgo.It("should be recreated when mirror pod gracefully deleted [NodeConformance]", func() {
  83. ginkgo.By("get mirror pod uid")
  84. pod, err := f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{})
  85. framework.ExpectNoError(err)
  86. uid := pod.UID
  87. ginkgo.By("delete the mirror pod with grace period 30s")
  88. err = f.ClientSet.CoreV1().Pods(ns).Delete(context.TODO(), mirrorPodName, metav1.NewDeleteOptions(30))
  89. framework.ExpectNoError(err)
  90. ginkgo.By("wait for the mirror pod to be recreated")
  91. gomega.Eventually(func() error {
  92. return checkMirrorPodRecreatedAndRunning(f.ClientSet, mirrorPodName, ns, uid)
  93. }, 2*time.Minute, time.Second*4).Should(gomega.BeNil())
  94. })
  95. /*
  96. Release : v1.9
  97. Testname: Mirror Pod, force delete
  98. Description: When a mirror-Pod is deleted, forcibly, then the mirror pod MUST be re-created. Create a static pod, verify that a mirror pod is created. Delete the mirror pod with delete wait time set to zero forcing immediate deletion, the mirror pod MUST be re-created and running.
  99. */
  100. ginkgo.It("should be recreated when mirror pod forcibly deleted [NodeConformance]", func() {
  101. ginkgo.By("get mirror pod uid")
  102. pod, err := f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{})
  103. framework.ExpectNoError(err)
  104. uid := pod.UID
  105. ginkgo.By("delete the mirror pod with grace period 0s")
  106. err = f.ClientSet.CoreV1().Pods(ns).Delete(context.TODO(), mirrorPodName, metav1.NewDeleteOptions(0))
  107. framework.ExpectNoError(err)
  108. ginkgo.By("wait for the mirror pod to be recreated")
  109. gomega.Eventually(func() error {
  110. return checkMirrorPodRecreatedAndRunning(f.ClientSet, mirrorPodName, ns, uid)
  111. }, 2*time.Minute, time.Second*4).Should(gomega.BeNil())
  112. })
  113. ginkgo.AfterEach(func() {
  114. ginkgo.By("delete the static pod")
  115. err := deleteStaticPod(podPath, staticPodName, ns)
  116. framework.ExpectNoError(err)
  117. ginkgo.By("wait for the mirror pod to disappear")
  118. gomega.Eventually(func() error {
  119. return checkMirrorPodDisappear(f.ClientSet, mirrorPodName, ns)
  120. }, 2*time.Minute, time.Second*4).Should(gomega.BeNil())
  121. })
  122. })
  123. })
  124. func staticPodPath(dir, name, namespace string) string {
  125. return filepath.Join(dir, namespace+"-"+name+".yaml")
  126. }
  127. func createStaticPod(dir, name, namespace, image string, restart v1.RestartPolicy) error {
  128. template := `
  129. apiVersion: v1
  130. kind: Pod
  131. metadata:
  132. name: %s
  133. namespace: %s
  134. spec:
  135. containers:
  136. - name: test
  137. image: %s
  138. restartPolicy: %s
  139. `
  140. file := staticPodPath(dir, name, namespace)
  141. podYaml := fmt.Sprintf(template, name, namespace, image, string(restart))
  142. f, err := os.OpenFile(file, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
  143. if err != nil {
  144. return err
  145. }
  146. defer f.Close()
  147. _, err = f.WriteString(podYaml)
  148. return err
  149. }
  150. func deleteStaticPod(dir, name, namespace string) error {
  151. file := staticPodPath(dir, name, namespace)
  152. return os.Remove(file)
  153. }
  154. func checkMirrorPodDisappear(cl clientset.Interface, name, namespace string) error {
  155. _, err := cl.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
  156. if apierrors.IsNotFound(err) {
  157. return nil
  158. }
  159. return goerrors.New("pod not disappear")
  160. }
  161. func checkMirrorPodRunning(cl clientset.Interface, name, namespace string) error {
  162. pod, err := cl.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
  163. if err != nil {
  164. return fmt.Errorf("expected the mirror pod %q to appear: %v", name, err)
  165. }
  166. if pod.Status.Phase != v1.PodRunning {
  167. return fmt.Errorf("expected the mirror pod %q to be running, got %q", name, pod.Status.Phase)
  168. }
  169. return validateMirrorPod(cl, pod)
  170. }
  171. func checkMirrorPodRecreatedAndRunning(cl clientset.Interface, name, namespace string, oUID types.UID) error {
  172. pod, err := cl.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
  173. if err != nil {
  174. return fmt.Errorf("expected the mirror pod %q to appear: %v", name, err)
  175. }
  176. if pod.UID == oUID {
  177. return fmt.Errorf("expected the uid of mirror pod %q to be changed, got %q", name, pod.UID)
  178. }
  179. if pod.Status.Phase != v1.PodRunning {
  180. return fmt.Errorf("expected the mirror pod %q to be running, got %q", name, pod.Status.Phase)
  181. }
  182. return validateMirrorPod(cl, pod)
  183. }
  184. func validateMirrorPod(cl clientset.Interface, mirrorPod *v1.Pod) error {
  185. hash, ok := mirrorPod.Annotations[kubetypes.ConfigHashAnnotationKey]
  186. if !ok || hash == "" {
  187. return fmt.Errorf("expected mirror pod %q to have a hash annotation", mirrorPod.Name)
  188. }
  189. mirrorHash, ok := mirrorPod.Annotations[kubetypes.ConfigMirrorAnnotationKey]
  190. if !ok || mirrorHash == "" {
  191. return fmt.Errorf("expected mirror pod %q to have a mirror pod annotation", mirrorPod.Name)
  192. }
  193. if hash != mirrorHash {
  194. return fmt.Errorf("expected mirror pod %q to have a matching mirror pod hash: got %q; expected %q", mirrorPod.Name, mirrorHash, hash)
  195. }
  196. source, ok := mirrorPod.Annotations[kubetypes.ConfigSourceAnnotationKey]
  197. if !ok {
  198. return fmt.Errorf("expected mirror pod %q to have a source annotation", mirrorPod.Name)
  199. }
  200. if source == kubetypes.ApiserverSource {
  201. return fmt.Errorf("expected mirror pod %q source to not be 'api'; got: %q", mirrorPod.Name, source)
  202. }
  203. if len(mirrorPod.OwnerReferences) != 1 {
  204. return fmt.Errorf("expected mirror pod %q to have a single owner reference: got %d", mirrorPod.Name, len(mirrorPod.OwnerReferences))
  205. }
  206. node, err := cl.CoreV1().Nodes().Get(context.TODO(), framework.TestContext.NodeName, metav1.GetOptions{})
  207. if err != nil {
  208. return fmt.Errorf("failed to fetch test node: %v", err)
  209. }
  210. controller := true
  211. expectedOwnerRef := metav1.OwnerReference{
  212. APIVersion: "v1",
  213. Kind: "Node",
  214. Name: framework.TestContext.NodeName,
  215. UID: node.UID,
  216. Controller: &controller,
  217. }
  218. ref := mirrorPod.OwnerReferences[0]
  219. if !apiequality.Semantic.DeepEqual(ref, expectedOwnerRef) {
  220. return fmt.Errorf("unexpected mirror pod %q owner ref: %v", mirrorPod.Name, cmp.Diff(expectedOwnerRef, ref))
  221. }
  222. return nil
  223. }