test/e2e: Allow zones to be passed to e2e cloud provider

Instead of allowing the cloud provider to guess at the zones that
should be applied for a cluster under test, allow the explicit list
of zones to consider to be passed as a new test context flag -gce-zones.

Only the GCE test cloud provider recognizes this value because only
the GCE test cloud provider makes assumptions about zones for verifying
values, and the default assumptions for GKE do not always match non-GKE
providers.
This commit is contained in:
Clayton Coleman
2021-02-04 20:48:45 -05:00
parent 10e11baa05
commit 9830cc911f
5 changed files with 176 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ package gce
import (
"context"
"fmt"
"math/rand"
"net/http"
"os/exec"
"regexp"
@@ -47,6 +48,21 @@ func factory() (framework.ProviderInterface, error) {
framework.Logf("Fetching cloud provider for %q\r", framework.TestContext.Provider)
zone := framework.TestContext.CloudConfig.Zone
region := framework.TestContext.CloudConfig.Region
allowedZones := framework.TestContext.CloudConfig.Zones
// ensure users don't specify a zone outside of the requested zones
if len(zone) > 0 && len(allowedZones) > 0 {
var found bool
for _, allowedZone := range allowedZones {
if zone == allowedZone {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("the provided zone %q must be included in the list of allowed zones %v", zone, allowedZones)
}
}
var err error
if region == "" {
@@ -59,6 +75,9 @@ func factory() (framework.ProviderInterface, error) {
if !framework.TestContext.CloudConfig.MultiZone {
managedZones = []string{zone}
}
if len(allowedZones) > 0 {
managedZones = allowedZones
}
gceCloud, err := gcecloud.CreateGCECloud(&gcecloud.CloudConfig{
APIEndpoint: framework.TestContext.CloudConfig.APIEndpoint,
@@ -79,7 +98,10 @@ func factory() (framework.ProviderInterface, error) {
return nil, fmt.Errorf("Error building GCE/GKE provider: %v", err)
}
// Arbitrarily pick one of the zones we have nodes in
// Arbitrarily pick one of the zones we have nodes in, looking at prepopulated zones first.
if framework.TestContext.CloudConfig.Zone == "" && len(managedZones) > 0 {
framework.TestContext.CloudConfig.Zone = managedZones[rand.Intn(len(managedZones))]
}
if framework.TestContext.CloudConfig.Zone == "" && framework.TestContext.CloudConfig.MultiZone {
zones, err := gceCloud.GetAllZonesFromCloudProvider()
if err != nil {