pods.go 7.7 KB

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