From 6eb180597cc4f57f7547deb2685cb4ec7e10040d Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 12 Oct 2022 11:19:33 -0700 Subject: [PATCH] Adding update support for labels to google_container_node_pool (#6599) (#12773) Signed-off-by: Modular Magician Signed-off-by: Modular Magician --- .changelog/6599.txt | 3 ++ google/node_config.go | 1 - google/resource_container_node_pool.go | 55 +++++++++++++++++++++ google/resource_container_node_pool_test.go | 4 ++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .changelog/6599.txt diff --git a/.changelog/6599.txt b/.changelog/6599.txt new file mode 100644 index 0000000000..12e321b45b --- /dev/null +++ b/.changelog/6599.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +container: added support for in-place update of `node_config.0.tags` for `google_container_node_pool` resource +``` diff --git a/google/node_config.go b/google/node_config.go index 714b6fe847..1f5b7de37a 100644 --- a/google/node_config.go +++ b/google/node_config.go @@ -268,7 +268,6 @@ func schemaNodeConfig() *schema.Schema { "tags": { Type: schema.TypeList, Optional: true, - ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, Description: `The list of instance tags applied to all nodes.`, }, diff --git a/google/resource_container_node_pool.go b/google/resource_container_node_pool.go index 6c985e3dea..757f1c0fb2 100644 --- a/google/resource_container_node_pool.go +++ b/google/resource_container_node_pool.go @@ -910,6 +910,61 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node } if d.HasChange(prefix + "node_config") { + + if d.HasChange(prefix + "node_config.0.tags") { + req := &container.UpdateNodePoolRequest{ + Name: name, + } + if v, ok := d.GetOk(prefix + "node_config.0.tags"); ok { + tagsList := v.([]interface{}) + tags := []string{} + for _, v := range tagsList { + if v != nil { + tags = append(tags, v.(string)) + } + } + ntags := &container.NetworkTags{ + Tags: tags, + } + req.Tags = ntags + } + + // sets tags to the empty list when user removes a previously defined list of tags entriely + // aka the node pool goes from having tags to no longer having any + if req.Tags == nil { + tags := []string{} + ntags := &container.NetworkTags{ + Tags: tags, + } + req.Tags = ntags + } + + 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 tags", userAgent, + timeout) + } + + // Call update serially. + if err := lockedCall(lockKey, updateF); err != nil { + return err + } + + log.Printf("[INFO] Updated tags for node pool %s", name) + } + if d.HasChange(prefix + "node_config.0.image_type") { req := &container.UpdateClusterRequest{ Update: &container.ClusterUpdate{ diff --git a/google/resource_container_node_pool_test.go b/google/resource_container_node_pool_test.go index f22f655d0e..544569789a 100644 --- a/google/resource_container_node_pool_test.go +++ b/google/resource_container_node_pool_test.go @@ -1349,6 +1349,8 @@ resource "google_container_node_pool" "np_with_node_config" { ] preemptible = true min_cpu_platform = "Intel Broadwell" + + tags = ["ga"] taint { key = "taint_key" @@ -1394,6 +1396,8 @@ resource "google_container_node_pool" "np_with_node_config" { preemptible = true min_cpu_platform = "Intel Broadwell" + tags = ["beta"] + taint { key = "taint_key" value = "taint_value"