Skip to content

Commit

Permalink
[#12620] Allow gke nodepool labels to be updated (#6941) (#4998)
Browse files Browse the repository at this point in the history
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>

Signed-off-by: Modular Magician <magic-modules@google.com>
Co-authored-by: Shuya Ma <87669292+shuyama1@users.noreply.github.com>
Co-authored-by: Luca Prete <lucaprete@google.com>
  • Loading branch information
3 people authored Dec 19, 2022
1 parent bbf1274 commit 5a2906d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 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-beta/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,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.`,
DiffSuppressFunc: containerNodePoolLabelsSuppress,
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var (
}

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

Expand Down
2 changes: 1 addition & 1 deletion google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5001,7 +5001,7 @@ resource "google_container_cluster" "with_sandbox_config" {
labels = {
"test.terraform.io/gke-sandbox" = "true"
"test.terraform.io/gke-sandbox-amended" = "also-true"
"test.terraform.io/gke-sandbox-amended" = "also-true"
}
taint {
Expand Down
38 changes: 38 additions & 0 deletions google-beta/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,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-beta/resource_container_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1871,12 +1871,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 @@ -1892,6 +1886,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 @@ -1922,13 +1926,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 @@ -1943,6 +1940,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 5a2906d

Please sign in to comment.