diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 13cb24aa5a..cbb79593a7 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -58,6 +58,10 @@ const ( AutoCleanupLabelValue = "true" ) +// NamespaceLabel is the label that is put on all namespaces that are created +// for e2e tests. +var NamespaceLabel = map[string]string{"owner": "e2e-test"} + // Framework is a testing framework type Framework struct { KubeClient kubernetes.Interface @@ -500,7 +504,7 @@ func (f *Framework) CreateNamespace(namespace string) error { ns := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespace, - Labels: map[string]string{"owner": "e2e-test"}, + Labels: NamespaceLabel, }, } diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go index 2a2e0ffee3..ac3796d22e 100644 --- a/test/e2e/main_test.go +++ b/test/e2e/main_test.go @@ -23,6 +23,8 @@ import ( e2eframework "agones.dev/agones/test/e2e/framework" "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" ) var framework *e2eframework.Framework @@ -49,6 +51,11 @@ func TestMain(m *testing.M) { os.Exit(1) } + if err = cleanupNamespaces(framework); err != nil { + log.WithError(err).Error("failed to cleanup e2e namespaces") + os.Exit(1) + } + if framework.Namespace == "" { // use a custom namespace - Unix timestamp framework.Namespace = strconv.Itoa(int(time.Now().Unix())) @@ -84,3 +91,21 @@ func TestMain(m *testing.M) { exitCode = m.Run() } + +func cleanupNamespaces(framework *e2eframework.Framework) error { + // list all e2e namespaces + opts := metav1.ListOptions{LabelSelector: labels.Set(e2eframework.NamespaceLabel).String()} + list, err := framework.KubeClient.CoreV1().Namespaces().List(opts) + if err != nil { + return err + } + + // loop through them, and delete them + for _, ns := range list.Items { + if err := framework.DeleteNamespace(ns.ObjectMeta.Name); err != nil { + return err + } + } + + return nil +}