Skip to content

Commit

Permalink
add support for node pool versions (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
danawillow authored Mar 29, 2018
1 parent 3a65aae commit 33d87a2
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
36 changes: 36 additions & 0 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
85 changes: 85 additions & 0 deletions google/resource_container_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions website/docs/r/container_node_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 33d87a2

Please sign in to comment.