Prevent deletion of namespace again

This commit is contained in:
Hemant Kumar 2020-08-27 17:54:07 -04:00
parent 5832c44dbf
commit c4ce420667
2 changed files with 38 additions and 19 deletions

View File

@ -473,6 +473,38 @@ func (f *Framework) AfterEach() {
}
}
// DeleteNamespace can be used to delete a namespace. Additionally it can be used to
// dump namespace information so as it can be used as an alternative of framework
// deleting the namespace towards the end.
func (f *Framework) DeleteNamespace(name string) {
defer func() {
err := f.ClientSet.CoreV1().Namespaces().Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
Logf("error deleting namespace %s: %v", name, err)
return
}
err = WaitForNamespacesDeleted(f.ClientSet, []string{name}, DefaultNamespaceDeletionTimeout)
if err != nil {
Logf("error deleting namespace %s: %v", name, err)
return
}
// remove deleted namespace from namespacesToDelete map
for i, ns := range f.namespacesToDelete {
if ns == nil {
continue
}
if ns.Name == name {
f.namespacesToDelete = append(f.namespacesToDelete[:i], f.namespacesToDelete[i+1:]...)
}
}
}()
// if current test failed then we should dump namespace information
if !f.SkipNamespaceCreation && ginkgo.CurrentGinkgoTestDescription().Failed && TestContext.DumpLogsOnFailure {
DumpAllNamespaceInfo(f.ClientSet, name)
}
}
// CreateNamespace creates a namespace for e2e testing.
func (f *Framework) CreateNamespace(baseName string, labels map[string]string) (*v1.Namespace, error) {
createTestingNS := TestContext.CreateTestingNS

View File

@ -213,14 +213,14 @@ func (h *hostpathCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.Per
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
// Delete the primary namespace but its okay to fail here because this namespace will
// also be deleted by framework.Aftereach hook
tryFunc(deleteNamespaceFunc(f.ClientSet, ns1, framework.DefaultNamespaceDeletionTimeout))
tryFunc(func() { f.DeleteNamespace(ns1) })
ginkgo.By("uninstalling csi mock driver")
tryFunc(cleanup)
tryFunc(cancelLogging)
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
tryFunc(deleteNamespaceFunc(f.ClientSet, ns2, framework.DefaultNamespaceDeletionTimeout))
tryFunc(func() { f.DeleteNamespace(ns2) })
// cleanup function has already ran and hence we don't need to run it again.
// We do this as very last action because in-case defer(or AfterEach) races
// with AfterSuite and test routine gets killed then this block still
@ -416,7 +416,7 @@ func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.PerTest
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
// Delete the primary namespace but its okay to fail here because this namespace will
// also be deleted by framework.Aftereach hook
tryFunc(deleteNamespaceFunc(f.ClientSet, ns1, framework.DefaultNamespaceDeletionTimeout))
tryFunc(func() { f.DeleteNamespace(ns1) })
ginkgo.By("uninstalling csi mock driver")
tryFunc(func() {
@ -429,7 +429,7 @@ func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.PerTest
tryFunc(cleanup)
tryFunc(cancelLogging)
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
tryFunc(deleteNamespaceFunc(f.ClientSet, ns2, framework.DefaultNamespaceDeletionTimeout))
tryFunc(func() { f.DeleteNamespace(ns2) })
// cleanup function has already ran and hence we don't need to run it again.
// We do this as very last action because in-case defer(or AfterEach) races
// with AfterSuite and test routine gets killed then this block still
@ -577,14 +577,14 @@ func (g *gcePDCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.PerTes
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
// Delete the primary namespace but its okay to fail here because this namespace will
// also be deleted by framework.Aftereach hook
tryFunc(deleteNamespaceFunc(f.ClientSet, ns1, framework.DefaultNamespaceDeletionTimeout))
tryFunc(func() { f.DeleteNamespace(ns1) })
ginkgo.By("uninstalling csi mock driver")
tryFunc(cleanup)
tryFunc(cancelLogging)
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
tryFunc(deleteNamespaceFunc(f.ClientSet, ns2, framework.DefaultNamespaceDeletionTimeout))
tryFunc(func() { f.DeleteNamespace(ns2) })
// cleanup function has already ran and hence we don't need to run it again.
// We do this as very last action because in-case defer(or AfterEach) races
// with AfterSuite and test routine gets killed then this block still
@ -644,19 +644,6 @@ func WaitForCSIDriverRegistrationOnNode(nodeName string, driverName string, cs c
return nil
}
func deleteNamespaceFunc(cs clientset.Interface, ns string, timeout time.Duration) func() {
return func() {
err := cs.CoreV1().Namespaces().Delete(context.TODO(), ns, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
framework.Logf("error deleting namespace %s: %v", ns, err)
}
err = framework.WaitForNamespacesDeleted(cs, []string{ns}, timeout)
if err != nil {
framework.Logf("error deleting namespace %s: %v", ns, err)
}
}
}
func tryFunc(f func()) error {
var err error
if f == nil {