Skip to content

Commit d1eea9b

Browse files
modular-magicianc2thorn
authored andcommitted
make vpc-native clusters the default (for new clusters) (#9067) (#6402)
Signed-off-by: Modular Magician <[email protected]>
1 parent 9cfd5b2 commit d1eea9b

File tree

5 files changed

+79
-15
lines changed

5 files changed

+79
-15
lines changed

.changelog/9067.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
container: newly created `google_container_cluster` resources now default to VPC-native instead of routes-based.
3+
```

google-beta/services/container/resource_container_cluster.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,13 +1567,14 @@ func ResourceContainerCluster() *schema.Resource {
15671567
},
15681568
},
15691569

1570+
// Defaults to "VPC_NATIVE" during create only
15701571
"networking_mode": {
15711572
Type: schema.TypeString,
15721573
Optional: true,
15731574
Computed: true,
15741575
ForceNew: true,
15751576
ValidateFunc: validation.StringInSlice([]string{"VPC_NATIVE", "ROUTES"}, false),
1576-
Description: `Determines whether alias IPs or routes will be used for pod IPs in the cluster.`,
1577+
Description: `Determines whether alias IPs or routes will be used for pod IPs in the cluster. Defaults to VPC_NATIVE for new clusters.`,
15771578
},
15781579

15791580
"remove_default_node_pool": {
@@ -2086,6 +2087,21 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
20862087

20872088
clusterName := d.Get("name").(string)
20882089

2090+
// Default to VPC_NATIVE mode during initial creation
2091+
// This solution (a conditional default) should not be considered to set a precedent on its own.
2092+
// If you're considering a similar approach on a different resource, strongly weigh making the field required.
2093+
// GKE tends to require exceptional handling in general- and this default was a breaking change in their API
2094+
// that was compounded on by numerous product developments afterwards. We have not seen a similar case
2095+
// since, after several years.
2096+
networkingMode := d.Get("networking_mode").(string)
2097+
clusterIpv4Cidr := d.Get("cluster_ipv4_cidr").(string)
2098+
if networkingMode == "" && clusterIpv4Cidr == "" {
2099+
err := d.Set("networking_mode", "VPC_NATIVE")
2100+
if err != nil {
2101+
return fmt.Errorf("Error setting networking mode during creation: %s", err)
2102+
}
2103+
}
2104+
20892105
ipAllocationBlock, err := expandIPAllocationPolicy(d.Get("ip_allocation_policy"), d.Get("networking_mode").(string), d.Get("enable_autopilot").(bool))
20902106
if err != nil {
20912107
return err
@@ -4246,10 +4262,7 @@ func expandIPAllocationPolicy(configured interface{}, networkingMode string, aut
42464262
l := configured.([]interface{})
42474263
if len(l) == 0 || l[0] == nil {
42484264
if networkingMode == "VPC_NATIVE" {
4249-
if autopilot {
4250-
return nil, nil
4251-
}
4252-
return nil, fmt.Errorf("`ip_allocation_policy` block is required for VPC_NATIVE clusters.")
4265+
return nil, nil
42534266
}
42544267
return &container.IPAllocationPolicy{
42554268
UseIpAliases: false,

google-beta/services/container/resource_container_cluster_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestAccContainerCluster_basic(t *testing.T) {
2828
Check: resource.ComposeAggregateTestCheckFunc(
2929
resource.TestCheckResourceAttrSet("google_container_cluster.primary", "services_ipv4_cidr"),
3030
resource.TestCheckResourceAttrSet("google_container_cluster.primary", "self_link"),
31+
resource.TestCheckResourceAttr("google_container_cluster.primary", "networking_mode", "VPC_NATIVE"),
3132
),
3233
},
3334
{
@@ -57,21 +58,31 @@ func TestAccContainerCluster_basic(t *testing.T) {
5758
func TestAccContainerCluster_networkingModeRoutes(t *testing.T) {
5859
t.Parallel()
5960

60-
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
61+
firstClusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
62+
secondClusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
6163
acctest.VcrTest(t, resource.TestCase{
6264
PreCheck: func() { acctest.AccTestPreCheck(t) },
6365
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
6466
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
6567
Steps: []resource.TestStep{
6668
{
67-
Config: testAccContainerCluster_networkingModeRoutes(clusterName),
69+
Config: testAccContainerCluster_networkingModeRoutes(firstClusterName, secondClusterName),
70+
Check: resource.ComposeTestCheckFunc(
71+
resource.TestCheckResourceAttr("google_container_cluster.primary", "networking_mode", "ROUTES"),
72+
resource.TestCheckResourceAttr("google_container_cluster.secondary", "networking_mode", "ROUTES")),
6873
},
6974
{
7075
ResourceName: "google_container_cluster.primary",
7176
ImportState: true,
7277
ImportStateVerify: true,
7378
ImportStateVerifyIgnore: []string{"deletion_protection"},
7479
},
80+
{
81+
ResourceName: "google_container_cluster.secondary",
82+
ImportState: true,
83+
ImportStateVerify: true,
84+
ImportStateVerifyIgnore: []string{"deletion_protection"},
85+
},
7586
},
7687
})
7788
}
@@ -2693,6 +2704,9 @@ func TestAccContainerCluster_withAutopilot(t *testing.T) {
26932704
Steps: []resource.TestStep{
26942705
{
26952706
Config: testAccContainerCluster_withAutopilot(pid, containerNetName, clusterName, "us-central1", true, false, ""),
2707+
Check: resource.ComposeTestCheckFunc(
2708+
resource.TestCheckResourceAttr("google_container_cluster.with_autopilot", "networking_mode", "VPC_NATIVE"),
2709+
),
26962710
},
26972711
{
26982712
ResourceName: "google_container_cluster.with_autopilot",
@@ -4260,7 +4274,7 @@ resource "google_container_cluster" "primary" {
42604274
`, name)
42614275
}
42624276

