Skip to content

Commit 2238fa6

Browse files
mjogdandDawid212
authored andcommitted
Added pod_autoscaling field to google_container_cluster resource (GoogleCloudPlatform#13359)
1 parent 8ef7a20 commit 2238fa6

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

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

+91
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,29 @@ func ResourceContainerCluster() *schema.Resource {
15891589
},
15901590
},
15911591
},
1592+
1593+
"pod_autoscaling": {
1594+
Type: schema.TypeList,
1595+
Optional: true,
1596+
Computed: true,
1597+
MaxItems: 1,
1598+
Description: `PodAutoscaling is used for configuration of parameters for workload autoscaling`,
1599+
Elem: &schema.Resource{
1600+
Schema: map[string]*schema.Schema{
1601+
"hpa_profile": {
1602+
Type: schema.TypeString,
1603+
Required: true,
1604+
ValidateFunc: validation.StringInSlice([]string{"NONE", "PERFORMANCE"}, false),
1605+
Description: `
1606+
HPA Profile is used to configure the Horizontal Pod Autoscaler (HPA) profile for the cluster.
1607+
Available options include:
1608+
- NONE: Customers explicitly opt-out of HPA profiles.
1609+
- PERFORMANCE: PERFORMANCE is used when customers opt-in to the performance HPA profile. In this profile we support a higher number of HPAs per cluster and faster metrics collection for workload autoscaling.
1610+
`,
1611+
},
1612+
},
1613+
},
1614+
},
15921615
{{- end }}
15931616
"secret_manager_config": {
15941617
Type: schema.TypeList,
@@ -2515,6 +2538,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
25152538
IpAllocationPolicy: ipAllocationBlock,
25162539
{{- if ne $.TargetVersionName "ga" }}
25172540
PodSecurityPolicyConfig: expandPodSecurityPolicyConfig(d.Get("pod_security_policy_config")),
2541+
PodAutoscaling: expandPodAutoscaling(d.Get("pod_autoscaling")),
25182542
{{- end }}
25192543
SecretManagerConfig: expandSecretManagerConfig(d.Get("secret_manager_config")),
25202544
Autoscaling: expandClusterAutoscaling(d.Get("cluster_autoscaling"), d),
@@ -3194,6 +3218,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
31943218
if err := d.Set("cluster_telemetry", flattenClusterTelemetry(cluster.ClusterTelemetry)); err != nil {
31953219
return err
31963220
}
3221+
3222+
if err := d.Set("pod_autoscaling", flattenPodAutoscaling(cluster.PodAutoscaling)); err != nil {
3223+
return err
3224+
}
31973225
{{- end }}
31983226

31993227
if err := d.Set("secret_manager_config", flattenSecretManagerConfig(cluster.SecretManagerConfig)); err != nil {
@@ -4156,6 +4184,34 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
41564184
}
41574185
log.Printf("[INFO] GKE cluster %s pod security policy config has been updated", d.Id())
41584186
}
4187+
4188+
if d.HasChange("pod_autoscaling") {
4189+
c := d.Get("pod_autoscaling")
4190+
req := &container.UpdateClusterRequest{
4191+
Update: &container.ClusterUpdate{
4192+
DesiredPodAutoscaling: expandPodAutoscaling(c),
4193+
},
4194+
}
4195+
4196+
updateF := func() error {
4197+
name := containerClusterFullName(project, location, clusterName)
4198+
clusterUpdateCall := config.NewContainerClient(userAgent).Projects.Locations.Clusters.Update(name, req)
4199+
if config.UserProjectOverride {
4200+
clusterUpdateCall.Header().Add("X-Goog-User-Project", project)
4201+
}
4202+
op, err := clusterUpdateCall.Do()
4203+
if err != nil {
4204+
return err
4205+
}
4206+
// Wait until it's updated
4207+
return ContainerOperationWait(config, op, project, location, "updating Horizontal pod Autoscaling profile", userAgent, d.Timeout(schema.TimeoutUpdate))
4208+
}
4209+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
4210+
return err
4211+
}
4212+
log.Printf("[INFO] GKE cluster %s horizontal pod autoscaling profile has been updated", d.Id())
4213+
}
4214+
41594215
{{- end }}
41604216

41614217
if d.HasChange("secret_manager_config") {
@@ -5736,6 +5792,28 @@ func expandPodSecurityPolicyConfig(configured interface{}) *container.PodSecurit
57365792
ForceSendFields: []string{"Enabled"},
57375793
}
57385794
}
5795+
5796+
func expandPodAutoscaling(configured interface{}) *container.PodAutoscaling {
5797+
if configured == nil {
5798+
return nil
5799+
}
5800+
5801+
podAutoscaling := &container.PodAutoscaling{}
5802+
5803+
configs := configured.([]interface{})
5804+
5805+
if len(configs) == 0 || configs[0] == nil {
5806+
return nil
5807+
}
5808+
5809+
config := configs[0].(map[string]interface{})
5810+
5811+
if v, ok := config["hpa_profile"]; ok {
5812+
podAutoscaling.HpaProfile = v.(string)
5813+
}
5814+
5815+
return podAutoscaling
5816+
}
57395817
{{- end }}
57405818

57415819
func expandSecretManagerConfig(configured interface{}) *container.SecretManagerConfig {
@@ -6699,6 +6777,19 @@ func flattenPodSecurityPolicyConfig(c *container.PodSecurityPolicyConfig) []map[
66996777
}
67006778
}
67016779

