Added ability to configure resource labels on a GKE cluster

This commit is contained in:
Gareth Evans 2018-06-15 11:10:25 +01:00
parent 43cccf3e2c
commit e14aa8bf39
3 changed files with 74 additions and 0 deletions

View File

@ -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()

View File

@ -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)
}

View File

@ -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.