diff --git a/google/resource_container_cluster.go b/google/resource_container_cluster.go index 12f37cd3..2c20ab8c 100644 --- a/google/resource_container_cluster.go +++ b/google/resource_container_cluster.go @@ -462,6 +462,12 @@ func resourceContainerCluster() *schema.Resource { ForceNew: true, ValidateFunc: validation.CIDRNetwork(28, 28), }, + + "resource_labels": { + Type: schema.TypeMap, + Optional: true, + Elem: schema.TypeString, + }, }, } } @@ -639,6 +645,14 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er } } + if v, ok := d.GetOk("resource_labels"); ok { + m := make(map[string]string) + for k, val := range v.(map[string]interface{}) { + m[k] = val.(string) + } + cluster.ResourceLabels = m + } + req := &containerBeta.CreateClusterRequest{ Cluster: cluster, } @@ -773,6 +787,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro d.Set("private_cluster", cluster.PrivateCluster) d.Set("master_ipv4_cidr_block", cluster.MasterIpv4CidrBlock) + d.Set("resource_labels", cluster.ResourceLabels) return nil } @@ -1129,6 +1144,31 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er d.SetPartial("pod_security_policy_config") } + if d.HasChange("resource_labels") { + resourceLabels := d.Get("resource_labels").(map[string]string) + + req := &containerBeta.SetLabelsRequest{ + ResourceLabels: resourceLabels, + } + updateF := func() error { + name := containerClusterFullName(project, location, clusterName) + op, err := config.clientContainerBeta.Projects.Locations.Clusters.SetResourceLabels(name, req).Do() + if err != nil { + return err + } + + // Wait until it's updated + return containerSharedOperationWait(config, op, project, location, "updating GKE resource labels", timeoutInMinutes, 2) + } + + // Call update serially. + if err := lockedCall(lockKey, updateF); err != nil { + return err + } + + d.SetPartial("resource_labels") + } + if d.HasChange("remove_default_node_pool") && d.Get("remove_default_node_pool").(bool) { name := fmt.Sprintf("%s/nodePools/%s", containerClusterFullName(project, location, clusterName), "default-pool") op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Delete(name).Do() diff --git a/google/resource_container_cluster_test.go b/google/resource_container_cluster_test.go index 6632e359..1c879a55 100644 --- a/google/resource_container_cluster_test.go +++ b/google/resource_container_cluster_test.go @@ -1160,6 +1160,29 @@ func TestAccContainerCluster_sharedVpc(t *testing.T) { }) } +func TestAccContainerCluster_withResourceLabels(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_withResourceLabels(clusterName), + }, + { + ResourceName: "google_container_cluster.with_resource_labels", + ImportStateIdPrefix: "us-central1-a/", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckContainerClusterDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -2229,3 +2252,17 @@ resource "google_container_cluster" "shared_vpc_cluster" { ] }`, projectName, org, billingId, projectName, org, billingId, acctest.RandString(10), acctest.RandString(10), name) } + +func testAccContainerCluster_withResourceLabels(clusterName string) string { + return fmt.Sprintf(` +resource "google_container_cluster" "with_resource_labels" { + name = "%s" + zone = "us-central1-a" + initial_node_count = 1 + + resource_labels { + created-by = "terraform" + } +} +`, clusterName) +} diff --git a/website/docs/r/container_cluster.html.markdown b/website/docs/r/container_cluster.html.markdown index 7be019c1..a7c5d088 100644 --- a/website/docs/r/container_cluster.html.markdown +++ b/website/docs/r/container_cluster.html.markdown @@ -172,6 +172,8 @@ output "cluster_ca_certificate" { * `remove_default_node_pool` - (Optional) If true, deletes the default node pool upon cluster creation. +* `resource_labels` - (Optional) The GCE resource labels (a map of key/value pairs) to be applied to the cluster. + * `subnetwork` - (Optional) The name or self_link of the Google Compute Engine subnetwork in which the cluster's instances are launched.