diff --git a/google/resource_container_cluster.go b/google/resource_container_cluster.go index 54adaaf4..3f6feb60 100644 --- a/google/resource_container_cluster.go +++ b/google/resource_container_cluster.go @@ -461,6 +461,12 @@ func resourceContainerCluster() *schema.Resource { ForceNew: true, ValidateFunc: validation.CIDRNetwork(28, 28), }, + + "resource_labels": { + Type: schema.TypeMap, + Optional: true, + Elem: schema.TypeString, + }, }, } } @@ -638,6 +644,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er } } + if v, ok := d.GetOk("resource_labels"); ok { + cluster.ResourceLabels = v.(map[string]string) + } + req := &containerBeta.CreateClusterRequest{ Cluster: cluster, } @@ -787,6 +797,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 } @@ -1143,6 +1154,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..06e118c8 100644 --- a/google/resource_container_cluster_test.go +++ b/google/resource_container_cluster_test.go @@ -1160,6 +1160,27 @@ func TestAccContainerCluster_sharedVpc(t *testing.T) { }) } +func TestAccContainerCluster_withResourceLabels(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckContainerClusterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccContainerCluster_withResourceLabels(), + }, + { + 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 +2250,18 @@ resource "google_container_cluster" "shared_vpc_cluster" { ] }`, projectName, org, billingId, projectName, org, billingId, acctest.RandString(10), acctest.RandString(10), name) } + +func testAccContainerCluster_withResourceLabels() string { + testId := acctest.RandString(10) + return fmt.Sprintf(` +resource "google_container_cluster" "with_resource_labels" { + name = "tf-cluster-resourcelabel-test-%s" + zone = "us-central1-a" + initial_node_count = 1 + + resource_labels { + created-by = "terraform" + } +} +`, testId) +} diff --git a/website/docs/r/container_cluster.html.markdown b/website/docs/r/container_cluster.html.markdown index 72fdc59b..56b03354 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 (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.