Skip to content

Commit 8b17a6e

Browse files
authored
Add Autoscaled Blue green upgrade API support to container_node_pool for public preview (GoogleCloudPlatform#15591)
1 parent 7219d28 commit 8b17a6e

4 files changed

Lines changed: 223 additions & 31 deletions

File tree

mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,93 @@ func TestAccContainerCluster_withTpu(t *testing.T) {
17931793
}
17941794
{{- end }}
17951795

1796+
func TestAccContainerCluster_nodePoolWithUpgradeSettings(t *testing.T) {
1797+
t.Parallel()
1798+
1799+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
1800+
npName := fmt.Sprintf("tf-test-np-%s", acctest.RandString(t, 10))
1801+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
1802+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
1803+
1804+
acctest.VcrTest(t, resource.TestCase{
1805+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1806+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1807+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
1808+
Steps: []resource.TestStep{
1809+
{
1810+
Config: testAccContainerCluster_nodePoolWithUpgradeSettings(clusterName, npName, networkName, subnetworkName, 2, 3, "SURGE", "", 0, 0.0, "", "", ""),
1811+
},
1812+
{
1813+
ResourceName: "google_container_cluster.primary",
1814+
ImportState: true,
1815+
ImportStateVerify: true,
1816+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1817+
},
1818+
{
1819+
Config: testAccContainerCluster_nodePoolWithUpgradeSettings(clusterName, npName, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "STANDARD", 1, 0.0, "100s", "100s", ""),
1820+
},
1821+
{
1822+
ResourceName: "google_container_cluster.primary",
1823+
ImportState: true,
1824+
ImportStateVerify: true,
1825+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1826+
},
1827+
{
1828+
Config: testAccContainerCluster_nodePoolWithUpgradeSettings(clusterName, npName, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "STANDARD", 0, 0.5, "100s", "100s", ""),
1829+
},
1830+
{
1831+
ResourceName: "google_container_cluster.primary",
1832+
ImportState: true,
1833+
ImportStateVerify: true,
1834+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1835+
},
1836+
{{ if ne $.TargetVersionName `ga` -}}
1837+
{
1838+
Config: testAccContainerCluster_nodePoolWithUpgradeSettings(clusterName, npName, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "AUTOSCALED", 0, 0.0, "", "0s", ""),
1839+
},
1840+
{
1841+
ResourceName: "google_container_cluster.primary",
1842+
ImportState: true,
1843+
ImportStateVerify: true,
1844+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1845+
},
1846+
{
1847+
Config: testAccContainerCluster_nodePoolWithUpgradeSettings(clusterName, npName, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "AUTOSCALED", 0, 0.0, "", "0s", "100s"),
1848+
},
1849+
{
1850+
ResourceName: "google_container_cluster.primary",
1851+
ImportState: true,
1852+
ImportStateVerify: true,
1853+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1854+
},
1855+
{{- end }}
1856+
},
1857+
})
1858+
}
1859+
1860+
func testAccContainerCluster_nodePoolWithUpgradeSettings(cluster, np, network, subnetwork string, maxSurge, maxUnavailable int, strategy, policy string, batchNodeCount int, batchPercentage float64, batchSoakDuration, nodePoolSoakDuration, waitForDrainDuration string) string {
1861+
upgradeSettings := makeUpgradeSettings(maxSurge, maxUnavailable, strategy, policy, nodePoolSoakDuration, batchNodeCount, batchPercentage, batchSoakDuration, waitForDrainDuration)
1862+
return fmt.Sprintf(`
1863+
resource "google_container_cluster" "primary" {
1864+
name = "%s"
1865+
location = "us-central1-f"
1866+
network = "%s"
1867+
subnetwork = "%s"
1868+
deletion_protection = false
1869+
1870+
node_pool {
1871+
name = "%s"
1872+
initial_node_count = 1
1873+
autoscaling {
1874+
min_node_count = 1
1875+
max_node_count = 3
1876+
}
1877+
%s
1878+
}
1879+
}
1880+
`, cluster, network, subnetwork, np, upgradeSettings)
1881+
}
1882+
17961883
func TestAccContainerCluster_withPrivateClusterConfigBasic(t *testing.T) {
17971884
t.Parallel()
17981885

mmv1/third_party/terraform/services/container/resource_container_node_pool.go.tmpl

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,19 @@ var schemaBlueGreenSettings = &schema.Schema{
250250
Schema: map[string]*schema.Schema{
251251
"standard_rollout_policy": {
252252
Type: schema.TypeList,
253+
{{ if eq $.TargetVersionName `ga` -}}
253254
Required: true,
255+
{{- else }}
256+
Optional: true,
257+
{{- end }}
254258
MaxItems: 1,
255259
Description: `Standard rollout policy is the default policy for blue-green.`,
256260
Elem: &schema.Resource{
257261
Schema: map[string]*schema.Schema{
258262
"batch_percentage": {
259263
Type: schema.TypeFloat,
260264
Optional: true,
261-
Computed: true,
265+
Computed: true,
262266
Description: `Percentage of the blue pool nodes to drain in a batch.`,
263267
ValidateFunc: validation.FloatBetween(0.0, 1.0),
264268
},
@@ -271,7 +275,7 @@ var schemaBlueGreenSettings = &schema.Schema{
271275
"batch_soak_duration": {
272276
Type: schema.TypeString,
273277
Optional: true,
274-
Computed: true,
278+
Computed: true,
275279
Description: `Soak time after each batch gets drained.`,
276280
},
277281
},
@@ -283,6 +287,24 @@ var schemaBlueGreenSettings = &schema.Schema{
283287
Computed: true,
284288
Description: `Time needed after draining entire blue pool. After this period, blue pool will be cleaned up.`,
285289
},
290+
{{ if ne $.TargetVersionName `ga` -}}
291+
"autoscaled_rollout_policy": {
292+
Type: schema.TypeList,
293+
Optional: true,
294+
MaxItems: 1,
295+
Description: `Autoscaled rollout policy for blue-green upgrade.`,
296+
Elem: &schema.Resource{
297+
Schema: map[string]*schema.Schema{
298+
"wait_for_drain_duration": {
299+
Type: schema.TypeString,
300+
Optional: true,
301+
Computed: true,
302+
Description: `Time in seconds to wait after cordoning the blue pool before draining the nodes.`,
303+
},
304+
},
305+
},
306+
},
307+
{{- end }}
286308
},
287309
},
288310
Description: `Settings for BlueGreen node pool upgrade.`,
@@ -1240,6 +1262,14 @@ func expandNodePool(d *schema.ResourceData, prefix string) (*container.NodePool,
12401262
return nil, fmt.Errorf("Blue-green upgrade settings may not be changed when blue-green strategy is not enabled")
12411263
}
12421264

1265+
{{ if ne $.TargetVersionName `ga` -}}
1266+
if _, ok1 := blueGreenSettingsConfig["standard_rollout_policy"]; ok1 {
1267+
if _, ok2 := blueGreenSettingsConfig["autoscaled_rollout_policy"]; ok2 {
1268+
return nil, fmt.Errorf("`standard_rollout_policy` and `autoscaled_rollout_policy` cannot be set at the same time")
1269+
}
1270+
}
1271+
{{- end }}
1272+
12431273
if v, ok := blueGreenSettingsConfig["node_pool_soak_duration"]; ok {
12441274
np.UpgradeSettings.BlueGreenSettings.NodePoolSoakDuration = v.(string)
12451275
}
@@ -1260,6 +1290,17 @@ func expandNodePool(d *schema.ResourceData, prefix string) (*container.NodePool,
12601290

12611291
np.UpgradeSettings.BlueGreenSettings.StandardRolloutPolicy = standardRolloutPolicy
12621292
}
1293+
{{ if ne $.TargetVersionName `ga` -}}
1294+
if v, ok := blueGreenSettingsConfig["autoscaled_rollout_policy"]; ok && len(v.([]interface{})) > 0 {
1295+
autoscaledRolloutPolicyConfig := v.([]interface{})[0].(map[string]interface{})
1296+
autoscaledRolloutPolicy := &container.AutoscaledRolloutPolicy{}
1297+
1298+
if v, ok := autoscaledRolloutPolicyConfig["wait_for_drain_duration"]; ok {
1299+
autoscaledRolloutPolicy.WaitForDrainDuration = v.(string)
1300+
}
1301+
np.UpgradeSettings.BlueGreenSettings.AutoscaledRolloutPolicy = autoscaledRolloutPolicy
1302+
}
1303+
{{- end }}
12631304
}
12641305
}
12651306

@@ -1280,6 +1321,20 @@ func flattenNodePoolStandardRolloutPolicy(rp *container.StandardRolloutPolicy) [
12801321
}
12811322
}
12821323

1324+
{{ if ne $.TargetVersionName `ga` -}}
1325+
func flattenNodePoolAutoscaledRolloutPolicy(rp *container.AutoscaledRolloutPolicy) []map[string]interface{} {
1326+
if rp == nil {
1327+
return nil
1328+
}
1329+
1330+
return []map[string]interface{}{
1331+
{
1332+
"wait_for_drain_duration": rp.WaitForDrainDuration,
1333+
},
1334+
}
1335+
}
1336+
{{- end }}
1337+
12831338
func flattenNodePoolBlueGreenSettings(bg *container.BlueGreenSettings) []map[string]interface{} {
12841339
if bg == nil {
12851340
return nil
@@ -1288,6 +1343,9 @@ func flattenNodePoolBlueGreenSettings(bg *container.BlueGreenSettings) []map[str
12881343
{
12891344
"node_pool_soak_duration": bg.NodePoolSoakDuration,
12901345
"standard_rollout_policy": flattenNodePoolStandardRolloutPolicy(bg.StandardRolloutPolicy),
1346+
{{ if ne $.TargetVersionName `ga` -}}
1347+
"autoscaled_rollout_policy": flattenNodePoolAutoscaledRolloutPolicy(bg.AutoscaledRolloutPolicy),
1348+
{{- end }}
12911349
},
12921350
}
12931351
}
@@ -1797,6 +1855,17 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node
17971855
}
17981856
blueGreenSettings.StandardRolloutPolicy = standardRolloutPolicy
17991857
}
1858+
{{ if ne $.TargetVersionName `ga` -}}
1859+
if v, ok := blueGreenSettingsConfig["autoscaled_rollout_policy"]; ok && len(v.([]interface{})) > 0 {
1860+
autoscaledRolloutPolicy := &container.AutoscaledRolloutPolicy{}
1861+
if autoscaledRolloutPolicyConfig, ok := v.([]interface{})[0].(map[string]interface{}); ok {
1862+
if v, ok := autoscaledRolloutPolicyConfig["wait_for_drain_duration"]; ok {
1863+
autoscaledRolloutPolicy.WaitForDrainDuration = v.(string)
1864+
}
1865+
}
1866+
blueGreenSettings.AutoscaledRolloutPolicy = autoscaledRolloutPolicy
1867+
}
1868+
{{- end }}
18001869
upgradeSettings.BlueGreenSettings = blueGreenSettings
18011870
}
18021871
}