4263-
func testAccContainerCluster_networkingModeRoutes(name string) string {
4277+
func testAccContainerCluster_networkingModeRoutes(firstName, secondName string) string {
42644278
return fmt.Sprintf(`
42654279
resource "google_container_cluster" "primary" {
42664280
name = "%s"
@@ -4269,7 +4283,15 @@ resource "google_container_cluster" "primary" {
42694283
networking_mode = "ROUTES"
42704284
deletion_protection = false
42714285
}
4272-
`, name)
4286+
4287+
resource "google_container_cluster" "secondary" {
4288+
name = "%s"
4289+
location = "us-central1-a"
4290+
initial_node_count = 1
4291+
cluster_ipv4_cidr = "10.96.0.0/14"
4292+
deletion_protection = false
4293+
}
4294+
`, firstName, secondName)
42734295
}
42744296

42754297
func testAccContainerCluster_misc(name string) string {

website/docs/guides/version_5_upgrade.html.markdown

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,34 @@ Terraform from destroying or recreating the cluster.
452452
**`deletion_protection` does NOT prevent deletion outside of Terraform.**
453453
To destroy a `google_container_cluster`, this field must be explicitly set to `false`.
454454

455+
### `networking_mode` defaults to `VPC_NATIVE` for newly created clusters
456+
457+
New clusters will default to `VPC_NATIVE` which enables [IP aliasing](https://cloud.google.com/kubernetes-engine/docs/how-to/ip-aliases). Previously, `google_container_cluster` would default to using routes as
458+
the networking mode unless `ip_allocation_policy` policy was set. Now, `networking_mode` will
459+
default to `VPC_NATIVE` and `ip_allocation_policy` will be set by the server if unset in
460+
configuration. Existing clusters should not be affected.
461+
462+
#### New Minimal Config for VPC-native cluster
463+
464+
```hcl
465+
resource "google_container_cluster" "primary" {
466+
name = "my_cluster"
467+
location = "us-central1-a"
468+
initial_node_count = 1
469+
}
470+
```
471+
472+
#### New Minimal Config for Routes-based cluster
473+
474+
```hcl
475+
resource "google_container_cluster" "primary" {
476+
name = "my_cluster"
477+
location = "us-central1-a"
478+
initial_node_count = 1
479+
networking_mode = "ROUTES"
480+
}
481+
```
482+
455483
### `enable_binary_authorization` is now removed
456484

457485
`enable_binary_authorization` has been removed in favor of `binary_authorization.enabled`.

website/docs/r/container_cluster.html.markdown

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ set to `true`).
136136
* `cluster_ipv4_cidr` - (Optional) The IP address range of the Kubernetes pods
137137
in this cluster in CIDR notation (e.g. `10.96.0.0/14`). Leave blank to have one
138138
automatically chosen or specify a `/14` block in `10.0.0.0/8`. This field will
139-
only work for routes-based clusters, where `ip_allocation_policy` is not defined.
139+
default a new cluster to routes-based, where `ip_allocation_policy` is not defined.
140140

141141
* `cluster_autoscaling` - (Optional)
142142
Per-cluster configuration of Node Auto-Provisioning with Cluster Autoscaler to
@@ -194,13 +194,11 @@ set this to a value of at least `1`, alongside setting
194194
`remove_default_node_pool` to `true`.
195195

196196
* `ip_allocation_policy` - (Optional) Configuration of cluster IP allocation for
197-
VPC-native clusters. Adding this block enables [IP aliasing](https://cloud.google.com/kubernetes-engine/docs/how-to/ip-aliases),
198-
making the cluster VPC-native instead of routes-based. Structure is [documented
199-
below](#nested_ip_allocation_policy).
197+
VPC-native clusters. If this block is unset during creation, it will be set by the GKE backend.
198+
Structure is [documented below](#nested_ip_allocation_policy).
200199

201200
* `networking_mode` - (Optional) Determines whether alias IPs or routes will be used for pod IPs in the cluster.
202-
Options are `VPC_NATIVE` or `ROUTES`. `VPC_NATIVE` enables [IP aliasing](https://cloud.google.com/kubernetes-engine/docs/how-to/ip-aliases),
203-
and requires the `ip_allocation_policy` block to be defined. By default, when this field is unspecified and no `ip_allocation_policy` blocks are set, GKE will create a `ROUTES`-based cluster.
201+
Options are `VPC_NATIVE` or `ROUTES`. `VPC_NATIVE` enables [IP aliasing](https://cloud.google.com/kubernetes-engine/docs/how-to/ip-aliases). Newly created clusters will default to `VPC_NATIVE`.
204202

205203
* `logging_config` - (Optional) Logging configuration for the cluster.
206204
Structure is [documented below](#nested_logging_config).

0 commit comments

Comments
 (0)