Skip to content

Commit

Permalink
[hashicorp#12620] Allow gke nodepool labels to be updated (hashicorp#…
Browse files Browse the repository at this point in the history
…6941)

Co-authored-by: Shuya Ma <87669292+shuyama1@users.noreply.github.com>
Co-authored-by: Luca Prete <lucaprete@google.com>
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
3 people committed Dec 19, 2022
1 parent 9e62bc4 commit 557d238
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .changelog/6941.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: supported in-place update for `labels` in `google_container_node_pool`
```
1 change: 0 additions & 1 deletion google/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func schemaNodeConfig() *schema.Schema {
Optional: true,
// Computed=true because GKE Sandbox will automatically add labels to nodes that can/cannot run sandboxed pods.
Computed: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
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.`,
},
Expand Down
1 change: 1 addition & 0 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ var (
}

forceNewClusterNodeConfigFields = []string{
"labels",
"workload_metadata_config",
}

Expand Down
38 changes: 38 additions & 0 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,44 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node
log.Printf("[INFO] Updated resource labels for node pool %s", name)
}

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

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

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 labels", userAgent,
timeout)
}

// Call update serially.
if err := retryWhileIncompatibleOperation(timeout, npLockKey, updateF); err != nil {
return err
}

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

if d.HasChange(prefix + "node_config.0.image_type") {
req := &container.UpdateClusterRequest{
Update: &container.ClusterUpdate{
Expand Down
35 changes: 22 additions & 13 deletions google/resource_container_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1606,12 +1606,6 @@ resource "google_container_node_pool" "np_with_node_config" {
]
preemptible = true
min_cpu_platform = "Intel Broadwell"
tags = ["ga"]
resource_labels = {
"key1" = "value"
}
taint {
key = "taint_key"
Expand All @@ -1627,6 +1621,16 @@ resource "google_container_node_pool" "np_with_node_config" {
// Updatable fields
image_type = "COS_CONTAINERD"
tags = ["foo"]
labels = {
"test.terraform.io/key1" = "foo"
}
resource_labels = {
"key1" = "foo"
}
}
}
`, cluster, nodePool)
Expand Down Expand Up @@ -1657,13 +1661,6 @@ resource "google_container_node_pool" "np_with_node_config" {
preemptible = true
min_cpu_platform = "Intel Broadwell"
tags = ["beta"]
resource_labels = {
"key1" = "value1"
"key2" = "value2"
}
taint {
key = "taint_key"
value = "taint_value"
Expand All @@ -1678,6 +1675,18 @@ resource "google_container_node_pool" "np_with_node_config" {
// Updatable fields
image_type = "UBUNTU_CONTAINERD"
tags = ["bar", "foobar"]
labels = {
"test.terraform.io/key1" = "bar"
"test.terraform.io/key2" = "foo"
}
resource_labels = {
"key1" = "bar"
"key2" = "foo"
}
}
}
`, cluster, nodePool)
Expand Down

0 comments on commit 557d238

Please sign in to comment.