mmv1/third_party/terraform/services/container/resource_container_node_pool_test.go.tmpl

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,45 +1419,63 @@ func TestAccContainerNodePool_withUpgradeSettings(t *testing.T) {
14191419
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
14201420
Steps: []resource.TestStep{
14211421
{
1422-
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 2, 3, "SURGE", "", 0, 0.0, ""),
1422+
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 2, 3, "SURGE", "", "", 0, 0.0, "", ""),
14231423
},
14241424
{
14251425
ResourceName: "google_container_node_pool.with_upgrade_settings",
14261426
ImportState: true,
14271427
ImportStateVerify: true,
14281428
},
14291429
{
1430-
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 2, 1, "SURGE", "", 0, 0.0, ""),
1430+
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 2, 1, "SURGE", "", "", 0, 0.0, "", ""),
14311431
},
14321432
{
14331433
ResourceName: "google_container_node_pool.with_upgrade_settings",
14341434
ImportState: true,
14351435
ImportStateVerify: true,
14361436
},
14371437
{
1438-
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 1, 1, "SURGE", "", 0, 0.0, ""),
1438+
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 1, 1, "SURGE", "", "", 0, 0.0, "", ""),
14391439
},
14401440
{
14411441
ResourceName: "google_container_node_pool.with_upgrade_settings",
14421442
ImportState: true,
14431443
ImportStateVerify: true,
14441444
},
14451445
{
1446-
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "100s", 1, 0.0, "0s"),
1446+
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "STANDARD", "100s", 1, 0.0, "0s", ""),
14471447
},
14481448
{
14491449
ResourceName: "google_container_node_pool.with_upgrade_settings",
14501450
ImportState: true,
14511451
ImportStateVerify: true,
14521452
},
14531453
{
1454-
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "100s", 0, 0.5, "1s"),
1454+
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "STANDARD", "100s", 0, 0.5, "1s", ""),
14551455
},
14561456
{
14571457
ResourceName: "google_container_node_pool.with_upgrade_settings",
14581458
ImportState: true,
14591459
ImportStateVerify: true,
14601460
},
1461+
{{ if ne $.TargetVersionName `ga` -}}
1462+
{
1463+
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "AUTOSCALED", "0s", 0, 0.0, "", ""),
1464+
},
1465+
{
1466+
ResourceName: "google_container_node_pool.with_upgrade_settings",
1467+
ImportState: true,
1468+
ImportStateVerify: true,
1469+
},
1470+
{
1471+
Config: testAccContainerNodePool_withUpgradeSettings(cluster, np, networkName, subnetworkName, 0, 0, "BLUE_GREEN", "AUTOSCALED", "0s", 0, 0.0, "", "100s"),
1472+
},
1473+
{
1474+
ResourceName: "google_container_node_pool.with_upgrade_settings",
1475+
ImportState: true,
1476+
ImportStateVerify: true,
1477+
},
1478+
{{- end }}
14611479
},
14621480
})
14631481
}
@@ -4507,32 +4525,46 @@ resource "google_container_node_pool" "with_boot_disk_kms_key" {
45074525
}
45084526
{{- end }}
45094527

