Skip to content

Commit

Permalink
Merge pull request #225 from AwesomePatrol/fix-352484069
Browse files Browse the repository at this point in the history
Add Fallback Cleanup
  • Loading branch information
google-oss-prow[bot] authored Sep 5, 2024
2 parents 2dcb3fc + a8d49f3 commit 6f792c8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
23 changes: 20 additions & 3 deletions e2e/tcproute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func createHealthchecks(graphBuilder *rgraph.Builder, hcNum int, name string) ([
var e error
for i := 0; i < hcNum; i++ {
hc, err := buildHealthCheck(graphBuilder, "hc-"+name+"-"+strconv.Itoa(i), 15)
errors.Join(e, err)
e = errors.Join(e, err)
hcs = append(hcs, hc)
}
return hcs, e
Expand All @@ -448,7 +448,7 @@ func createBackendServicesWithHC(graphBuilder *rgraph.Builder, bsNum int, name s
var e error
for i := 0; i < bsNum; i++ {
bs, err := buildBackendServiceWithLBScheme(graphBuilder, name+"-"+strconv.Itoa(i)+"-bs", hcs[i], "INTERNAL_SELF_MANAGED")
errors.Join(e, err)
e = errors.Join(e, err)
bss = append(bss, bs)
}
return bss, e
Expand Down Expand Up @@ -490,7 +490,7 @@ func createTCPRoutes(t *testing.T, graphBuilder *rgraph.Builder, numTCPR int, na
name := namePrefix + "-" + strconv.Itoa(i/2)
tcpr, err := buildTCPRoute(graphBuilder, name, meshURL, tcprRules)
if err != nil {
errors.Join(e, fmt.Errorf("buildTcpRoute(_, %s, %s, %v) = %v, want nil", name, meshURL, tcprRules, err))
e = errors.Join(e, fmt.Errorf("buildTcpRoute(_, %s, %s, %v) = %v, want nil", name, meshURL, tcprRules, err))
}
tcprs = append(tcprs, tcpr)
t.Logf("%s = %s", name, pretty.Sprint(tcpr))
Expand Down Expand Up @@ -772,6 +772,23 @@ func TestEnsureTcpRoute(t *testing.T) {

processGraphAndExpectActions(t, graphBuilder, expectedActions)

t.Cleanup(func() {
for _, r := range tcprs {
err := theCloud.TcpRoutes().Delete(ctx, r.Key)
t.Logf("theCloud.TcpRoutes().Delete(ctx, %s): %v", r.Key, err)
}

for _, bs := range bss {
err = theCloud.BackendServices().Delete(ctx, bs.Key)
t.Logf("theCloud.BackendServices().Delete(ctx, %s): %v", bs.Key, err)
}

for _, hc := range hcs {
err = theCloud.HealthChecks().Delete(ctx, hc.Key)
t.Logf("theCloud.HealthChecks().Delete(ctx, %s): %v", hc.Key, err)
}
})

expectedActions = []exec.ActionMetadata{
{Type: exec.ActionTypeMeta, Name: eventName(bss[0])},
{Type: exec.ActionTypeMeta, Name: eventName(bss[1])},
Expand Down
82 changes: 81 additions & 1 deletion e2e/testmain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import (
"log"
"math/rand"
"os"
"strings"
"testing"
"time"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/filter"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -123,7 +126,9 @@ func TestMain(m *testing.M) {
}
theCloud = cloud.NewGCE(svc)

os.Exit(m.Run())
code := m.Run()
fallbackCleanup(ctx)
os.Exit(code)
}

func checkErrCode(t *testing.T, err error, wantCode int, fmtStr string, args ...interface{}) {
Expand All @@ -137,3 +142,78 @@ func checkErrCode(t *testing.T, err error, wantCode int, fmtStr string, args ...
t.Fatalf("%s: got code %d, want %d (err: %v)", fmt.Sprintf(fmtStr, args...), gerr.Code, wantCode, err)
}
}

func matchTestResource(name string) bool {
return strings.HasPrefix(name, testFlags.resourcePrefix) && strings.Contains(name, 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 {
if !matchTestResource(tcpr.Name) {
continue
}
key := meta.GlobalKey(tcpr.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 {
if !matchTestResource(tcpr.Name) {
continue
}
key := meta.GlobalKey(tcpr.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)
}
}

func fallbackCleanup(ctx context.Context) {
cleanupTcpRoutes(ctx)
cleanupBackendServices(ctx)
cleanupHealthChecks(ctx)
cleanupMeshes(ctx)
}

0 comments on commit 6f792c8

Please sign in to comment.