namespace.go 9.6 KB

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