Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for node pool versions #1266

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,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 @@ -385,6 +391,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 @@ -437,6 +444,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 @@ -583,6 +591,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 @@ -312,6 +312,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 @@ -600,3 +639,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