Skip to content

Commit

Permalink
feat(google_container_cluster): add resource_labels to node_config (#…
Browse files Browse the repository at this point in the history
…6842) (#13104)

Fixes #13064

Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Nov 22, 2022
1 parent 9dfb774 commit 0ee63a8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/6842.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
container: added `resource_labels` field to `node_config` resource
```
16 changes: 16 additions & 0 deletions google/node_config.go
Expand Up @@ -149,6 +149,13 @@ func schemaNodeConfig() *schema.Schema {
Description: `The map of Kubernetes labels (key/value pairs) to be applied to each node. These will added in addition to any default label(s) that Kubernetes may apply to the node.`,
},

"resource_labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the node pool.`,
},

"local_ssd_count": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -525,6 +532,14 @@ func expandNodeConfig(v interface{}) *container.NodeConfig {
nc.Labels = m
}

if v, ok := nodeConfig["resource_labels"]; ok {
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
nc.ResourceLabels = m
}

if v, ok := nodeConfig["tags"]; ok {
tagsList := v.([]interface{})
tags := []string{}
Expand Down Expand Up @@ -638,6 +653,7 @@ func flattenNodeConfig(c *container.NodeConfig) []map[string]interface{} {
"metadata": c.Metadata,
"image_type": c.ImageType,
"labels": c.Labels,
"resource_labels": c.ResourceLabels,
"tags": c.Tags,
"preemptible": c.Preemptible,
"spot": c.Spot,
Expand Down
38 changes: 38 additions & 0 deletions google/resource_container_node_pool.go
Expand Up @@ -1143,6 +1143,44 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node
log.Printf("[INFO] Updated tags for node pool %s", name)
}

if d.HasChange(prefix + "node_config.0.resource_labels") {
req := &container.UpdateNodePoolRequest{
Name: name,
}

if v, ok := d.GetOk(prefix + "node_config.0.resource_labels"); ok {
resourceLabels := v.(map[string]interface{})
req.ResourceLabels = &container.ResourceLabels{
Labels: convertStringMap(resourceLabels),
}
}

updateF := func() error {
clusterNodePoolsUpdateCall := config.NewContainerClient(userAgent).Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req)
if config.UserProjectOverride {
clusterNodePoolsUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project)
}
op, err := clusterNodePoolsUpdateCall.Do()
if err != nil {
return err
}

// Wait until it's updated
return containerOperationWait(config, op,
nodePoolInfo.project,
nodePoolInfo.location,
"updating GKE node pool resource labels", userAgent,
timeout)
}

// Call update serially.
if err := lockedCall(lockKey, updateF); err != nil {
return err
}

log.Printf("[INFO] Updated resource labels for node pool %s", name)
}

if d.HasChange(prefix + "node_config.0.image_type") {
req := &container.UpdateClusterRequest{
Update: &container.ClusterUpdate{
Expand Down
9 changes: 9 additions & 0 deletions google/resource_container_node_pool_test.go
Expand Up @@ -1427,6 +1427,10 @@ resource "google_container_node_pool" "np_with_node_config" {
tags = ["ga"]
resource_labels = {
"key1" = "value"
}
taint {
key = "taint_key"
value = "taint_value"
Expand Down Expand Up @@ -1473,6 +1477,11 @@ resource "google_container_node_pool" "np_with_node_config" {
tags = ["beta"]
resource_labels = {
"key1" = "value1"
"key2" = "value2"
}
taint {
key = "taint_key"
value = "taint_value"
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/container_cluster.html.markdown
Expand Up @@ -750,6 +750,9 @@ gvnic {
* `labels` - (Optional) The Kubernetes labels (key/value pairs) to be applied to each node. The kubernetes.io/ and k8s.io/ prefixes are
reserved by Kubernetes Core components and cannot be specified.

* `resource_labels` - (Optional) The GCP labels (key/value pairs) to be applied to each node. Refer [here](https://cloud.google.com/kubernetes-engine/docs/how-to/creating-managing-labels)
for how these labels are applied to clusters, node pools and nodes.

* `local_ssd_count` - (Optional) The amount of local SSD disks that will be
attached to each cluster node. Defaults to 0.

Expand Down

0 comments on commit 0ee63a8

Please sign in to comment.