6780+
func flattenPodAutoscaling(c *container.PodAutoscaling) []map[string]interface{} {
6781+
config := make([]map[string]interface{}, 0, 1)
6782+
6783+
if c == nil {
6784+
return config
6785+
}
6786+
6787+
config = append(config, map[string]interface{}{
6788+
"hpa_profile": c.HpaProfile,
6789+
})
6790+
return config
6791+
}
6792+
67026793
{{ end }}
67036794

67046795
func flattenSecretManagerConfig(c *container.SecretManagerConfig) []map[string]interface{} {

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

+60-2
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,6 @@ func TestAccContainerCluster_withAuthenticatorGroupsConfig(t *testing.T) {
707707
})
708708
}
709709

710-
711-
712710
func TestUnitContainerCluster_Rfc3339TimeDiffSuppress(t *testing.T) {
713711
cases := map[string]struct {
714712
Old, New string
@@ -752,6 +750,66 @@ func TestUnitContainerCluster_Rfc3339TimeDiffSuppress(t *testing.T) {
752750
}
753751
}
754752

753+
{{ if ne $.TargetVersionName `ga` -}}
754+
func TestAccContainerCluster_withPodAutoscaling(t *testing.T) {
755+
t.Parallel()
756+
757+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
758+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
759+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
760+
761+
acctest.VcrTest(t, resource.TestCase{
762+
PreCheck: func() { acctest.AccTestPreCheck(t) },
763+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
764+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
765+
Steps: []resource.TestStep{
766+
{
767+
Config: testAccContainerCluster_podAutoscalingConfig(clusterName, networkName, subnetworkName, "NONE"),
768+
},
769+
{
770+
ResourceName: "google_container_cluster.pod_autoscaling_config",
771+
ImportState: true,
772+
ImportStateVerify: true,
773+
ImportStateVerifyIgnore: []string{"deletion_protection"},
774+
Check: resource.TestCheckResourceAttr("google_container_cluster.pod_autoscaling_config", "pod_autoscaling.hpa_profile", "NONE"),
775+
},
776+
{
777+
Config: testAccContainerCluster_podAutoscalingConfig(clusterName, networkName, subnetworkName, "PERFORMANCE"),
778+
},
779+
{
780+
ResourceName: "google_container_cluster.pod_autoscaling_config",
781+
ImportState: true,
782+
ImportStateVerify: true,
783+
ImportStateVerifyIgnore: []string{"deletion_protection"},
784+
Check: resource.TestCheckResourceAttr("google_container_cluster.pod_autoscaling_config", "pod_autoscaling.hpa_profile", "PERFORMANCE"),
785+
},
786+
},
787+
})
788+
}
789+
790+
func testAccContainerCluster_podAutoscalingConfig(clusterName string, networkName string, subnetworkName string, hpaProfile string) string {
791+
return fmt.Sprintf(`
792+
resource "google_container_cluster" "pod_autoscaling_config" {
793+
name = "%s"
794+
location = "us-central1-a"
795+
initial_node_count = 1
796+
network = "%s"
797+
subnetwork = "%s"
798+
799+
pod_autoscaling {
800+
hpa_profile = "%s"
801+
}
802+
803+
private_cluster_config {
804+
enable_private_nodes = true
805+
}
806+
807+
deletion_protection = false
808+
}
809+
`, clusterName, networkName, subnetworkName, hpaProfile)
810+
}
811+
{{- end }}
812+
755813

756814
func testAccContainerCluster_enableMultiNetworking(clusterName string) string {
757815
return fmt.Sprintf(`

mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown

+11
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ region are guaranteed to support the same version.
293293
[PodSecurityPolicy](https://cloud.google.com/kubernetes-engine/docs/how-to/pod-security-policies) feature.
294294
Structure is [documented below](#nested_pod_security_policy_config).
295295

296+
* `pod_autoscaling` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) Configuration for the
297+
Structure is [documented below](#nested_pod_autoscaling).
298+
296299
* `secret_manager_config` - (Optional) Configuration for the
297300
[SecretManagerConfig](https://cloud.google.com/secret-manager/docs/secret-manager-managed-csi-component) feature.
298301
Structure is [documented below](#nested_secret_manager_config).
@@ -1195,6 +1198,14 @@ notification_config {
11951198
* `enabled` (Required) - Enable the PodSecurityPolicy controller for this cluster.
11961199
If enabled, pods must be valid under a PodSecurityPolicy to be created.
11971200

1201+
<a name="nested_pod_autoscaling"></a>The `pod_autoscaling` block supports:
1202+
1203+
* `hpa_profile` (Required) - Enable the Horizontal Pod Autoscaling profile for this cluster.
1204+
Acceptable values are:
1205+
* `"NONE"`: Customers explicitly opt-out of HPA profiles.
1206+
* `"PERFORMANCE"`: PERFORMANCE is used when customers opt-in to the performance HPA profile. In this profile we support a higher number of HPAs per cluster and faster metrics collection for workload autoscaling.
1207+
See [HPAProfile](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#hpaprofile) for more details.
1208+
11981209
<a name="nested_secret_manager_config"></a>The `secret_manager_config` block supports:
11991210

12001211
* `enabled` (Required) - Enable the Secret Manager add-on for this cluster.

0 commit comments

Comments
 (0)