4510-
func makeUpgradeSettings(maxSurge int, maxUnavailable int, strategy string, nodePoolSoakDuration string, batchNodeCount int, batchPercentage float64, batchSoakDuration string) string {
4528+
func makeUpgradeSettings(maxSurge int, maxUnavailable int, strategy, policy string, nodePoolSoakDuration string, batchNodeCount int, batchPercentage float64, batchSoakDuration string, waitForDrainDuration string) string {
45114529
if strategy == "BLUE_GREEN" {
4530+
policyBlock := ""
4531+
if policy == "STANDARD" {
4532+
policyBlock = fmt.Sprintf(`
4533+
standard_rollout_policy {
4534+
batch_node_count = %d
4535+
batch_percentage = %f
4536+
batch_soak_duration = "%s"
4537+
}
4538+
node_pool_soak_duration = "%s"
4539+
`, batchNodeCount, batchPercentage, batchSoakDuration, nodePoolSoakDuration)
4540+
} else if policy == "AUTOSCALED" {
4541+
policyBlock = fmt.Sprintf(`
4542+
autoscaled_rollout_policy {
4543+
wait_for_drain_duration = "%s"
4544+
}
4545+
node_pool_soak_duration = "%s"
4546+
`, waitForDrainDuration, nodePoolSoakDuration)
4547+
}
45124548
return fmt.Sprintf(`
4513-
upgrade_settings {
4514-
strategy = "%s"
4515-
blue_green_settings {
4516-
node_pool_soak_duration = "%s"
4517-
standard_rollout_policy {
4518-
batch_node_count = %d
4519-
batch_percentage = %f
4520-
batch_soak_duration = "%s"
4549+
upgrade_settings {
4550+
strategy = "%s"
4551+
blue_green_settings {
4552+
%s
45214553
}
45224554
}
4523-
}
4524-
`, strategy, nodePoolSoakDuration, batchNodeCount, batchPercentage, batchSoakDuration)
4555+
`, strategy, policyBlock)
45254556
}
45264557
return fmt.Sprintf(`
4527-
upgrade_settings {
4528-
max_surge = %d
4529-
max_unavailable = %d
4530-
strategy = "%s"
4531-
}
4532-
`, maxSurge, maxUnavailable, strategy)
4558+
upgrade_settings {
4559+
max_surge = %d
4560+
max_unavailable = %d
4561+
strategy = "%s"
4562+
}
4563+
`, maxSurge, maxUnavailable, strategy)
45334564
}
45344565

4535-
func testAccContainerNodePool_withUpgradeSettings(clusterName, nodePoolName, networkName, subnetworkName string, maxSurge int, maxUnavailable int, strategy string, nodePoolSoakDuration string, batchNodeCount int, batchPercentage float64, batchSoakDuration string) string {
4566+
func testAccContainerNodePool_withUpgradeSettings(clusterName, nodePoolName, networkName, subnetworkName string, maxSurge int, maxUnavailable int, strategy, policy string, nodePoolSoakDuration string, batchNodeCount int, batchPercentage float64, batchSoakDuration, waitForDrainDuration string) string {
4567+
upgradeSettings := makeUpgradeSettings(maxSurge, maxUnavailable, strategy, policy, nodePoolSoakDuration, batchNodeCount, batchPercentage, batchSoakDuration, waitForDrainDuration)
45364568
return fmt.Sprintf(`
45374569
data "google_container_engine_versions" "central1" {
45384570
location = "us-central1"
@@ -4553,9 +4585,13 @@ resource "google_container_node_pool" "with_upgrade_settings" {
45534585
location = "us-central1"
45544586
cluster = google_container_cluster.cluster.name
45554587
initial_node_count = 1
4588+
autoscaling {
4589+
min_node_count = 1
4590+
max_node_count = 3
4591+
}
45564592
%s
45574593
}
4558-
`, clusterName, networkName, subnetworkName, nodePoolName, makeUpgradeSettings(maxSurge, maxUnavailable, strategy, nodePoolSoakDuration, batchNodeCount, batchPercentage, batchSoakDuration))
4594+
`, clusterName, networkName, subnetworkName, nodePoolName, upgradeSettings)
45594595
}
45604596

45614597
func testAccContainerNodePool_withGPU(cluster, np, networkName, subnetworkName string) string {

0 commit comments

Comments
 (0)