mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Prevent deletion of namespace again
This commit is contained in:
parent
5832c44dbf
commit
c4ce420667
@ -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.
|
// CreateNamespace creates a namespace for e2e testing.
|
||||||
func (f *Framework) CreateNamespace(baseName string, labels map[string]string) (*v1.Namespace, error) {
|
func (f *Framework) CreateNamespace(baseName string, labels map[string]string) (*v1.Namespace, error) {
|
||||||
createTestingNS := TestContext.CreateTestingNS
|
createTestingNS := TestContext.CreateTestingNS
|
||||||
|
@ -213,14 +213,14 @@ func (h *hostpathCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.Per
|
|||||||
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
|
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
|
||||||
// Delete the primary namespace but its okay to fail here because this namespace will
|
// Delete the primary namespace but its okay to fail here because this namespace will
|
||||||
// also be deleted by framework.Aftereach hook
|
// 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")
|
ginkgo.By("uninstalling csi mock driver")
|
||||||
tryFunc(cleanup)
|
tryFunc(cleanup)
|
||||||
tryFunc(cancelLogging)
|
tryFunc(cancelLogging)
|
||||||
|
|
||||||
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
|
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.
|
// 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
|
// 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
|
// 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))
|
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
|
||||||
// Delete the primary namespace but its okay to fail here because this namespace will
|
// Delete the primary namespace but its okay to fail here because this namespace will
|
||||||
// also be deleted by framework.Aftereach hook
|
// 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")
|
ginkgo.By("uninstalling csi mock driver")
|
||||||
tryFunc(func() {
|
tryFunc(func() {
|
||||||
@ -429,7 +429,7 @@ func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*testsuites.PerTest
|
|||||||
tryFunc(cleanup)
|
tryFunc(cleanup)
|
||||||
tryFunc(cancelLogging)
|
tryFunc(cancelLogging)
|
||||||
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
|
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.
|
// 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
|
// 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
|
// 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))
|
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1))
|
||||||
// Delete the primary namespace but its okay to fail here because this namespace will
|
// Delete the primary namespace but its okay to fail here because this namespace will
|
||||||
// also be deleted by framework.Aftereach hook
|
// 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")
|
ginkgo.By("uninstalling csi mock driver")
|
||||||
tryFunc(cleanup)
|
tryFunc(cleanup)
|
||||||
tryFunc(cancelLogging)
|
tryFunc(cancelLogging)
|
||||||
|
|
||||||
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
|
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.
|
// 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
|
// 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
|
// 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
|
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 {
|
func tryFunc(f func()) error {
|
||||||
var err error
|
var err error
|
||||||
if f == nil {
|
if f == nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user