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 resizing a node pool defined in google_container_cluster #331

Merged
merged 2 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 55 additions & 14 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func resourceContainerCluster() *schema.Resource {
Delete: schema.DefaultTimeout(10 * time.Minute),
},

SchemaVersion: 1,
SchemaVersion: 2,
MigrateState: resourceContainerClusterMigrateState,

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -236,10 +236,9 @@ func resourceContainerCluster() *schema.Resource {
ForceNew: true, // TODO(danawillow): Add ability to add/remove nodePools
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"initial_node_count": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep the old field and marked it as deprecated or removed? this way, we can show an error message to the user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"node_count": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},

"name": {
Expand Down Expand Up @@ -374,7 +373,8 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
nodePools := make([]*container.NodePool, 0, nodePoolsCount)
for i := 0; i < nodePoolsCount; i++ {
prefix := fmt.Sprintf("node_pool.%d", i)
nodeCount := d.Get(prefix + ".initial_node_count").(int)

nodeCount := d.Get(prefix + ".node_count").(int)

name, err := generateNodePoolName(prefix, d)
if err != nil {
Expand Down Expand Up @@ -472,7 +472,11 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
d.Set("network", d.Get("network").(string))
d.Set("subnetwork", cluster.Subnetwork)
d.Set("node_config", flattenClusterNodeConfig(cluster.NodeConfig))
d.Set("node_pool", flattenClusterNodePools(d, cluster.NodePools))
nps, err := flattenClusterNodePools(d, config, cluster.NodePools)
if err != nil {
return err
}
d.Set("node_pool", nps)

if igUrls, err := getInstanceGroupUrlsFromManagerUrls(config, cluster.InstanceGroupUrls); err != nil {
return err
Expand Down Expand Up @@ -597,6 +601,32 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
d.SetPartial("enable_legacy_abac")
}

if n, ok := d.GetOk("node_pool.#"); ok {
for i := 0; i < n.(int); i++ {
if d.HasChange(fmt.Sprintf("node_pool.%d.node_count", i)) {
newSize := int64(d.Get(fmt.Sprintf("node_pool.%d.node_count", i)).(int))
req := &container.SetNodePoolSizeRequest{
NodeCount: newSize,
}
npName := d.Get(fmt.Sprintf("node_pool.%d.name", i)).(string)
op, err := config.clientContainer.Projects.Zones.Clusters.NodePools.SetSize(project, zoneName, clusterName, npName, req).Do()
if err != nil {
return err
}

// Wait until it's updated
waitErr := containerOperationWait(config, op, project, zoneName, "updating GKE node pool size", timeoutInMinutes, 2)
if waitErr != nil {
return waitErr
}

log.Printf("[INFO] GKE node pool %s size has been updated to %d", npName, newSize)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove extra line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}
d.SetPartial("node_pool")
}

d.Partial(false)

return resourceContainerClusterRead(d, meta)
Expand Down Expand Up @@ -679,22 +709,33 @@ func flattenClusterNodeConfig(c *container.NodeConfig) []map[string]interface{}
return config
}

func flattenClusterNodePools(d *schema.ResourceData, c []*container.NodePool) []map[string]interface{} {
count := len(c)

nodePools := make([]map[string]interface{}, 0, count)
func flattenClusterNodePools(d *schema.ResourceData, config *Config, c []*container.NodePool) ([]map[string]interface{}, error) {
nodePools := make([]map[string]interface{}, 0, len(c))

for i, np := range c {
// Node pools don't expose the current node count in their API, so read the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we talked with the GKE folks about updating their API to include the node count?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup (I can show you the thread off-github)

// instance groups instead. They should all have the same size, but in case a resize
// failed or something else strange happened, we'll just use the average size.
size := 0
for _, url := range np.InstanceGroupUrls {
// retrieve instance group manager (InstanceGroupUrls are actually URLs for InstanceGroupManagers)
matches := instanceGroupManagerURL.FindStringSubmatch(url)
igm, err := config.clientCompute.InstanceGroupManagers.Get(matches[1], matches[2], matches[3]).Do()
if err != nil {
return nil, fmt.Errorf("Error reading instance group manager returned as an instance group URL: %s", err)
}
size += int(igm.TargetSize)
}
nodePool := map[string]interface{}{
"name": np.Name,
"name_prefix": d.Get(fmt.Sprintf("node_pool.%d.name_prefix", i)),
"initial_node_count": np.InitialNodeCount,
"node_config": flattenClusterNodeConfig(np.Config),
"name": np.Name,
"name_prefix": d.Get(fmt.Sprintf("node_pool.%d.name_prefix", i)),
"node_config": flattenClusterNodeConfig(np.Config),
"node_count": size / len(np.InstanceGroupUrls),
}
nodePools = append(nodePools, nodePool)
}

return nodePools
return nodePools, nil
}

func generateNodePoolName(prefix string, d *schema.ResourceData) (string, error) {
Expand Down
23 changes: 23 additions & 0 deletions google/resource_container_cluster_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func resourceContainerClusterMigrateState(
case 0:
log.Println("[INFO] Found Container Cluster State v0; migrating to v1")
return migrateClusterStateV0toV1(is)
case 1:
log.Println("[INFO] Found Container Cluster State v1; migrating to v2")
return migrateClusterStateV1toV2(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
Expand Down Expand Up @@ -68,3 +71,23 @@ func migrateClusterStateV0toV1(is *terraform.InstanceState) (*terraform.Instance
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}

func migrateClusterStateV1toV2(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)

for k, v := range is.Attributes {
if !strings.HasPrefix(k, "node_pool.") {
continue
}
if !strings.HasSuffix(k, ".initial_node_count") {
continue
}

is.Attributes[strings.Replace(k, "initial_node_count", "node_count", 1)] = v

delete(is.Attributes, k)
}

log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
14 changes: 14 additions & 0 deletions google/resource_container_cluster_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ func TestContainerClusterMigrateState(t *testing.T) {
},
Meta: &Config{},
},
"rename node_pool.initial_node_count to node_pool.node_count": {
StateVersion: 1,
Attributes: map[string]string{
"node_pool.#": "2",
"node_pool.0.initial_node_count": "3",
"node_pool.1.initial_node_count": "2",
},
Expected: map[string]string{
"node_pool.#": "2",
"node_pool.0.node_count": "3",
"node_pool.1.node_count": "2",
},
Meta: &Config{},
},
}

for tn, tc := range cases {
Expand Down
95 changes: 79 additions & 16 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

"strconv"

"regexp"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"regexp"
)

func TestAccContainerCluster_basic(t *testing.T) {
Expand Down Expand Up @@ -252,6 +253,34 @@ func TestAccContainerCluster_withNodePoolBasic(t *testing.T) {
})
}

func TestAccContainerCluster_withNodePoolResize(t *testing.T) {
clusterName := fmt.Sprintf("tf-cluster-nodepool-test-%s", acctest.RandString(10))
npName := fmt.Sprintf("tf-cluster-nodepool-test-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withNodePoolAdditionalZones(clusterName, npName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster(
"google_container_cluster.with_node_pool"),
resource.TestCheckResourceAttr("google_container_cluster.with_node_pool", "node_pool.0.node_count", "2"),
),
},
{
Config: testAccContainerCluster_withNodePoolResize(clusterName, npName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster(
"google_container_cluster.with_node_pool"),
resource.TestCheckResourceAttr("google_container_cluster.with_node_pool", "node_pool.0.node_count", "3"),
),
},
},
})
}

