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

WIP: Ability to add/remove node pools from a GKE cluster #779

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 11 additions & 12 deletions google/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,56 @@ var schemaNodeConfig = &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(10),
},

"image_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"labels": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: schema.TypeString,
},

"local_ssd_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(0),
},

"machine_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"metadata": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: schema.TypeString,
},

"min_cpu_platform": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"oauth_scopes": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
StateFunc: func(v interface{}) string {
Expand All @@ -81,27 +72,35 @@ var schemaNodeConfig = &schema.Schema{
"preemptible": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},

"service_account": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"tags": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}

func addForceNew(s *schema.Schema) *schema.Schema {
newSchema := *s
newSchema.ForceNew = true
if el, ok := newSchema.Elem.(*schema.Resource); ok {
for k, v := range el.Schema {
el.Schema[k] = addForceNew(v)
}
}
return &newSchema
}

func expandNodeConfig(v interface{}) *container.NodeConfig {
nodeConfigs := v.([]interface{})
nodeConfig := nodeConfigs[0].(map[string]interface{})
Expand Down
97 changes: 86 additions & 11 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,27 @@ func resourceContainerCluster() *schema.Resource {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true, // TODO(danawillow): Add ability to add/remove nodePools
Elem: &schema.Resource{
Schema: schemaNodePool,
Schema: mergeSchemas(
schemaNodePool,
map[string]*schema.Schema{
"initial_node_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
Deprecated: "Use node_count instead",
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"node_config": schemaNodeConfig,
}),
},
},

Expand Down Expand Up @@ -612,6 +630,72 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er

d.Partial(true)

// Add/remove node pools first in case any other changes affect all nodes
if d.HasChange("node_pool") {
o, n := d.GetChange("node_pool")

getNpNames := func(config interface{}) *schema.Set {
names := []interface{}{}
for _, np := range config.([]interface{}) {
npConfig := np.(map[string]interface{})
names = append(names, npConfig["name"])
}
return schema.NewSet(schema.HashString, names)
}

oNps := getNpNames(o)
nNps := getNpNames(n)

// Delete all the node pools that were previously there but no longer are
for _, np := range oNps.Difference(nNps).List() {
npName := np.(string)
op, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Delete(project, zoneName, clusterName, npName).Do()
if err != nil {
return fmt.Errorf("Error deleting NodePool: %s", err)
}

// Wait until it's deleted
waitErr := containerOperationWait(config, op, project, zoneName, "deleting GKE NodePool", timeoutInMinutes, 2)
if waitErr != nil {
return waitErr
}
log.Printf("[INFO] Node pool %q has been removed from GKE cluster %q", npName, clusterName)
}

// Add the new node pools and update existing ones
for i, np := range n.([]interface{}) {
name := np.(map[string]interface{})["name"].(string)
prefix := fmt.Sprintf("node_pool.%d.", i)

if !oNps.Contains(name) {
nodePool, err := expandNodePool(d, prefix)
if err != nil {
return err
}

req := &container.CreateNodePoolRequest{
NodePool: nodePool,
}
op, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Create(project, zoneName, clusterName, req).Do()
if err != nil {
return fmt.Errorf("Error creating NodePool: %s", err)
}

waitErr := containerOperationWait(config, op, project, zoneName, "creating GKE NodePool", timeoutInMinutes, 3)
if waitErr != nil {
return waitErr
}
log.Printf("[INFO] Node pool %q has been added to GKE cluster %q", nodePool.Name, clusterName)
} else {
if err := nodePoolUpdate(d, meta, clusterName, prefix, timeoutInMinutes); err != nil {
return err
}
}
}

d.SetPartial("node_pool")
}

if d.HasChange("master_authorized_networks_config") {
c := d.Get("master_authorized_networks_config")
req := &container.UpdateClusterRequest{
Expand Down Expand Up @@ -802,15 +886,6 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
d.SetPartial("monitoring_service")
}

if n, ok := d.GetOk("node_pool.#"); ok {
for i := 0; i < n.(int); i++ {
if err := nodePoolUpdate(d, meta, clusterName, fmt.Sprintf("node_pool.%d.", i), timeoutInMinutes); err != nil {
return err
}
}
d.SetPartial("node_pool")
}

if d.HasChange("logging_service") {
logging := d.Get("logging_service").(string)

Expand Down
45 changes: 22 additions & 23 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ func resourceContainerNodePool() *schema.Resource {
Required: true,
ForceNew: true,
},

// The following fields are specified separately because they are ForceNew
// in resource_container_node_pool but not in resource_container_cluster.
"initial_node_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
Deprecated: "Use node_count instead",
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"node_config": addForceNew(schemaNodeConfig),
}),
}
}
Expand All @@ -77,14 +99,6 @@ var schemaNodePool = map[string]*schema.Schema{
},
},

"initial_node_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
Deprecated: "Use node_count instead",
},

"management": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -107,21 +121,6 @@ var schemaNodePool = map[string]*schema.Schema{
},
},

"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"name_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"node_config": schemaNodeConfig,

"node_count": {
Type: schema.TypeInt,
Optional: true,
Expand Down