Skip to content

Commit 9c65c48

Browse files
Added pod_autoscaling field to google_container_cluster resource (#13359) (#9574)
[upstream:855e89592920b4310a1441da89ecdf077b47f56d] Signed-off-by: Modular Magician <[email protected]>
1 parent 928eae6 commit 9c65c48

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

.changelog/13359.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note: enhancement
2+
container: added `pod_autoscaling` field to `google_container_cluster` resource
3+
```

google-beta/services/container/resource_container_cluster.go

+90
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,29 @@ func ResourceContainerCluster() *schema.Resource {
15661566
},
15671567
},
15681568
},
1569+
1570+
"pod_autoscaling": {
1571+
Type: schema.TypeList,
1572+
Optional: true,
1573+
Computed: true,
1574+
MaxItems: 1,
1575+
Description: `PodAutoscaling is used for configuration of parameters for workload autoscaling`,
1576+
Elem: &schema.Resource{
1577+
Schema: map[string]*schema.Schema{
1578+
"hpa_profile": {
1579+
Type: schema.TypeString,
1580+
Required: true,
1581+
ValidateFunc: validation.StringInSlice([]string{"NONE", "PERFORMANCE"}, false),
1582+
Description: `
1583+
HPA Profile is used to configure the Horizontal Pod Autoscaler (HPA) profile for the cluster.
1584+
Available options include:
1585+
- NONE: Customers explicitly opt-out of HPA profiles.
1586+
- 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.
1587+
`,
1588+
},
1589+
},
1590+
},
1591+
},
15691592
"secret_manager_config": {
15701593
Type: schema.TypeList,
15711594
Optional: true,
@@ -2486,6 +2509,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
24862509
EnableKubernetesAlpha: d.Get("enable_kubernetes_alpha").(bool),
24872510
IpAllocationPolicy: ipAllocationBlock,
24882511
PodSecurityPolicyConfig: expandPodSecurityPolicyConfig(d.Get("pod_security_policy_config")),
2512+
PodAutoscaling: expandPodAutoscaling(d.Get("pod_autoscaling")),
24892513
SecretManagerConfig: expandSecretManagerConfig(d.Get("secret_manager_config")),
24902514
Autoscaling: expandClusterAutoscaling(d.Get("cluster_autoscaling"), d),
24912515
BinaryAuthorization: expandBinaryAuthorization(d.Get("binary_authorization")),
@@ -3156,6 +3180,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
31563180
return err
31573181
}
31583182

3183+
if err := d.Set("pod_autoscaling", flattenPodAutoscaling(cluster.PodAutoscaling)); err != nil {
3184+
return err
3185+
}
3186+
31593187
if err := d.Set("secret_manager_config", flattenSecretManagerConfig(cluster.SecretManagerConfig)); err != nil {
31603188
return err
31613189
}
@@ -4112,6 +4140,33 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
41124140
log.Printf("[INFO] GKE cluster %s pod security policy config has been updated", d.Id())
41134141
}
41144142

4143+
if d.HasChange("pod_autoscaling") {
4144+
c := d.Get("pod_autoscaling")
4145+
req := &container.UpdateClusterRequest{
4146+
Update: &container.ClusterUpdate{
4147+
DesiredPodAutoscaling: expandPodAutoscaling(c),
4148+
},
4149+
}
4150+
4151+
updateF := func() error {
4152+
name := containerClusterFullName(project, location, clusterName)
4153+
clusterUpdateCall := config.NewContainerClient(userAgent).Projects.Locations.Clusters.Update(name, req)
4154+
if config.UserProjectOverride {
4155+
clusterUpdateCall.Header().Add("X-Goog-User-Project", project)
4156+
}
4157+
op, err := clusterUpdateCall.Do()
4158+
if err != nil {
4159+
return err
4160+
}
4161+
// Wait until it's updated
4162+
return ContainerOperationWait(config, op, project, location, "updating Horizontal pod Autoscaling profile", userAgent, d.Timeout(schema.TimeoutUpdate))
4163+
}
4164+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
4165+
return err
4166+
}
4167+
log.Printf("[INFO] GKE cluster %s horizontal pod autoscaling profile has been updated", d.Id())
4168+
}
4169+
41154170
if d.HasChange("secret_manager_config") {
41164171
c := d.Get("secret_manager_config")
41174172
req := &container.UpdateClusterRequest{
@@ -5677,6 +5732,28 @@ func expandPodSecurityPolicyConfig(configured interface{}) *container.PodSecurit
56775732
}
56785733
}
56795734

5735+
func expandPodAutoscaling(configured interface{}) *container.PodAutoscaling {
5736+
if configured == nil {
5737+
return nil
5738+
}
5739+
5740+
podAutoscaling := &container.PodAutoscaling{}
5741+
5742+
configs := configured.([]interface{})
5743+
5744+
if len(configs) == 0 || configs[0] == nil {
5745+
return nil
5746+
}
5747+
5748+
config := configs[0].(map[string]interface{})
5749+
5750+
if v, ok := config["hpa_profile"]; ok {
5751+
podAutoscaling.HpaProfile = v.(string)
5752+
}
5753+
5754+
return podAutoscaling
5755+
}
5756+
56805757
func expandSecretManagerConfig(configured interface{}) *container.SecretManagerConfig {
56815758
l := configured.([]interface{})
56825759
if len(l) == 0 || l[0] == nil {
@@ -6625,6 +6702,19 @@ func flattenPodSecurityPolicyConfig(c *container.PodSecurityPolicyConfig) []map[
66256702
}
66266703
}
66276704

6705+
func flattenPodAutoscaling(c *container.PodAutoscaling) []map[string]interface{} {
6706+
config := make([]map[string]interface{}, 0, 1)
6707+
6708+
if c == nil {
6709+
return config
6710+
}
6711+
6712+
config = append(config, map[string]interface{}{
6713+
"hpa_profile": c.HpaProfile,
6714+
})
6715+
return config
6716+
}
6717+
66286718
func flattenSecretManagerConfig(c *container.SecretManagerConfig) []map[string]interface{} {
66296719
if c == nil {
66306720
return []map[string]interface{}{

google-beta/services/container/resource_container_cluster_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,64 @@ func TestUnitContainerCluster_Rfc3339TimeDiffSuppress(t *testing.T) {
752752
}
753753
}
754754

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

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)