Skip to content

Commit

Permalink
Community/veritcal pod
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
chrisst authored and modular-magician committed May 20, 2019
1 parent ab3f8a8 commit e86b868
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
59 changes: 59 additions & 0 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,20 @@ func resourceContainerCluster() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},

"vertical_pod_autoscaling": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},

"tpu_ipv4_cidr_block": {
Computed: true,
Type: schema.TypeString,
Expand Down Expand Up @@ -828,6 +842,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
cluster.DatabaseEncryption = expandDatabaseEncryption(v)
}

if v, ok := d.GetOk("vertical_pod_autoscaling"); ok {
cluster.VerticalPodAutoscaling = expandVerticalPodAutoscaling(v)
}

req := &containerBeta.CreateClusterRequest{
Cluster: cluster,
}
Expand Down Expand Up @@ -992,6 +1010,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
return err
}

if err := d.Set("vertical_pod_autoscaling", flattenVerticalPodAutoscaling(cluster.VerticalPodAutoscaling)); err != nil {
return err
}

if err := d.Set("pod_security_policy_config", flattenPodSecurityPolicyConfig(cluster.PodSecurityPolicyConfig)); err != nil {
return err
}
Expand Down Expand Up @@ -1510,6 +1532,26 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
d.SetPartial("pod_security_policy_config")
}

if d.HasChange("vertical_pod_autoscaling") {
if ac, ok := d.GetOk("vertical_pod_autoscaling"); ok {
req := &containerBeta.UpdateClusterRequest{
Update: &containerBeta.ClusterUpdate{
DesiredVerticalPodAutoscaling: expandVerticalPodAutoscaling(ac),
},
}

updateF := updateFunc(req, "updating GKE cluster vertical pod autoscaling")
// Call update serially.
if err := lockedCall(lockKey, updateF); err != nil {
return err
}

log.Printf("[INFO] GKE cluster %s vertical pod autoscaling has been updated", d.Id())

d.SetPartial("vertical_pod_autoscaling")
}
}

if d.HasChange("resource_labels") {
resourceLabels := d.Get("resource_labels").(map[string]interface{})
req := &containerBeta.SetLabelsRequest{
Expand Down Expand Up @@ -1923,6 +1965,12 @@ func expandDatabaseEncryption(configured interface{}) *containerBeta.DatabaseEnc
}
}

func expandVerticalPodAutoscaling(configured interface{}) *containerBeta.VerticalPodAutoscaling {
return &containerBeta.VerticalPodAutoscaling{
Enabled: config["enabled"].(bool),
}
}

func expandPodSecurityPolicyConfig(configured interface{}) *containerBeta.PodSecurityPolicyConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down Expand Up @@ -2045,6 +2093,17 @@ func flattenPrivateClusterConfig(c *containerBeta.PrivateClusterConfig) []map[st
}
}

func flattenVerticalPodAutoscaling(c *containerBeta.VerticalPodAutoscaling) []map[string]interface{} {
if c == nil {
return nil
}
return []map[string]interface{}{
{
"enabled": c.Enabled,
},
}
}

func flattenIPAllocationPolicy(c *containerBeta.Cluster, d *schema.ResourceData, config *Config) []map[string]interface{} {
if c == nil || c.IpAllocationPolicy == nil {
return nil
Expand Down
61 changes: 61 additions & 0 deletions google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,39 @@ func TestAccContainerCluster_sharedVpc(t *testing.T) {
})
}

func TestAccContainerCluster_withVerticalPodAutoscaling(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withVerticalPodAutoscalingEnabled(clusterName),
},
{
ResourceName: "google_container_cluster.with_vertical_pod_autoscaling",
ImportStateIdPrefix: "us-central1-a/",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_withVerticalPodAutoscalingDisabled(clusterName),
},
{
ResourceName: "google_container_cluster.with_vertical_pod_autoscaling",
ImportStateIdPrefix: "us-central1-a/",
ImportState: true,
ImportStateVerify: true,
},
},
})

}

func TestAccContainerCluster_withResourceLabels(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -3068,6 +3101,34 @@ resource "google_container_cluster" "with_resource_labels" {
`, clusterName)
}

func testAccContainerCluster_withVerticalPodAutoscalingEnabled(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_vertical_pod_autoscaling" {
name = "%s"
zone = "us-central1-a"
initial_node_count = 1
vertical_pod_autoscaling {
enabled = true
}
}
`, clusterName)
}

func testAccContainerCluster_withVerticalPodAutoscalingDisabled(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_vertical_pod_autoscaling" {
name = "%s"
zone = "us-central1-a"
initial_node_count = 1
vertical_pod_autoscaling {
enabled = false
}
}
`, clusterName)
}

func testAccContainerCluster_withResourceLabels(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_resource_labels" {
Expand Down

0 comments on commit e86b868

Please sign in to comment.