diff --git a/cmd/e2e-cleaner/main.go b/cmd/e2e-cleaner/main.go new file mode 100644 index 00000000..72c578d7 --- /dev/null +++ b/cmd/e2e-cleaner/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "context" + "flag" + "fmt" + "os" + + "github.com/GoogleCloudPlatform/k8s-cloud-provider/e2e" + _ "k8s.io/klog/v2" +) + +func init() { + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) + flag.PrintDefaults() + fmt.Fprint(flag.CommandLine.Output(), "\n\n Example Usage: go run ./cmd/e2e-cleaner/main.go -project my-project -run-id \"\"\n") + } +} + +func main() { + e2e.ParseFlagsOrDie() + ctx := context.Background() + e2e.SetupCloudOrDie(ctx) + e2e.FallbackCleanup(ctx) +} diff --git a/e2e/cleanup.go b/e2e/cleanup.go index 3de8b565..b7bee9b3 100644 --- a/e2e/cleanup.go +++ b/e2e/cleanup.go @@ -2,7 +2,9 @@ package e2e import ( "context" + "fmt" "log" + "path" "strings" "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/filter" @@ -10,7 +12,11 @@ import ( ) func matchTestResource(name string) bool { - return strings.HasPrefix(name, TestFlags.ResourcePrefix) && strings.Contains(name, runID) + if RunID == "" { + return strings.HasPrefix(name, TestFlags.ResourcePrefix) + } else { + return strings.HasPrefix(name, fmt.Sprintf("%s%s-", TestFlags.ResourcePrefix, RunID)) + } } func cleanupMeshes(ctx context.Context) { @@ -20,10 +26,11 @@ func cleanupMeshes(ctx context.Context) { return } for _, tcpr := range tcprs { - if !matchTestResource(tcpr.Name) { + name := path.Base(tcpr.Name) + if !matchTestResource(name) { continue } - key := meta.GlobalKey(tcpr.Name) + key := meta.GlobalKey(name) err = theCloud.Meshes().Delete(ctx, key) log.Printf("FallbackCleanup: theCloud.Meshes().Delete(ctx, %s): %v\n", key, err) } @@ -36,10 +43,11 @@ func cleanupTcpRoutes(ctx context.Context) { return } for _, tcpr := range tcprs { - if !matchTestResource(tcpr.Name) { + name := path.Base(tcpr.Name) + if !matchTestResource(name) { continue } - key := meta.GlobalKey(tcpr.Name) + key := meta.GlobalKey(name) err = theCloud.TcpRoutes().Delete(ctx, key) log.Printf("FallbackCleanup: theCloud.TcpRoutes().Delete(ctx, %s): %v\n", key, err) } diff --git a/e2e/flags.go b/e2e/flags.go index a087dc99..aa441cc6 100644 --- a/e2e/flags.go +++ b/e2e/flags.go @@ -15,12 +15,13 @@ var ( Project string ResourcePrefix string ServiceAccountName string + RunID string }{ Project: "", ResourcePrefix: "k8scp-", ServiceAccountName: "", } - runID string + RunID string ) func init() { @@ -29,8 +30,7 @@ func init() { flag.StringVar(&TestFlags.Project, "project", TestFlags.Project, "GCP Project ID") flag.StringVar(&TestFlags.ResourcePrefix, "resourcePrefix", TestFlags.ResourcePrefix, "Prefix used to name all resources created in the tests. Any resources with this prefix will be removed during cleanup.") flag.StringVar(&TestFlags.ServiceAccountName, "sa-name", TestFlags.ServiceAccountName, "Name of the Service Account to impersonate") - - runID = fmt.Sprintf("%0x", rand.Int63()&0xffff) + flag.StringVar(&RunID, "run-id", fmt.Sprintf("%0x", rand.Int63()&0xffff), "The RunID to use when managing resources.") } func ParseFlagsOrDie() { flag.Parse() diff --git a/e2e/testmain_test.go b/e2e/testmain_test.go index e4d6629a..28b03459 100644 --- a/e2e/testmain_test.go +++ b/e2e/testmain_test.go @@ -26,7 +26,7 @@ import ( ) func resourceName(name string) string { - return TestFlags.ResourcePrefix + runID + "-" + name + return TestFlags.ResourcePrefix + RunID + "-" + name } func TestMain(m *testing.M) {