-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from briantkennedy/cleantool
Cleantool
- Loading branch information
Showing
8 changed files
with
246 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
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 { | ||
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) { | ||
tcprs, err := theCloud.Meshes().List(ctx, filter.None) | ||
if err != nil { | ||
log.Printf("FallbackCleanup: theCloud.Meshes().List(ctx, _): %v\n", err) | ||
return | ||
} | ||
for _, tcpr := range tcprs { | ||
name := path.Base(tcpr.Name) | ||
if !matchTestResource(name) { | ||
continue | ||
} | ||
key := meta.GlobalKey(name) | ||
err = theCloud.Meshes().Delete(ctx, key) | ||
log.Printf("FallbackCleanup: theCloud.Meshes().Delete(ctx, %s): %v\n", key, err) | ||
} | ||
} | ||
|
||
func cleanupTcpRoutes(ctx context.Context) { | ||
tcprs, err := theCloud.TcpRoutes().List(ctx, filter.None) | ||
if err != nil { | ||
log.Printf("FallbackCleanup: theCloud.TcpRoutes().List(ctx, _): %v\n", err) | ||
return | ||
} | ||
for _, tcpr := range tcprs { | ||
name := path.Base(tcpr.Name) | ||
if !matchTestResource(name) { | ||
continue | ||
} | ||
key := meta.GlobalKey(name) | ||
err = theCloud.TcpRoutes().Delete(ctx, key) | ||
log.Printf("FallbackCleanup: theCloud.TcpRoutes().Delete(ctx, %s): %v\n", key, err) | ||
} | ||
} | ||
|
||
func cleanupBackendServices(ctx context.Context) { | ||
bss, err := theCloud.BackendServices().List(ctx, filter.None) | ||
if err != nil { | ||
log.Printf("FallbackCleanup: theCloud.BackendServices().List(ctx, _): %v\n", err) | ||
return | ||
} | ||
for _, bs := range bss { | ||
if !matchTestResource(bs.Name) { | ||
continue | ||
} | ||
key := meta.GlobalKey(bs.Name) | ||
err = theCloud.BackendServices().Delete(ctx, key) | ||
log.Printf("FallbackCleanup: theCloud.BackendServices().Delete(ctx, %s): %v\n", key, err) | ||
} | ||
} | ||
|
||
func cleanupHealthChecks(ctx context.Context) { | ||
hcs, err := theCloud.HealthChecks().List(ctx, filter.None) | ||
if err != nil { | ||
log.Printf("FallbackCleanup: theCloud.HealthChecks().List(ctx, _): %v\n", err) | ||
return | ||
} | ||
for _, hc := range hcs { | ||
if !matchTestResource(hc.Name) { | ||
continue | ||
} | ||
key := meta.GlobalKey(hc.Name) | ||
err = theCloud.HealthChecks().Delete(ctx, key) | ||
log.Printf("FallbackCleanup: theCloud.HealthChecks().Delete(ctx, %s): %v\n", key, err) | ||
} | ||
} | ||
|
||
// FallbackCleanup cleans all the resources created during the test run. | ||
func FallbackCleanup(ctx context.Context) { | ||
cleanupTcpRoutes(ctx) | ||
cleanupBackendServices(ctx) | ||
cleanupHealthChecks(ctx) | ||
cleanupMeshes(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package e2e | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/google" | ||
"google.golang.org/api/compute/v1" | ||
"google.golang.org/api/impersonate" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
var ( | ||
// theCloud is a global to be used in the e2e tests. | ||
theCloud cloud.Cloud | ||
) | ||
|
||
func SetupCloudOrDie(ctx context.Context) { | ||
credentials, err := google.FindDefaultCredentials(ctx, compute.ComputeScope) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ts := credentials.TokenSource | ||
|
||
// Optionally, impersonate service account by replacing token source for http client. | ||
if TestFlags.ServiceAccountName != "" { | ||
ts, err = impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{ | ||
TargetPrincipal: TestFlags.ServiceAccountName, | ||
Scopes: []string{compute.ComputeScope, compute.CloudPlatformScope}, | ||
}, option.WithCredentials(credentials)) | ||
if err != nil { | ||
log.Fatalf("Failed to use %q credentials: %v", TestFlags.ServiceAccountName, err) | ||
} | ||
} | ||
client := oauth2.NewClient(ctx, ts) | ||
|
||
mrl := &cloud.MinimumRateLimiter{RateLimiter: &cloud.NopRateLimiter{}, Minimum: 50 * time.Millisecond} | ||
crl := cloud.NewCompositeRateLimiter(mrl) | ||
|
||
// The default limit is 1500 per minute. Leave 200 buffer. | ||
computeRL := cloud.NewTickerRateLimiter(1300, time.Minute) | ||
crl.Register("HealthChecks", "", computeRL) | ||
crl.Register("BackendServices", "", computeRL) | ||
crl.Register("NetworkEndpointGroups", "", computeRL) | ||
|
||
// The default limit is 1200 per minute. Leave 200 buffer. | ||
networkServicesRL := cloud.NewTickerRateLimiter(1000, time.Minute) | ||
crl.Register("TcpRoutes", "", networkServicesRL) | ||
crl.Register("Meshes", "", networkServicesRL) | ||
|
||
// To ensure minimum time between operations, wrap the network services rate limiter. | ||
orl := &cloud.MinimumRateLimiter{RateLimiter: networkServicesRL, Minimum: 100 * time.Millisecond} | ||
crl.Register("Operations", "", orl) | ||
|
||
svc, err := cloud.NewService(ctx, client, &cloud.SingleProjectRouter{ID: TestFlags.Project}, crl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
theCloud = cloud.NewGCE(svc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package e2e | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
var ( | ||
// TestFlags passed in from the command line. | ||
TestFlags = struct { | ||
Project string | ||
ResourcePrefix string | ||
ServiceAccountName string | ||
RunID string | ||
}{ | ||
Project: "", | ||
ResourcePrefix: "k8scp-", | ||
ServiceAccountName: "", | ||
} | ||
RunID string | ||
) | ||
|
||
func init() { | ||
klog.InitFlags(flag.CommandLine) | ||
|
||
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") | ||
flag.StringVar(&RunID, "run-id", fmt.Sprintf("%0x", rand.Int63()&0xffff), "The RunID to use when managing resources.") | ||
} | ||
func ParseFlagsOrDie() { | ||
flag.Parse() | ||
|
||
if TestFlags.Project == "" { | ||
fmt.Println("-project must be set") | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.