Skip to content

Commit

Permalink
Move theCloud and related setup to e2e package.
Browse files Browse the repository at this point in the history
  • Loading branch information
briantkennedy committed Sep 27, 2024
1 parent 742d76d commit 8711e4d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
63 changes: 63 additions & 0 deletions e2e/cloud.go
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)
}
56 changes: 1 addition & 55 deletions e2e/testmain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,10 @@ package e2e
import (
"context"
"fmt"
"log"
"os"
"testing"
"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/googleapi"
"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 resourceName(name string) string {
Expand All @@ -46,48 +33,7 @@ func TestMain(m *testing.M) {
ParseFlagsOrDie()

ctx := context.Background()

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)
SetupCloudOrDie(ctx)

code := m.Run()
FallbackCleanup(ctx)
Expand Down

0 comments on commit 8711e4d

Please sign in to comment.