Skip to content

Commit

Permalink
Add tool for cleaning leaked resoures from e2e tests.
Browse files Browse the repository at this point in the history
* Fix a bug in resource cleanup where tcproutes and meshes were not
  being cleaned up due to sending invalid URL.
* Adjust cleanup matching to clean resources from all tests when RunID is empty.
  • Loading branch information
briantkennedy committed Sep 27, 2024
1 parent 8711e4d commit 8aadc2d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
26 changes: 26 additions & 0 deletions cmd/e2e-cleaner/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 13 additions & 5 deletions e2e/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package e2e

import (
"context"
"fmt"
"log"
"path"
"strings"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/filter"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
)

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) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions e2e/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ var (
Project string
ResourcePrefix string
ServiceAccountName string
RunID string
}{
Project: "",
ResourcePrefix: "k8scp-",
ServiceAccountName: "",
}
runID string
RunID string
)

func init() {
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion e2e/testmain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8aadc2d

Please sign in to comment.