PageRenderTime 75ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/test/e2e/node/pods.go

https://gitlab.com/unofficial-mirrors/kubernetes
Go | 241 lines | 198 code | 23 blank | 20 comment | 19 complexity | 39f333e95ca1f4b5d99aa11f6fd9920e MD5 | raw file
  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 node
  14. import (
  15. "crypto/tls"
  16. "fmt"
  17. "net/http"
  18. "regexp"
  19. "strconv"
  20. "time"
  21. "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/api/resource"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/labels"
  25. "k8s.io/apimachinery/pkg/util/uuid"
  26. "k8s.io/apimachinery/pkg/util/wait"
  27. "k8s.io/apimachinery/pkg/watch"
  28. "k8s.io/kubernetes/test/e2e/framework"
  29. . "github.com/onsi/ginkgo"
  30. . "github.com/onsi/gomega"
  31. imageutils "k8s.io/kubernetes/test/utils/image"
  32. )
  33. var _ = SIGDescribe("Pods Extended", func() {
  34. f := framework.NewDefaultFramework("pods")
  35. framework.KubeDescribe("Delete Grace Period", func() {
  36. var podClient *framework.PodClient
  37. BeforeEach(func() {
  38. podClient = f.PodClient()
  39. })
  40. // Flaky issue #36821.
  41. framework.ConformanceIt("should be submitted and removed [Flaky]", func() {
  42. By("creating the pod")
  43. name := "pod-submit-remove-" + string(uuid.NewUUID())
  44. value := strconv.Itoa(time.Now().Nanosecond())
  45. pod := &v1.Pod{
  46. ObjectMeta: metav1.ObjectMeta{
  47. Name: name,
  48. Labels: map[string]string{
  49. "name": "foo",
  50. "time": value,
  51. },
  52. },
  53. Spec: v1.PodSpec{
  54. Containers: []v1.Container{
  55. {
  56. Name: "nginx",
  57. Image: imageutils.GetE2EImage(imageutils.NginxSlim),
  58. },
  59. },
  60. },
  61. }
  62. By("setting up watch")
  63. selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  64. options := metav1.ListOptions{LabelSelector: selector.String()}
  65. pods, err := podClient.List(options)
  66. Expect(err).NotTo(HaveOccurred(), "failed to query for pod")
  67. Expect(len(pods.Items)).To(Equal(0))
  68. options = metav1.ListOptions{
  69. LabelSelector: selector.String(),
  70. ResourceVersion: pods.ListMeta.ResourceVersion,
  71. }
  72. w, err := podClient.Watch(options)
  73. Expect(err).NotTo(HaveOccurred(), "failed to set up watch")
  74. By("submitting the pod to kubernetes")
  75. podClient.Create(pod)
  76. By("verifying the pod is in kubernetes")
  77. selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  78. options = metav1.ListOptions{LabelSelector: selector.String()}
  79. pods, err = podClient.List(options)
  80. Expect(err).NotTo(HaveOccurred(), "failed to query for pod")
  81. Expect(len(pods.Items)).To(Equal(1))
  82. By("verifying pod creation was observed")
  83. select {
  84. case event, _ := <-w.ResultChan():
  85. if event.Type != watch.Added {
  86. framework.Failf("Failed to observe pod creation: %v", event)
  87. }
  88. case <-time.After(framework.PodStartTimeout):
  89. framework.Failf("Timeout while waiting for pod creation")
  90. }
  91. // We need to wait for the pod to be running, otherwise the deletion
  92. // may be carried out immediately rather than gracefully.
  93. framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
  94. // save the running pod
  95. pod, err = podClient.Get(pod.Name, metav1.GetOptions{})
  96. Expect(err).NotTo(HaveOccurred(), "failed to GET scheduled pod")
  97. // start local proxy, so we can send graceful deletion over query string, rather than body parameter
  98. cmd := framework.KubectlCmd("proxy", "-p", "0")
  99. stdout, stderr, err := framework.StartCmdAndStreamOutput(cmd)
  100. Expect(err).NotTo(HaveOccurred(), "failed to start up proxy")
  101. defer stdout.Close()
  102. defer stderr.Close()
  103. defer framework.TryKill(cmd)
  104. buf := make([]byte, 128)
  105. var n int
  106. n, err = stdout.Read(buf)
  107. Expect(err).NotTo(HaveOccurred(), "failed to read from kubectl proxy stdout")
  108. output := string(buf[:n])
  109. proxyRegexp := regexp.MustCompile("Starting to serve on 127.0.0.1:([0-9]+)")
  110. match := proxyRegexp.FindStringSubmatch(output)
  111. Expect(len(match)).To(Equal(2))
  112. port, err := strconv.Atoi(match[1])
  113. Expect(err).NotTo(HaveOccurred(), "failed to convert port into string")
  114. endpoint := fmt.Sprintf("http://localhost:%d/api/v1/namespaces/%s/pods/%s?gracePeriodSeconds=30", port, pod.Namespace, pod.Name)
  115. tr := &http.Transport{
  116. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  117. }
  118. client := &http.Client{Transport: tr}
  119. req, err := http.NewRequest("DELETE", endpoint, nil)
  120. Expect(err).NotTo(HaveOccurred(), "failed to create http request")
  121. By("deleting the pod gracefully")
  122. rsp, err := client.Do(req)
  123. Expect(err).NotTo(HaveOccurred(), "failed to use http client to send delete")
  124. defer rsp.Body.Close()
  125. By("verifying the kubelet observed the termination notice")
  126. Expect(wait.Poll(time.Second*5, time.Second*30, func() (bool, error) {
  127. podList, err := framework.GetKubeletPods(f.ClientSet, pod.Spec.NodeName)
  128. if err != nil {
  129. framework.Logf("Unable to retrieve kubelet pods for node %v: %v", pod.Spec.NodeName, err)
  130. return false, nil
  131. }
  132. for _, kubeletPod := range podList.Items {
  133. if pod.Name != kubeletPod.Name {
  134. continue
  135. }
  136. if kubeletPod.ObjectMeta.DeletionTimestamp == nil {
  137. framework.Logf("deletion has not yet been observed")
  138. return false, nil
  139. }
  140. return true, nil
  141. }
  142. framework.Logf("no pod exists with the name we were looking for, assuming the termination request was observed and completed")
  143. return true, nil
  144. })).NotTo(HaveOccurred(), "kubelet never observed the termination notice")
  145. By("verifying pod deletion was observed")
  146. deleted := false
  147. timeout := false
  148. var lastPod *v1.Pod
  149. timer := time.After(2 * time.Minute)
  150. for !deleted && !timeout {
  151. select {
  152. case event, _ := <-w.ResultChan():
  153. if event.Type == watch.Deleted {
  154. lastPod = event.Object.(*v1.Pod)
  155. deleted = true
  156. }
  157. case <-timer:
  158. timeout = true
  159. }
  160. }
  161. if !deleted {
  162. framework.Failf("Failed to observe pod deletion")
  163. }
  164. Expect(lastPod.DeletionTimestamp).ToNot(BeNil())
  165. Expect(lastPod.Spec.TerminationGracePeriodSeconds).ToNot(BeZero())
  166. selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  167. options = metav1.ListOptions{LabelSelector: selector.String()}
  168. pods, err = podClient.List(options)
  169. Expect(err).NotTo(HaveOccurred(), "failed to query for pods")
  170. Expect(len(pods.Items)).To(Equal(0))
  171. })
  172. })
  173. framework.KubeDescribe("Pods Set QOS Class", func() {
  174. var podClient *framework.PodClient
  175. BeforeEach(func() {
  176. podClient = f.PodClient()
  177. })
  178. framework.ConformanceIt("should be submitted and removed ", func() {
  179. By("creating the pod")
  180. name := "pod-qos-class-" + string(uuid.NewUUID())
  181. pod := &v1.Pod{
  182. ObjectMeta: metav1.ObjectMeta{
  183. Name: name,
  184. Labels: map[string]string{
  185. "name": name,
  186. },
  187. },
  188. Spec: v1.PodSpec{
  189. Containers: []v1.Container{
  190. {
  191. Name: "nginx",
  192. Image: imageutils.GetE2EImage(imageutils.NginxSlim),
  193. Resources: v1.ResourceRequirements{
  194. Limits: v1.ResourceList{
  195. v1.ResourceCPU: resource.MustParse("100m"),
  196. v1.ResourceMemory: resource.MustParse("100Mi"),
  197. },
  198. Requests: v1.ResourceList{
  199. v1.ResourceCPU: resource.MustParse("100m"),
  200. v1.ResourceMemory: resource.MustParse("100Mi"),
  201. },
  202. },
  203. },
  204. },
  205. },
  206. }
  207. By("submitting the pod to kubernetes")
  208. podClient.Create(pod)
  209. By("verifying QOS class is set on the pod")
  210. pod, err := podClient.Get(name, metav1.GetOptions{})
  211. Expect(err).NotTo(HaveOccurred(), "failed to query for pod")
  212. Expect(pod.Status.QOSClass == v1.PodQOSGuaranteed)
  213. })
  214. })
  215. })