Skip to content

Commit

Permalink
Support for GKE dual-stack clusters (GoogleCloudPlatform#7459)
Browse files Browse the repository at this point in the history
  • Loading branch information
600lyy authored and ericayyliu committed Jul 26, 2023
1 parent 28b6477 commit 6a49ef5
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,15 @@ func ResourceContainerCluster() *schema.Resource {
ConflictsWith: ipAllocationCidrBlockFields,
Description: `The name of the existing secondary range in the cluster's subnetwork to use for service ClusterIPs. Alternatively, services_ipv4_cidr_block can be used to automatically create a GKE-managed one.`,
},

"stack_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "IPV4",
ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV4_IPV6"}, false),
Description: `The IP Stack type of the cluster. Choose between IPV4 and IPV4_IPV6. Default type is IPV4 Only if not set`,
},
},
},
},
Expand Down Expand Up @@ -3964,6 +3973,7 @@ func expandClusterAddonsConfig(configured interface{}) *container.AddonsConfig {
}

func expandIPAllocationPolicy(configured interface{}, networkingMode string) (*container.IPAllocationPolicy, error) {

l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
if networkingMode == "VPC_NATIVE" {
Expand All @@ -3972,20 +3982,23 @@ func expandIPAllocationPolicy(configured interface{}, networkingMode string) (*c
return &container.IPAllocationPolicy{
UseIpAliases: false,
UseRoutes: true,
StackType: "IPV4",
ForceSendFields: []string{"UseIpAliases"},
}, nil
}

config := l[0].(map[string]interface{})
stackType := config["stack_type"].(string)

return &container.IPAllocationPolicy{
UseIpAliases: networkingMode == "VPC_NATIVE" || networkingMode == "",
ClusterIpv4CidrBlock: config["cluster_ipv4_cidr_block"].(string),
ServicesIpv4CidrBlock: config["services_ipv4_cidr_block"].(string),

ClusterSecondaryRangeName: config["cluster_secondary_range_name"].(string),
ServicesSecondaryRangeName: config["services_secondary_range_name"].(string),
ForceSendFields: []string{"UseIpAliases"},
UseRoutes: networkingMode == "ROUTES",
StackType: stackType,
}, nil
}

Expand Down Expand Up @@ -5138,12 +5151,14 @@ func flattenIPAllocationPolicy(c *container.Cluster, d *schema.ResourceData, con
}

p := c.IpAllocationPolicy

return []map[string]interface{}{
{
"cluster_ipv4_cidr_block": p.ClusterIpv4CidrBlock,
"services_ipv4_cidr_block": p.ServicesIpv4CidrBlock,
"cluster_secondary_range_name": p.ClusterSecondaryRangeName,
"services_secondary_range_name": p.ServicesSecondaryRangeName,
"stack_type": p.StackType,
},
}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,62 @@ func TestAccContainerCluster_withIPAllocationPolicy_specificSizes(t *testing.T)
})
}

func TestAccContainerCluster_stackType_withDualStack(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", RandString(t, 10))
containerNetName := fmt.Sprintf("tf-test-cluster-%s", RandString(t, 10))
resourceName := "google_container_cluster.with_stack_type"

VcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_stackType_withDualStack(containerNetName, clusterName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "ip_allocation_policy.0.stack_type", "IPV4_IPV6"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
},
})
}

func TestAccContainerCluster_stackType_withSingleStack(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", RandString(t, 10))
containerNetName := fmt.Sprintf("tf-test-cluster-%s", RandString(t, 10))
resourceName := "google_container_cluster.with_stack_type"

VcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_stackType_withSingleStack(containerNetName, clusterName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "ip_allocation_policy.0.stack_type", "IPV4"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
},
})
}

func TestAccContainerCluster_nodeAutoprovisioning(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -6252,6 +6308,77 @@ resource "google_container_cluster" "with_ip_allocation_policy" {
`, containerNetName, clusterName)
}

func testAccContainerCluster_stackType_withDualStack(containerNetName string, clusterName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
name = "%s"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "container_subnetwork" {
name = google_compute_network.container_network.name
network = google_compute_network.container_network.name
region = "us-central1"

ip_cidr_range = "10.2.0.0/16"
stack_type = "IPV4_IPV6"
ipv6_access_type = "EXTERNAL"
}

resource "google_container_cluster" "with_stack_type" {
name = "%s"
location = "us-central1-a"
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name

min_master_version = "1.25"
initial_node_count = 1
datapath_provider = "ADVANCED_DATAPATH"
enable_l4_ilb_subsetting = true

ip_allocation_policy {
cluster_ipv4_cidr_block = "10.0.0.0/16"
services_ipv4_cidr_block = "10.1.0.0/16"
stack_type = "IPV4_IPV6"
}
}
`, containerNetName, clusterName)
}

func testAccContainerCluster_stackType_withSingleStack(containerNetName string, clusterName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
name = "%s"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "container_subnetwork" {
name = google_compute_network.container_network.name
network = google_compute_network.container_network.name
region = "us-central1"

ip_cidr_range = "10.2.0.0/16"
}

resource "google_container_cluster" "with_stack_type" {
name = "%s"
location = "us-central1-a"
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name

min_master_version = "1.25"
initial_node_count = 1
enable_l4_ilb_subsetting = true

ip_allocation_policy {
cluster_ipv4_cidr_block = "10.0.0.0/16"
services_ipv4_cidr_block = "10.1.0.0/16"
stack_type = "IPV4"
}
}
`, containerNetName, clusterName)
}

func testAccContainerCluster_withResourceUsageExportConfig(clusterName, datasetId, enableMetering string) string {
return fmt.Sprintf(`
provider "google" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,10 @@ to have a range chosen with a specific netmask. Set to a CIDR notation (e.g. 10.
from the RFC-1918 private networks (e.g. 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) to
pick a specific range to use.

* `stack_type` - (Optional) The IP Stack Type of the cluster.
Default value is `IPV4`.
Possible values are `IPV4` and `PV4_IPV6`.

<a name="nested_master_auth"></a>The `master_auth` block supports:

* `client_certificate_config` - (Required) Whether client certificate authorization is enabled for this cluster. For example:
Expand Down

0 comments on commit 6a49ef5

Please sign in to comment.