Skip to content

Commit

Permalink
Add support node config for GKE node pool (hashicorp#184)
Browse files Browse the repository at this point in the history
* Add support node config for GKE node pool

* Review fixes:
- Set max items in node config schema
- Fill missing node config fields
- Put test helpers above than test vars

* Update checks in node pool tests

* Fix node pool check match
  • Loading branch information
aadidenko authored and danawillow committed Jul 31, 2017
1 parent bc20268 commit a9626a8
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 112 deletions.
105 changes: 105 additions & 0 deletions google/node_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

var schemaNodeConfig = &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"machine_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)

if value < 10 {
errors = append(errors, fmt.Errorf(
"%q cannot be less than 10", k))
}
return
},
},

"local_ssd_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)

if value < 0 {
errors = append(errors, fmt.Errorf(
"%q cannot be negative", k))
}
return
},
},

"oauth_scopes": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
StateFunc: func(v interface{}) string {
return canonicalizeServiceScope(v.(string))
},
},
},

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

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

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

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

"tags": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
99 changes: 1 addition & 98 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,102 +214,8 @@ func resourceContainerCluster() *schema.Resource {
},
},
},
"node_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"machine_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)

if value < 10 {
errors = append(errors, fmt.Errorf(
"%q cannot be less than 10", k))
}
return
},
},

"local_ssd_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)

if value < 0 {
errors = append(errors, fmt.Errorf(
"%q cannot be negative", k))
}
return
},
},

"oauth_scopes": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
StateFunc: func(v interface{}) string {
return canonicalizeServiceScope(v.(string))
},
},
},

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

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

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

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

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

"node_version": {
Type: schema.TypeString,
Expand Down Expand Up @@ -449,9 +355,6 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
}
if v, ok := d.GetOk("node_config"); ok {
nodeConfigs := v.([]interface{})
if len(nodeConfigs) > 1 {
return fmt.Errorf("Cannot specify more than one node_config.")
}
nodeConfig := nodeConfigs[0].(map[string]interface{})

cluster.NodeConfig = &container.NodeConfig{}
Expand Down
65 changes: 65 additions & 0 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func resourceContainerNodePool() *schema.Resource {
ForceNew: true,
},

"node_config": schemaNodeConfig,

"autoscaling": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -121,6 +123,68 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e
InitialNodeCount: int64(nodeCount),
}

if v, ok := d.GetOk("node_config"); ok {
nodeConfigs := v.([]interface{})
nodeConfig := nodeConfigs[0].(map[string]interface{})

nodePool.Config = &container.NodeConfig{}

if v, ok = nodeConfig["machine_type"]; ok {
nodePool.Config.MachineType = v.(string)
}

if v, ok = nodeConfig["disk_size_gb"]; ok {
nodePool.Config.DiskSizeGb = int64(v.(int))
}

if v, ok = nodeConfig["local_ssd_count"]; ok {
nodePool.Config.LocalSsdCount = int64(v.(int))
}

if v, ok := nodeConfig["oauth_scopes"]; ok {
scopesList := v.([]interface{})
scopes := []string{}
for _, v := range scopesList {
scopes = append(scopes, canonicalizeServiceScope(v.(string)))
}

nodePool.Config.OauthScopes = scopes
}

if v, ok = nodeConfig["service_account"]; ok {
nodePool.Config.ServiceAccount = v.(string)
}

if v, ok = nodeConfig["metadata"]; ok {
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
nodePool.Config.Metadata = m
}

if v, ok = nodeConfig["image_type"]; ok {
nodePool.Config.ImageType = v.(string)
}

if v, ok = nodeConfig["labels"]; ok {
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
nodePool.Config.Labels = m
}

if v, ok := nodeConfig["tags"]; ok {
tagsList := v.([]interface{})
tags := []string{}
for _, v := range tagsList {
tags = append(tags, v.(string))
}
nodePool.Config.Tags = tags
}
}

if v, ok := d.GetOk("autoscaling"); ok {
autoscaling := v.([]interface{})[0].(map[string]interface{})
nodePool.Autoscaling = &container.NodePoolAutoscaling{
Expand Down Expand Up @@ -174,6 +238,7 @@ func resourceContainerNodePoolRead(d *schema.ResourceData, meta interface{}) err

d.Set("name", nodePool.Name)
d.Set("initial_node_count", nodePool.InitialNodeCount)
d.Set("node_config", flattenClusterNodeConfig(nodePool.Config))

autoscaling := []map[string]interface{}{}
if nodePool.Autoscaling != nil && nodePool.Autoscaling.Enabled {
Expand Down
Loading

0 comments on commit a9626a8

Please sign in to comment.