Skip to content

Commit ac6447c

Browse files
authored
Merge pull request kubernetes#94318 from gnufied/fix-namespace-deletion
Prevent deletion of namespace again
2 parents 6085d90 + c4ce420 commit ac6447c

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

test/e2e/framework/framework.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,38 @@ func (f *Framework) AfterEach() {
473473
}
474474
}
475475

476+
// DeleteNamespace can be used to delete a namespace. Additionally it can be used to
477+
// dump namespace information so as it can be used as an alternative of framework
478+
// deleting the namespace towards the end.
479+
func (f *Framework) DeleteNamespace(name string) {
480+
defer func() {
481+
err := f.ClientSet.CoreV1().Namespaces().Delete(context.TODO(), name, metav1.DeleteOptions{})
482+
if err != nil && !apierrors.IsNotFound(err) {
483+
Logf("error deleting namespace %s: %v", name, err)
484+
return
485+
}
486+
err = WaitForNamespacesDeleted(f.ClientSet, []string{name}, DefaultNamespaceDeletionTimeout)
487+
if err != nil {
488+
Logf("error deleting namespace %s: %v", name, err)
489+
return
490+
}
491+
// remove deleted namespace from namespacesToDelete map
492+
for i, ns := range f.namespacesToDelete {
493+
if ns == nil {
494+
continue
495+
}
496+
if ns.Name == name {
497+
f.namespacesToDelete = append(f.namespacesToDelete[:i], f.namespacesToDelete[i+1:]...)
498+
}
499+
}
500+
}()
501+
// if current test failed then we should dump namespace information
502+
if !f.SkipNamespaceCreation && ginkgo.CurrentGinkgoTestDescription().Failed && TestContext.DumpLogsOnFailure {
503+
DumpAllNamespaceInfo(f.ClientSet, name)
504+
}
505+
506+
}
507+
476508
// CreateNamespace creates a namespace for e2e testing.
477509
func (f *Framework) CreateNamespace(baseName string, labels map[string]string) (*v1.Namespace, error) {
478510
createTestingNS := TestContext.CreateTestingNS

test/e2e/storage/drivers/csi.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ func (h *hostpathCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.Per
213213
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
214214
// Delete the primary namespace but its okay to fail here because this namespace will
215215
// also be deleted by framework.Aftereach hook
216-
tryFunc(deleteNamespaceFunc(f.ClientSet, ns1, framework.DefaultNamespaceDeletionTimeout))
216+
tryFunc(func() { f.DeleteNamespace(ns1) })
217217

218218
ginkgo.By("uninstalling csi mock driver")
219219
tryFunc(cleanup)
220220
tryFunc(cancelLogging)
221221

222222
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
223-
tryFunc(deleteNamespaceFunc(f.ClientSet, ns2, framework.DefaultNamespaceDeletionTimeout))
223+
tryFunc(func() { f.DeleteNamespace(ns2) })
224224
// cleanup function has already ran and hence we don't need to run it again.
225225
// We do this as very last action because in-case defer(or AfterEach) races
226226
// with AfterSuite and test routine gets killed then this block still
@@ -416,7 +416,7 @@ func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.PerTest
416416
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
417417
// Delete the primary namespace but its okay to fail here because this namespace will
418418
// also be deleted by framework.Aftereach hook
419-
tryFunc(deleteNamespaceFunc(f.ClientSet, ns1, framework.DefaultNamespaceDeletionTimeout))
419+
tryFunc(func() { f.DeleteNamespace(ns1) })
420420

421421
ginkgo.By("uninstalling csi mock driver")
422422
tryFunc(func() {
@@ -429,7 +429,7 @@ func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.PerTest
429429
tryFunc(cleanup)
430430
tryFunc(cancelLogging)
431431
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
432-
tryFunc(deleteNamespaceFunc(f.ClientSet, ns2, framework.DefaultNamespaceDeletionTimeout))
432+
tryFunc(func() { f.DeleteNamespace(ns2) })
433433
// cleanup function has already ran and hence we don't need to run it again.
434434
// We do this as very last action because in-case defer(or AfterEach) races
435435
// with AfterSuite and test routine gets killed then this block still
@@ -577,14 +577,14 @@ func (g *gcePDCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.PerTes
577577
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
578578
// Delete the primary namespace but its okay to fail here because this namespace will
579579
// also be deleted by framework.Aftereach hook
580-
tryFunc(deleteNamespaceFunc(f.ClientSet, ns1, framework.DefaultNamespaceDeletionTimeout))
580+
tryFunc(func() { f.DeleteNamespace(ns1) })
581581

582582
ginkgo.By("uninstalling csi mock driver")
583583
tryFunc(cleanup)
584584
tryFunc(cancelLogging)
585585

586586
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
587-
tryFunc(deleteNamespaceFunc(f.ClientSet, ns2, framework.DefaultNamespaceDeletionTimeout))
587+
tryFunc(func() { f.DeleteNamespace(ns2) })
588588
// cleanup function has already ran and hence we don't need to run it again.
589589
// We do this as very last action because in-case defer(or AfterEach) races
590590
// with AfterSuite and test routine gets killed then this block still
@@ -646,19 +646,6 @@ func WaitForCSIDriverRegistrationOnNode(nodeName string, driverName string, cs c
646646
return nil
647647
}
648648

649-
func deleteNamespaceFunc(cs clientset.Interface, ns string, timeout time.Duration) func() {
650-
return func() {
651-
err := cs.CoreV1().Namespaces().Delete(context.TODO(), ns, metav1.DeleteOptions{})
652-
if err != nil && !apierrors.IsNotFound(err) {
653-
framework.Logf("error deleting namespace %s: %v", ns, err)
654-
}
655-
err = framework.WaitForNamespacesDeleted(cs, []string{ns}, timeout)
656-
if err != nil {
657-
framework.Logf("error deleting namespace %s: %v", ns, err)
658-
}
659-
}
660-
}
661-
662649
func tryFunc(f func()) error {
663650
var err error
664651
if f == nil {

0 commit comments

Comments
 (0)