From 33d87a2bc18f02704dc60e77f971c6c01eb8b062 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Thu, 29 Mar 2018 13:27:11 -0700 Subject: [PATCH] add support for node pool versions (#1266) --- google/resource_container_node_pool.go | 36 ++++++++ google/resource_container_node_pool_test.go | 85 +++++++++++++++++++ .../docs/r/container_node_pool.html.markdown | 4 + 3 files changed, 125 insertions(+) diff --git a/google/resource_container_node_pool.go b/google/resource_container_node_pool.go index ac1576102aa..5dbbaa4f3b9 100644 --- a/google/resource_container_node_pool.go +++ b/google/resource_container_node_pool.go @@ -144,6 +144,12 @@ var schemaNodePool = map[string]*schema.Schema{ Computed: true, ValidateFunc: validation.IntAtLeast(0), }, + + "version": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, } func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) error { @@ -386,6 +392,7 @@ func expandNodePool(d *schema.ResourceData, prefix string) (*containerBeta.NodeP Name: name, InitialNodeCount: int64(nodeCount), Config: expandNodeConfig(d.Get(prefix + "node_config")), + Version: d.Get("version").(string), } if v, ok := d.GetOk(prefix + "autoscaling"); ok { @@ -438,6 +445,7 @@ func flattenNodePool(d *schema.ResourceData, config *Config, np *containerBeta.N "node_count": size / len(np.InstanceGroupUrls), "node_config": flattenNodeConfig(np.Config), "instance_group_urls": np.InstanceGroupUrls, + "version": np.Version, } if np.Autoscaling != nil && np.Autoscaling.Enabled { @@ -584,6 +592,34 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, clusterName, prefi } } + if d.HasChange(prefix + "version") { + req := &container.UpdateNodePoolRequest{ + NodeVersion: d.Get("version").(string), + } + updateF := func() error { + op, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Update( + project, zone, clusterName, npName, req).Do() + + if err != nil { + return err + } + + // Wait until it's updated + return containerOperationWait(config, op, project, zone, "updating GKE node pool version", timeoutInMinutes, 2) + } + + // Call update serially. + if err := lockedCall(lockKey, updateF); err != nil { + return err + } + + log.Printf("[INFO] Updated version in Node Pool %s", npName) + + if prefix == "" { + d.SetPartial("version") + } + } + return nil } diff --git a/google/resource_container_node_pool_test.go b/google/resource_container_node_pool_test.go index c1649957cbf..dfd37459f49 100644 --- a/google/resource_container_node_pool_test.go +++ b/google/resource_container_node_pool_test.go @@ -332,6 +332,45 @@ func TestAccContainerNodePool_resize(t *testing.T) { }) } +func TestAccContainerNodePool_version(t *testing.T) { + t.Parallel() + + cluster := fmt.Sprintf("tf-nodepool-test-%s", acctest.RandString(10)) + np := fmt.Sprintf("tf-nodepool-test-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckContainerClusterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccContainerNodePool_version(cluster, np), + }, + { + ResourceName: "google_container_node_pool.np", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccContainerNodePool_updateVersion(cluster, np), + }, + { + ResourceName: "google_container_node_pool.np", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccContainerNodePool_version(cluster, np), + }, + { + ResourceName: "google_container_node_pool.np", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckContainerNodePoolDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -647,3 +686,49 @@ resource "google_container_node_pool" "np_with_node_config_scope_alias" { } }`, acctest.RandString(10), acctest.RandString(10)) } + +func testAccContainerNodePool_version(cluster, np string) string { + return fmt.Sprintf(` +data "google_container_engine_versions" "central1a" { + zone = "us-central1-a" +} + +resource "google_container_cluster" "cluster" { + name = "%s" + zone = "us-central1-a" + initial_node_count = 1 + min_master_version = "${data.google_container_engine_versions.central1a.latest_master_version}" +} + +resource "google_container_node_pool" "np" { + name = "%s" + zone = "us-central1-a" + cluster = "${google_container_cluster.cluster.name}" + initial_node_count = 1 + + version = "${data.google_container_engine_versions.central1a.valid_node_versions.1}" +}`, cluster, np) +} + +func testAccContainerNodePool_updateVersion(cluster, np string) string { + return fmt.Sprintf(` +data "google_container_engine_versions" "central1a" { + zone = "us-central1-a" +} + +resource "google_container_cluster" "cluster" { + name = "%s" + zone = "us-central1-a" + initial_node_count = 1 + min_master_version = "${data.google_container_engine_versions.central1a.latest_master_version}" +} + +resource "google_container_node_pool" "np" { + name = "%s" + zone = "us-central1-a" + cluster = "${google_container_cluster.cluster.name}" + initial_node_count = 1 + + version = "${data.google_container_engine_versions.central1a.valid_node_versions.0}" +}`, cluster, np) +} diff --git a/website/docs/r/container_node_pool.html.markdown b/website/docs/r/container_node_pool.html.markdown index ee2fe06d6bd..886fafb79f9 100644 --- a/website/docs/r/container_node_pool.html.markdown +++ b/website/docs/r/container_node_pool.html.markdown @@ -121,6 +121,10 @@ resource "google_container_cluster" "primary" { * `project` - (Optional) The ID of the project in which to create the node pool. If blank, the provider-configured project will be used. +* `version` - (Optional) The Kubernetes version for the nodes in this pool. Note that if this field + and `auto_upgrade` are both specified, they will fight each other for what the node version should + be, so setting both is highly discouraged. + The `autoscaling` block supports: * `min_node_count` - (Required) Minimum number of nodes in the NodePool. Must be >=1 and