func TestAccContainerCluster_withNodePoolNamePrefix(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -416,9 +445,7 @@ func testAccCheckContainerCluster(n string) resource.TestCheckFunc {

for i, np := range cluster.NodePools {
prefix := fmt.Sprintf("node_pool.%d.", i)
clusterTests = append(clusterTests,
clusterTestField{prefix + "name", np.Name},
clusterTestField{prefix + "initial_node_count", strconv.FormatInt(np.InitialNodeCount, 10)})
clusterTests = append(clusterTests, clusterTestField{prefix + "name", np.Name})
if np.Config != nil {
clusterTests = append(clusterTests,
clusterTestField{prefix + "node_config.0.machine_type", np.Config.MachineType},
Expand Down Expand Up @@ -822,11 +849,47 @@ resource "google_container_cluster" "with_node_pool" {
}

node_pool {
name = "tf-cluster-nodepool-test-%s"
initial_node_count = 2
name = "tf-cluster-nodepool-test-%s"
node_count = 2
}
}`, acctest.RandString(10), acctest.RandString(10))

func testAccContainerCluster_withNodePoolAdditionalZones(cluster, nodePool string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_node_pool" {
name = "%s"
zone = "us-central1-a"

additional_zones = [
"us-central1-b",
"us-central1-c"
]

node_pool {
name = "%s"
node_count = 2
}
}`, cluster, nodePool)
}

func testAccContainerCluster_withNodePoolResize(cluster, nodePool string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_node_pool" {
name = "%s"
zone = "us-central1-a"

additional_zones = [
"us-central1-b",
"us-central1-c"
]

node_pool {
name = "%s"
node_count = 3
}
}`, cluster, nodePool)
}

var testAccContainerCluster_withNodePoolNamePrefix = fmt.Sprintf(`
resource "google_container_cluster" "with_node_pool_name_prefix" {
name = "tf-cluster-nodepool-test-%s"
Expand All @@ -838,8 +901,8 @@ resource "google_container_cluster" "with_node_pool_name_prefix" {
}

node_pool {
name_prefix = "tf-np-test"
initial_node_count = 2
name_prefix = "tf-np-test"
node_count = 2
}
}`, acctest.RandString(10))

Expand All @@ -854,13 +917,13 @@ resource "google_container_cluster" "with_node_pool_multiple" {
}

node_pool {
name = "tf-cluster-nodepool-test-%s"
initial_node_count = 2
name = "tf-cluster-nodepool-test-%s"
node_count = 2
}

node_pool {
name = "tf-cluster-nodepool-test-%s"
initial_node_count = 3
name = "tf-cluster-nodepool-test-%s"
node_count = 3
}
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))

Expand All @@ -876,9 +939,9 @@ resource "google_container_cluster" "with_node_pool_multiple" {

node_pool {
# ERROR: name and name_prefix cannot be both specified
name = "tf-cluster-nodepool-test-%s"
name_prefix = "tf-cluster-nodepool-test-"
initial_node_count = 1
name = "tf-cluster-nodepool-test-%s"
name_prefix = "tf-cluster-nodepool-test-"
node_count = 1
}
}`, acctest.RandString(10), acctest.RandString(10))

Expand All @@ -890,7 +953,7 @@ resource "google_container_cluster" "with_node_pool_node_config" {
zone = "us-central1-a"
node_pool {
name = "tf-cluster-nodepool-test-%s"
initial_node_count = 2
node_count = 2
node_config {
machine_type = "n1-standard-1"
disk_size_gb = 15
Expand Down