namespace.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Copyright 2014 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 e2e
  14. import (
  15. "fmt"
  16. "strings"
  17. "sync"
  18. "time"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/api/errors"
  21. "k8s.io/kubernetes/pkg/util/intstr"
  22. "k8s.io/kubernetes/pkg/util/wait"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. . "github.com/onsi/ginkgo"
  25. . "github.com/onsi/gomega"
  26. )
  27. func extinguish(f *framework.Framework, totalNS int, maxAllowedAfterDel int, maxSeconds int) {
  28. var err error
  29. By("Creating testing namespaces")
  30. wg := &sync.WaitGroup{}
  31. wg.Add(totalNS)
  32. for n := 0; n < totalNS; n += 1 {
  33. go func(n int) {
  34. defer wg.Done()
  35. defer GinkgoRecover()
  36. _, err = f.CreateNamespace(fmt.Sprintf("nslifetest-%v", n), nil)
  37. Expect(err).NotTo(HaveOccurred())
  38. }(n)
  39. }
  40. wg.Wait()
  41. //Wait 10 seconds, then SEND delete requests for all the namespaces.
  42. By("Waiting 10 seconds")
  43. time.Sleep(time.Duration(10 * time.Second))
  44. deleted, err := framework.DeleteNamespaces(f.Client, []string{"nslifetest"}, nil /* skipFilter */)
  45. Expect(err).NotTo(HaveOccurred())
  46. Expect(len(deleted)).To(Equal(totalNS))
  47. By("Waiting for namespaces to vanish")
  48. //Now POLL until all namespaces have been eradicated.
  49. framework.ExpectNoError(wait.Poll(2*time.Second, time.Duration(maxSeconds)*time.Second,
  50. func() (bool, error) {
  51. var cnt = 0
  52. nsList, err := f.Client.Namespaces().List(api.ListOptions{})
  53. if err != nil {
  54. return false, err
  55. }
  56. for _, item := range nsList.Items {
  57. if strings.Contains(item.Name, "nslifetest") {
  58. cnt++
  59. }
  60. }
  61. if cnt > maxAllowedAfterDel {
  62. framework.Logf("Remaining namespaces : %v", cnt)
  63. return false, nil
  64. }
  65. return true, nil
  66. }))
  67. }
  68. func ensurePodsAreRemovedWhenNamespaceIsDeleted(f *framework.Framework) {
  69. var err error
  70. By("Creating a test namespace")
  71. namespace, err := f.CreateNamespace("nsdeletetest", nil)
  72. Expect(err).NotTo(HaveOccurred())
  73. By("Waiting for a default service account to be provisioned in namespace")
  74. err = framework.WaitForDefaultServiceAccountInNamespace(f.Client, namespace.Name)
  75. Expect(err).NotTo(HaveOccurred())
  76. By("Creating a pod in the namespace")
  77. pod := &api.Pod{
  78. ObjectMeta: api.ObjectMeta{
  79. Name: "test-pod",
  80. },
  81. Spec: api.PodSpec{
  82. Containers: []api.Container{
  83. {
  84. Name: "nginx",
  85. Image: framework.GetPauseImageName(f.Client),
  86. },
  87. },
  88. },
  89. }
  90. pod, err = f.Client.Pods(namespace.Name).Create(pod)
  91. Expect(err).NotTo(HaveOccurred())
  92. By("Waiting for the pod to have running status")
  93. framework.ExpectNoError(framework.WaitForPodRunningInNamespace(f.Client, pod))
  94. By("Deleting the namespace")
  95. err = f.Client.Namespaces().Delete(namespace.Name)
  96. Expect(err).NotTo(HaveOccurred())
  97. By("Waiting for the namespace to be removed.")
  98. maxWaitSeconds := int64(60) + *pod.Spec.TerminationGracePeriodSeconds
  99. framework.ExpectNoError(wait.Poll(1*time.Second, time.Duration(maxWaitSeconds)*time.Second,
  100. func() (bool, error) {
  101. _, err = f.Client.Namespaces().Get(namespace.Name)
  102. if err != nil && errors.IsNotFound(err) {
  103. return true, nil
  104. }
  105. return false, nil
  106. }))
  107. By("Verifying there is no pod in the namespace")
  108. _, err = f.Client.Pods(namespace.Name).Get(pod.Name)
  109. Expect(err).To(HaveOccurred())
  110. }
  111. func ensureServicesAreRemovedWhenNamespaceIsDeleted(f *framework.Framework) {
  112. var err error
  113. By("Creating a test namespace")
  114. namespace, err := f.CreateNamespace("nsdeletetest", nil)
  115. Expect(err).NotTo(HaveOccurred())
  116. By("Waiting for a default service account to be provisioned in namespace")
  117. err = framework.WaitForDefaultServiceAccountInNamespace(f.Client, namespace.Name)
  118. Expect(err).NotTo(HaveOccurred())
  119. By("Creating a service in the namespace")
  120. serviceName := "test-service"
  121. labels := map[string]string{
  122. "foo": "bar",
  123. "baz": "blah",
  124. }
  125. service := &api.Service{
  126. ObjectMeta: api.ObjectMeta{
  127. Name: serviceName,
  128. },
  129. Spec: api.ServiceSpec{
  130. Selector: labels,
  131. Ports: []api.ServicePort{{
  132. Port: 80,
  133. TargetPort: intstr.FromInt(80),
  134. }},
  135. },
  136. }
  137. service, err = f.Client.Services(namespace.Name).Create(service)
  138. Expect(err).NotTo(HaveOccurred())
  139. By("Deleting the namespace")
  140. err = f.Client.Namespaces().Delete(namespace.Name)
  141. Expect(err).NotTo(HaveOccurred())
  142. By("Waiting for the namespace to be removed.")
  143. maxWaitSeconds := int64(60)
  144. framework.ExpectNoError(wait.Poll(1*time.Second, time.Duration(maxWaitSeconds)*time.Second,
  145. func() (bool, error) {
  146. _, err = f.Client.Namespaces().Get(namespace.Name)
  147. if err != nil && errors.IsNotFound(err) {
  148. return true, nil
  149. }
  150. return false, nil
  151. }))
  152. By("Verifying there is no service in the namespace")
  153. _, err = f.Client.Services(namespace.Name).Get(service.Name)
  154. Expect(err).To(HaveOccurred())
  155. }
  156. // This test must run [Serial] due to the impact of running other parallel
  157. // tests can have on its performance. Each test that follows the common
  158. // test framework follows this pattern:
  159. // 1. Create a Namespace
  160. // 2. Do work that generates content in that namespace
  161. // 3. Delete a Namespace
  162. // Creation of a Namespace is non-trivial since it requires waiting for a
  163. // ServiceAccount to be generated.
  164. // Deletion of a Namespace is non-trivial and performance intensive since
  165. // its an orchestrated process. The controller that handles deletion must
  166. // query the namespace for all existing content, and then delete each piece
  167. // of content in turn. As the API surface grows to add more KIND objects
  168. // that could exist in a Namespace, the number of calls that the namespace
  169. // controller must orchestrate grows since it must LIST, DELETE (1x1) each
  170. // KIND.
  171. // There is work underway to improve this, but it's
  172. // most likely not going to get significantly better until etcd v3.
  173. // Going back to this test, this test generates 100 Namespace objects, and then
  174. // rapidly deletes all of them. This causes the NamespaceController to observe
  175. // and attempt to process a large number of deletes concurrently. In effect,
  176. // it's like running 100 traditional e2e tests in parallel. If the namespace
  177. // controller orchestrating deletes is slowed down deleting another test's
  178. // content then this test may fail. Since the goal of this test is to soak
  179. // Namespace creation, and soak Namespace deletion, its not appropriate to
  180. // further soak the cluster with other parallel Namespace deletion activities
  181. // that each have a variable amount of content in the associated Namespace.
  182. // When run in [Serial] this test appears to delete Namespace objects at a
  183. // rate of approximately 1 per second.
  184. var _ = framework.KubeDescribe("Namespaces [Serial]", func() {
  185. f := framework.NewDefaultFramework("namespaces")
  186. It("should ensure that all pods are removed when a namespace is deleted.",
  187. func() { ensurePodsAreRemovedWhenNamespaceIsDeleted(f) })
  188. It("should ensure that all services are removed when a namespace is deleted.",
  189. func() { ensureServicesAreRemovedWhenNamespaceIsDeleted(f) })
  190. It("should delete fast enough (90 percent of 100 namespaces in 150 seconds)",
  191. func() { extinguish(f, 100, 10, 150) })
  192. // On hold until etcd3; see #7372
  193. It("should always delete fast (ALL of 100 namespaces in 150 seconds) [Feature:ComprehensiveNamespaceDraining]",
  194. func() { extinguish(f, 100, 0, 150) })
  195. })