diff --git a/kubetest2-gke/deployer/deployer.go b/kubetest2-gke/deployer/deployer.go index d93dc4a..c5a04ef 100644 --- a/kubetest2-gke/deployer/deployer.go +++ b/kubetest2-gke/deployer/deployer.go @@ -201,6 +201,7 @@ func NewDeployer(opts types.Options) *Deployer { }, NetworkOptions: &options.NetworkOptions{ Network: "default", + UseCustomSubnetMode: false, RemoveNetwork: true, EnableULAInternalIPv6: false, }, diff --git a/kubetest2-gke/deployer/network.go b/kubetest2-gke/deployer/network.go index d775380..7b4e5cc 100644 --- a/kubetest2-gke/deployer/network.go +++ b/kubetest2-gke/deployer/network.go @@ -73,6 +73,10 @@ func (d *Deployer) VerifyNetworkFlags() error { if err := validateSubnetRanges(d.SubnetworkRanges); err != nil { return err } + + if !d.UseCustomSubnetMode { + return errors.New("the 'auto' subnet mode cannot be used for multi-project profile") + } } return d.internalizeNetworkFlags(numProjects) @@ -148,18 +152,18 @@ func (d *Deployer) internalizeNetworkFlags(numProjects int) error { func (d *Deployer) CreateNetwork() error { // Create network if it doesn't exist. - // For single project profile, the subnet-mode could be auto for simplicity. - // For multiple projects profile, the subnet-mode must be custom and should only be created in the host project. - // (Here we consider the first project to be the host project and the rest be service projects) - // Reference: https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc#creating_a_network_and_two_subnets - subnetMode := "auto" - if len(d.Projects) > 1 { - subnetMode = "custom" - } if runWithNoOutput(exec.Command("gcloud", "compute", "networks", "describe", d.Network, "--project="+d.Projects[0], "--format=value(name)")) != nil { // Assume error implies non-existent. + // For single project profile, the subnet-mode could be auto for simplicity. + // For multiple projects profile, the subnet-mode must be custom and should only be created in the host project. + // (Here we consider the first project to be the host project and the rest be service projects) + // Reference: https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc#creating_a_network_and_two_subnets + subnetMode := "auto" + if d.UseCustomSubnetMode || len(d.Projects) > 1 { + subnetMode = string("custom") + } // TODO(chizhg): find a more reliable way to check if the network exists or not. klog.V(1).Infof("Couldn't describe network %q, assuming it doesn't exist and creating it", d.Network) createNetworkCommand := []string{ diff --git a/kubetest2-gke/deployer/options/network.go b/kubetest2-gke/deployer/options/network.go index e6ca5d8..e8dc7ea 100644 --- a/kubetest2-gke/deployer/options/network.go +++ b/kubetest2-gke/deployer/options/network.go @@ -23,6 +23,7 @@ type NetworkOptions struct { PrivateClusterMasterIPRanges []string `flag:"~private-cluster-master-ip-range" desc:"Private cluster master IP ranges. It should be IPv4 CIDR(s), and its length must be the same as the number of clusters if private cluster is requested."` SubnetworkRanges []string `flag:"~subnetwork-ranges" desc:"Subnetwork ranges as required for shared VPC setup as described in https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc#creating_a_network_and_two_subnets. For multi-project profile, it is required and should be in the format of 10.0.4.0/22 10.0.32.0/20 10.4.0.0/14,172.16.4.0/22 172.16.16.0/20 172.16.4.0/22, where the subnetworks configuration for different project are separated by comma, and the ranges of each subnetwork configuration is separated by space."` EnableULAInternalIPv6 bool `flag:"~enable-ula-internal-ipv6" desc:"Enable Unique Local IPv6 Addresses (ULA). Adds the --enable-ula-internal-ipv6 flag to the gcloud compute networks create command"` + UseCustomSubnetMode bool `flag:"~use-custom-subnet-mode" desc:"Use '--subnet-mode=custom' when creating the network. Effective only for single-project deployments, as multi-project deployments always use 'custom' mode. Defaults to false."` RemoveNetwork bool `flag:"~remove-network" desc:"At the end of the test remove non-default network that was used by cluster. The 'default' network is never deleted. Defaults to true if not provided."` }