Skip to content

Commit 2082b2d

Browse files
Added upgrade_settings with validation for SURGE strategy with promoting min_cpu_platform to GA (#6815) (#4958)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 8023c05 commit 2082b2d

File tree

4 files changed

+369
-39
lines changed

4 files changed

+369
-39
lines changed

.changelog/6815.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:enhancement
2+
container: added `auto_provisioning_defaults.cluster_autoscaling.upgrade_settings` in `google_container_cluster`
3+
```
4+
```release-note:enhancement
5+
container: promoted `min_cpu_platform` in `google_container_cluster` to GA
6+
```

google-beta/resource_container_cluster.go

Lines changed: 201 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func resourceContainerCluster() *schema.Resource {
176176
containerClusterAutopilotCustomizeDiff,
177177
containerClusterNodeVersionRemoveDefaultCustomizeDiff,
178178
containerClusterNetworkPolicyEmptyCustomizeDiff,
179+
containerClusterSurgeSettingsCustomizeDiff,
179180
),
180181

181182
Timeouts: &schema.ResourceTimeout{
@@ -620,6 +621,93 @@ func resourceContainerCluster() *schema.Resource {
620621
},
621622
},
622623
},
624+
"upgrade_settings": {
625+
Type: schema.TypeList,
626+
Optional: true,
627+
Description: `Specifies the upgrade settings for NAP created node pools`,
628+
Computed: true,
629+
MaxItems: 1,
630+
Elem: &schema.Resource{
631+
Schema: map[string]*schema.Schema{
632+
"max_surge": {
633+
Type: schema.TypeInt,
634+
Optional: true,
635+
Description: `The maximum number of nodes that can be created beyond the current size of the node pool during the upgrade process.`,
636+
},
637+
"max_unavailable": {
638+
Type: schema.TypeInt,
639+
Optional: true,
640+
Description: `The maximum number of nodes that can be simultaneously unavailable during the upgrade process.`,
641+
},
642+
"strategy": {
643+
Type: schema.TypeString,
644+
Optional: true,
645+
Computed: true,
646+
Description: `Update strategy of the node pool.`,
647+
ValidateFunc: validation.StringInSlice([]string{"NODE_POOL_UPDATE_STRATEGY_UNSPECIFIED", "BLUE_GREEN", "SURGE"}, false),
648+
},
649+
"blue_green_settings": {
650+
Type: schema.TypeList,
651+
Optional: true,
652+
Computed: true,
653+
MaxItems: 1,
654+
Description: `Settings for blue-green upgrade strategy.`,
655+
Elem: &schema.Resource{
656+
Schema: map[string]*schema.Schema{
657+
"node_pool_soak_duration": {
658+
Type: schema.TypeString,
659+
Optional: true,
660+
Computed: true,
661+
Description: `Time needed after draining entire blue pool. After this period, blue pool will be cleaned up.
662+
663+
A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".`,
664+
},
665+
"standard_rollout_policy": {
666+
Type: schema.TypeList,
667+
Optional: true,
668+
Computed: true,
669+
MaxItems: 1,
670+
Description: `Standard policy for the blue-green upgrade.`,
671+
Elem: &schema.Resource{
672+
Schema: map[string]*schema.Schema{
673+
"batch_percentage": {
674+
Type: schema.TypeFloat,
675+
Optional: true,
676+
Computed: true,
677+
ValidateFunc: validation.FloatBetween(0.0, 1.0),
678+
ExactlyOneOf: []string{
679+
"cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings.0.blue_green_settings.0.standard_rollout_policy.0.batch_percentage",
680+
"cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings.0.blue_green_settings.0.standard_rollout_policy.0.batch_node_count",
681+
},
682+
Description: `Percentage of the bool pool nodes to drain in a batch. The range of this field should be (0.0, 1.0].`,
683+
},
684+
"batch_node_count": {
685+
Type: schema.TypeInt,
686+
Optional: true,
687+
Computed: true,
688+
ExactlyOneOf: []string{
689+
"cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings.0.blue_green_settings.0.standard_rollout_policy.0.batch_percentage",
690+
"cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings.0.blue_green_settings.0.standard_rollout_policy.0.batch_node_count",
691+
},
692+
Description: `Number of blue nodes to drain in a batch.`,
693+
},
694+
"batch_soak_duration": {
695+
Type: schema.TypeString,
696+
Optional: true,
697+
Default: "0s",
698+
Description: `Soak time after each batch gets drained.
699+
700+
A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".`,
701+
},
702+
},
703+
},
704+
},
705+
},
706+
},
707+
},
708+
},
709+
},
710+
},
623711
},
624712
},
625713
},
@@ -3843,13 +3931,14 @@ func expandAutoProvisioningDefaults(configured interface{}, d *schema.ResourceDa
38433931
config := l[0].(map[string]interface{})
38443932

38453933
npd := &container.AutoprovisioningNodePoolDefaults{
3846-
OauthScopes: convertStringArr(config["oauth_scopes"].([]interface{})),
3847-
ServiceAccount: config["service_account"].(string),
3848-
DiskSizeGb: int64(config["disk_size"].(int)),
3849-
DiskType: config["disk_type"].(string),
3850-
ImageType: config["image_type"].(string),
3851-
BootDiskKmsKey: config["boot_disk_kms_key"].(string),
3852-
Management: expandManagement(config["management"]),
3934+
OauthScopes: convertStringArr(config["oauth_scopes"].([]interface{})),
3935+
ServiceAccount: config["service_account"].(string),
3936+
DiskSizeGb: int64(config["disk_size"].(int)),
3937+
DiskType: config["disk_type"].(string),
3938+
ImageType: config["image_type"].(string),
3939+
BootDiskKmsKey: config["boot_disk_kms_key"].(string),
3940+
Management: expandManagement(config["management"]),
3941+
UpgradeSettings: expandUpgradeSettings(config["upgrade_settings"]),
38533942
}
38543943

38553944
if v, ok := config["shielded_instance_config"]; ok && len(v.([]interface{})) > 0 {
@@ -3866,9 +3955,58 @@ func expandAutoProvisioningDefaults(configured interface{}, d *schema.ResourceDa
38663955
cpu = "automatic"
38673956
}
38683957
npd.MinCpuPlatform = cpu
3958+
38693959
return npd
38703960
}
38713961

3962+
func expandUpgradeSettings(configured interface{}) *container.UpgradeSettings {
3963+
l, ok := configured.([]interface{})
3964+
if !ok || l == nil || len(l) == 0 || l[0] == nil {
3965+
return &container.UpgradeSettings{}
3966+
}
3967+
config := l[0].(map[string]interface{})
3968+
3969+
upgradeSettings := &container.UpgradeSettings{
3970+
MaxSurge: int64(config["max_surge"].(int)),
3971+
MaxUnavailable: int64(config["max_unavailable"].(int)),
3972+
Strategy: config["strategy"].(string),
3973+
BlueGreenSettings: expandBlueGreenSettings(config["blue_green_settings"]),
3974+
}
3975+
3976+
return upgradeSettings
3977+
}
3978+
3979+
func expandBlueGreenSettings(configured interface{}) *container.BlueGreenSettings {
3980+
l, ok := configured.([]interface{})
3981+
if !ok || l == nil || len(l) == 0 || l[0] == nil {
3982+
return &container.BlueGreenSettings{}
3983+
}
3984+
config := l[0].(map[string]interface{})
3985+
3986+
blueGreenSettings := &container.BlueGreenSettings{
3987+
NodePoolSoakDuration: config["node_pool_soak_duration"].(string),
3988+
StandardRolloutPolicy: expandStandardRolloutPolicy(config["standard_rollout_policy"]),
3989+
}
3990+
3991+
return blueGreenSettings
3992+
}
3993+
3994+
func expandStandardRolloutPolicy(configured interface{}) *container.StandardRolloutPolicy {
3995+
l, ok := configured.([]interface{})
3996+
if !ok || l == nil || len(l) == 0 || l[0] == nil {
3997+
return &container.StandardRolloutPolicy{}
3998+
}
3999+
4000+
config := l[0].(map[string]interface{})
4001+
standardRolloutPolicy := &container.StandardRolloutPolicy{
4002+
BatchPercentage: config["batch_percentage"].(float64),
4003+
BatchNodeCount: int64(config["batch_node_count"].(int)),
4004+
BatchSoakDuration: config["batch_soak_duration"].(string),
4005+
}
4006+
4007+
return standardRolloutPolicy
4008+
}
4009+
38724010
func expandManagement(configured interface{}) *container.NodeManagement {
38734011
l, ok := configured.([]interface{})
38744012
if !ok || l == nil || len(l) == 0 || l[0] == nil {
@@ -4837,6 +4975,45 @@ func flattenAutoProvisioningDefaults(a *container.AutoprovisioningNodePoolDefaul
48374975
r["boot_disk_kms_key"] = a.BootDiskKmsKey
48384976
r["shielded_instance_config"] = flattenShieldedInstanceConfig(a.ShieldedInstanceConfig)
48394977
r["management"] = flattenManagement(a.Management)
4978+
r["upgrade_settings"] = flattenUpgradeSettings(a.UpgradeSettings)
4979+
4980+
return []map[string]interface{}{r}
4981+
}
4982+
4983+
func flattenUpgradeSettings(a *container.UpgradeSettings) []map[string]interface{} {
4984+
if a == nil {
4985+
return nil
4986+
}
4987+
r := make(map[string]interface{})
4988+
r["max_surge"] = a.MaxSurge
4989+
r["max_unavailable"] = a.MaxUnavailable
4990+
r["strategy"] = a.Strategy
4991+
r["blue_green_settings"] = flattenBlueGreenSettings(a.BlueGreenSettings)
4992+
4993+
return []map[string]interface{}{r}
4994+
}
4995+
4996+
func flattenBlueGreenSettings(a *container.BlueGreenSettings) []map[string]interface{} {
4997+
if a == nil {
4998+
return nil
4999+
}
5000+
5001+
r := make(map[string]interface{})
5002+
r["node_pool_soak_duration"] = a.NodePoolSoakDuration
5003+
r["standard_rollout_policy"] = flattenStandardRolloutPolicy(a.StandardRolloutPolicy)
5004+
5005+
return []map[string]interface{}{r}
5006+
}
5007+
5008+
func flattenStandardRolloutPolicy(a *container.StandardRolloutPolicy) []map[string]interface{} {
5009+
if a == nil {
5010+
return nil
5011+
}
5012+
5013+
r := make(map[string]interface{})
5014+
r["batch_percentage"] = a.BatchPercentage
5015+
r["batch_node_count"] = a.BatchNodeCount
5016+
r["batch_soak_duration"] = a.BatchSoakDuration
48405017

48415018
return []map[string]interface{}{r}
48425019
}
@@ -5264,3 +5441,20 @@ func validateNodePoolAutoConfig(cluster *container.Cluster) error {
52645441

52655442
return nil
52665443
}
5444+
5445+
func containerClusterSurgeSettingsCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
5446+
if v, ok := d.GetOk("cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings.0.strategy"); ok {
5447+
if v != "SURGE" {
5448+
if _, maxSurgeIsPresent := d.GetOk("cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings.0.max_surge"); maxSurgeIsPresent {
5449+
return fmt.Errorf("Surge upgrade settings max_surge/max_unavailable can only be used when strategy is set to SURGE")
5450+
}
5451+
}
5452+
if v != "SURGE" {
5453+
if _, maxSurgeIsPresent := d.GetOk("cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings.0.max_unavailable"); maxSurgeIsPresent {
5454+
return fmt.Errorf("Surge upgrade settings max_surge/max_unavailable can only be used when strategy is set to SURGE")
5455+
}
5456+
}
5457+
}
5458+
5459+
return nil
5460+
}

0 commit comments

Comments
 (0)