Merge pull request #99175 from verult/csi-e2e-cleanup-cleanup

storage CSI e2e: Move csi driver cleanup functions into a common one
This commit is contained in:
Kubernetes Prow Robot 2021-02-18 02:14:51 -08:00 committed by GitHub
commit f760c21cd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -72,7 +72,6 @@ const (
type hostpathCSIDriver struct { type hostpathCSIDriver struct {
driverInfo storageframework.DriverInfo driverInfo storageframework.DriverInfo
manifests []string manifests []string
cleanupHandle framework.CleanupActionHandle
volumeAttributes []map[string]string volumeAttributes []map[string]string
} }
@ -177,8 +176,8 @@ func (h *hostpathCSIDriver) GetSnapshotClass(config *storageframework.PerTestCon
func (h *hostpathCSIDriver) PrepareTest(f *framework.Framework) (*storageframework.PerTestConfig, func()) { func (h *hostpathCSIDriver) PrepareTest(f *framework.Framework) (*storageframework.PerTestConfig, func()) {
// Create secondary namespace which will be used for creating driver // Create secondary namespace which will be used for creating driver
driverNamespace := utils.CreateDriverNamespace(f) driverNamespace := utils.CreateDriverNamespace(f)
ns2 := driverNamespace.Name driverns := driverNamespace.Name
ns1 := f.Namespace.Name testns := f.Namespace.Name
ginkgo.By(fmt.Sprintf("deploying %s driver", h.driverInfo.Name)) ginkgo.By(fmt.Sprintf("deploying %s driver", h.driverInfo.Name))
cancelLogging := utils.StartPodLogs(f, driverNamespace) cancelLogging := utils.StartPodLogs(f, driverNamespace)
@ -212,28 +211,13 @@ func (h *hostpathCSIDriver) PrepareTest(f *framework.Framework) (*storageframewo
framework.Failf("deploying %s driver: %v", h.driverInfo.Name, err) framework.Failf("deploying %s driver: %v", h.driverInfo.Name, err)
} }
// Cleanup CSI driver and namespaces. This function needs to be idempotent and can be cleanupFunc := generateDriverCleanupFunc(
// concurrently called from defer (or AfterEach) and AfterSuite action hooks. f,
cleanupFunc := func() { h.driverInfo.Name,
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1)) testns,
// Delete the primary namespace but its okay to fail here because this namespace will driverns,
// also be deleted by framework.Aftereach hook cleanup,
tryFunc(func() { f.DeleteNamespace(ns1) }) cancelLogging)
ginkgo.By("uninstalling csi mock driver")
tryFunc(cleanup)
tryFunc(cancelLogging)
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
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
// runs in AfterSuite
framework.RemoveCleanupAction(h.cleanupHandle)
}
h.cleanupHandle = framework.AddCleanupAction(cleanupFunc)
return config, cleanupFunc return config, cleanupFunc
} }
@ -248,7 +232,6 @@ type mockCSIDriver struct {
attachLimit int attachLimit int
enableTopology bool enableTopology bool
enableNodeExpansion bool enableNodeExpansion bool
cleanupHandle framework.CleanupActionHandle
javascriptHooks map[string]string javascriptHooks map[string]string
tokenRequests []storagev1.TokenRequest tokenRequests []storagev1.TokenRequest
requiresRepublish *bool requiresRepublish *bool
@ -359,8 +342,8 @@ func (m *mockCSIDriver) GetSnapshotClass(config *storageframework.PerTestConfig,
func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*storageframework.PerTestConfig, func()) { func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*storageframework.PerTestConfig, func()) {
// Create secondary namespace which will be used for creating driver // Create secondary namespace which will be used for creating driver
driverNamespace := utils.CreateDriverNamespace(f) driverNamespace := utils.CreateDriverNamespace(f)
ns2 := driverNamespace.Name driverns := driverNamespace.Name
ns1 := f.Namespace.Name testns := f.Namespace.Name
ginkgo.By("deploying csi mock driver") ginkgo.By("deploying csi mock driver")
cancelLogging := utils.StartPodLogs(f, driverNamespace) cancelLogging := utils.StartPodLogs(f, driverNamespace)
@ -408,7 +391,7 @@ func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*storageframework.P
}, },
} }
_, err = f.ClientSet.CoreV1().ConfigMaps(ns2).Create(context.TODO(), hooks, metav1.CreateOptions{}) _, err = f.ClientSet.CoreV1().ConfigMaps(driverns).Create(context.TODO(), hooks, metav1.CreateOptions{})
framework.ExpectNoError(err) framework.ExpectNoError(err)
if len(m.javascriptHooks) > 0 { if len(m.javascriptHooks) > 0 {
@ -441,43 +424,20 @@ func (m *mockCSIDriver) PrepareTest(f *framework.Framework) (*storageframework.P
framework.Failf("deploying csi mock driver: %v", err) framework.Failf("deploying csi mock driver: %v", err)
} }
// Cleanup CSI driver and namespaces. This function needs to be idempotent and can be cleanupFunc := generateDriverCleanupFunc(
// concurrently called from defer (or AfterEach) and AfterSuite action hooks. f,
cleanupFunc := func() { "mock",
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", ns1)) testns,
// Delete the primary namespace but its okay to fail here because this namespace will driverns,
// also be deleted by framework.Aftereach hook cleanup,
tryFunc(func() { f.DeleteNamespace(ns1) }) cancelLogging)
ginkgo.By("uninstalling csi mock driver")
tryFunc(func() {
err := f.ClientSet.CoreV1().ConfigMaps(ns2).Delete(context.TODO(), hooksConfigMapName, metav1.DeleteOptions{})
if err != nil {
framework.Logf("deleting failed: %s", err)
}
})
tryFunc(cleanup)
tryFunc(cancelLogging)
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", ns2))
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
// runs in AfterSuite
framework.RemoveCleanupAction(m.cleanupHandle)
}
m.cleanupHandle = framework.AddCleanupAction(cleanupFunc)
return config, cleanupFunc return config, cleanupFunc
} }
// gce-pd // gce-pd
type gcePDCSIDriver struct { type gcePDCSIDriver struct {
driverInfo storageframework.DriverInfo driverInfo storageframework.DriverInfo
cleanupHandle framework.CleanupActionHandle
} }
var _ storageframework.TestDriver = &gcePDCSIDriver{} var _ storageframework.TestDriver = &gcePDCSIDriver{}
@ -619,28 +579,13 @@ func (g *gcePDCSIDriver) PrepareTest(f *framework.Framework) (*storageframework.
framework.Failf("waiting for csi driver node registration on: %v", err) framework.Failf("waiting for csi driver node registration on: %v", err)
} }
// Cleanup CSI driver and namespaces. This function needs to be idempotent and can be cleanupFunc := generateDriverCleanupFunc(
// concurrently called from defer (or AfterEach) and AfterSuite action hooks. f,
cleanupFunc := func() { "gce-pd",
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", testns)) testns,
// Delete the primary namespace but it's okay to fail here because this namespace will driverns,
// also be deleted by framework.Aftereach hook cleanup,
tryFunc(func() { f.DeleteNamespace(testns) }) cancelLogging)
ginkgo.By("uninstalling csi gce-pd driver")
tryFunc(cleanup)
tryFunc(cancelLogging)
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", driverns))
tryFunc(func() { f.DeleteNamespace(driverns) })
// 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
// runs in AfterSuite
framework.RemoveCleanupAction(g.cleanupHandle)
}
g.cleanupHandle = framework.AddCleanupAction(cleanupFunc)
return &storageframework.PerTestConfig{ return &storageframework.PerTestConfig{
Driver: g, Driver: g,
@ -707,3 +652,35 @@ func tryFunc(f func()) error {
f() f()
return err return err
} }
func generateDriverCleanupFunc(
f *framework.Framework,
driverName, testns, driverns string,
driverCleanup, cancelLogging func()) func() {
cleanupHandle := new(framework.CleanupActionHandle)
// Cleanup CSI driver and namespaces. This function needs to be idempotent and can be
// concurrently called from defer (or AfterEach) and AfterSuite action hooks.
cleanupFunc := func() {
ginkgo.By(fmt.Sprintf("deleting the test namespace: %s", testns))
// Delete the primary namespace but it's okay to fail here because this namespace will
// also be deleted by framework.Aftereach hook
tryFunc(func() { f.DeleteNamespace(testns) })
ginkgo.By(fmt.Sprintf("uninstalling csi %s driver", driverName))
tryFunc(driverCleanup)
tryFunc(cancelLogging)
ginkgo.By(fmt.Sprintf("deleting the driver namespace: %s", driverns))
tryFunc(func() { f.DeleteNamespace(driverns) })
// 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
// runs in AfterSuite
framework.RemoveCleanupAction(*cleanupHandle)
}
*cleanupHandle = framework.AddCleanupAction(cleanupFunc)
return cleanupFunc
}