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

resource/aws_eks_node_group: add support for node_group_name prefixes #13938

Merged
merged 5 commits into from
May 12, 2021
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
3 changes: 3 additions & 0 deletions .changelog/13938.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_eks_node_group: Add `node_group_name_prefix` argument
```
25 changes: 25 additions & 0 deletions aws/internal/service/eks/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eks

import (
"fmt"
"strings"
)

const nodeGroupResourceIDSeparator = ":"

func NodeGroupCreateResourceID(clusterName, nodeGroupName string) string {
parts := []string{clusterName, nodeGroupName}
id := strings.Join(parts, nodeGroupResourceIDSeparator)

return id
}

func NodeGroupParseResourceID(id string) (string, string, error) {
parts := strings.Split(id, nodeGroupResourceIDSeparator)

if len(parts) == 2 && parts[0] != "" && parts[1] != "" {
return parts[0], parts[1], nil
}

return "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected cluster-name%[2]snode-group-name", id, nodeGroupResourceIDSeparator)
}
106 changes: 49 additions & 57 deletions aws/resource_aws_eks_node_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/naming"
tfeks "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/eks"
)

func resourceAwsEksNodeGroup() *schema.Resource {
Expand All @@ -35,15 +37,11 @@ func resourceAwsEksNodeGroup() *schema.Resource {

Schema: map[string]*schema.Schema{
"ami_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
eks.AMITypesAl2X8664,
eks.AMITypesAl2X8664Gpu,
eks.AMITypesAl2Arm64,
}, false),
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(eks.AMITypes_Values(), false),
},
"arn": {
Type: schema.TypeString,
Expand Down Expand Up @@ -115,10 +113,18 @@ func resourceAwsEksNodeGroup() *schema.Resource {
},
},
"node_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"node_group_name_prefix"},
},
"node_group_name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"node_group_name"},
},
"node_role_arn": {
Type: schema.TypeString,
Expand Down Expand Up @@ -226,9 +232,9 @@ func resourceAwsEksNodeGroupCreate(d *schema.ResourceData, meta interface{}) err
conn := meta.(*AWSClient).eksconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))
clusterName := d.Get("cluster_name").(string)
nodeGroupName := d.Get("node_group_name").(string)

clusterName := d.Get("cluster_name").(string)
nodeGroupName := naming.Generate(d.Get("node_group_name").(string), d.Get("node_group_name_prefix").(string))
input := &eks.CreateNodegroupInput{
ClientRequestToken: aws.String(resource.UniqueId()),
ClusterName: aws.String(clusterName),
Expand Down Expand Up @@ -283,13 +289,11 @@ func resourceAwsEksNodeGroupCreate(d *schema.ResourceData, meta interface{}) err

_, err := conn.CreateNodegroup(input)

id := resourceAwsEksNodeGroupCreateId(clusterName, nodeGroupName)

if err != nil {
return fmt.Errorf("error creating EKS Node Group (%s): %s", id, err)
return fmt.Errorf("error creating EKS Node Group (%s/%s): %w", clusterName, nodeGroupName, err)
}

d.SetId(id)
d.SetId(tfeks.NodeGroupCreateResourceID(clusterName, nodeGroupName))

stateConf := resource.StateChangeConf{
Pending: []string{eks.NodegroupStatusCreating},
Expand All @@ -299,7 +303,7 @@ func resourceAwsEksNodeGroupCreate(d *schema.ResourceData, meta interface{}) err
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("error waiting for EKS Node Group (%s) creation: %s", d.Id(), err)
return fmt.Errorf("error waiting for EKS Node Group (%s) creation: %w", d.Id(), err)
}

return resourceAwsEksNodeGroupRead(d, meta)
Expand All @@ -310,7 +314,8 @@ func resourceAwsEksNodeGroupRead(d *schema.ResourceData, meta interface{}) error
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

clusterName, nodeGroupName, err := resourceAwsEksNodeGroupParseId(d.Id())
clusterName, nodeGroupName, err := tfeks.NodeGroupParseResourceID(d.Id())

if err != nil {
return err
}
Expand All @@ -329,7 +334,7 @@ func resourceAwsEksNodeGroupRead(d *schema.ResourceData, meta interface{}) error
}

if err != nil {
return fmt.Errorf("error reading EKS Node Group (%s): %s", d.Id(), err)
return fmt.Errorf("error reading EKS Node Group (%s): %w", d.Id(), err)
}

nodeGroup := output.Nodegroup
Expand All @@ -346,37 +351,38 @@ func resourceAwsEksNodeGroupRead(d *schema.ResourceData, meta interface{}) error
d.Set("disk_size", nodeGroup.DiskSize)

if err := d.Set("instance_types", aws.StringValueSlice(nodeGroup.InstanceTypes)); err != nil {
return fmt.Errorf("error setting instance_types: %s", err)
return fmt.Errorf("error setting instance_types: %w", err)
}

if err := d.Set("labels", aws.StringValueMap(nodeGroup.Labels)); err != nil {
return fmt.Errorf("error setting labels: %s", err)
return fmt.Errorf("error setting labels: %w", err)
}

if err := d.Set("launch_template", flattenEksLaunchTemplateSpecification(nodeGroup.LaunchTemplate)); err != nil {
return fmt.Errorf("error setting launch_template: %s", err)
return fmt.Errorf("error setting launch_template: %w", err)
}

d.Set("node_group_name", nodeGroup.NodegroupName)
d.Set("node_group_name_prefix", naming.NamePrefixFromName(aws.StringValue(nodeGroup.NodegroupName)))
d.Set("node_role_arn", nodeGroup.NodeRole)
d.Set("release_version", nodeGroup.ReleaseVersion)

if err := d.Set("remote_access", flattenEksRemoteAccessConfig(nodeGroup.RemoteAccess)); err != nil {
return fmt.Errorf("error setting remote_access: %s", err)
return fmt.Errorf("error setting remote_access: %w", err)
}

if err := d.Set("resources", flattenEksNodeGroupResources(nodeGroup.Resources)); err != nil {
return fmt.Errorf("error setting resources: %s", err)
return fmt.Errorf("error setting resources: %w", err)
}

if err := d.Set("scaling_config", flattenEksNodeGroupScalingConfig(nodeGroup.ScalingConfig)); err != nil {
return fmt.Errorf("error setting scaling_config: %s", err)
return fmt.Errorf("error setting scaling_config: %w", err)
}

d.Set("status", nodeGroup.Status)

if err := d.Set("subnet_ids", aws.StringValueSlice(nodeGroup.Subnets)); err != nil {
return fmt.Errorf("error setting subnets: %s", err)
return fmt.Errorf("error setting subnets: %w", err)
}

tags := keyvaluetags.EksKeyValueTags(nodeGroup.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig)
Expand All @@ -398,7 +404,8 @@ func resourceAwsEksNodeGroupRead(d *schema.ResourceData, meta interface{}) error
func resourceAwsEksNodeGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).eksconn

clusterName, nodeGroupName, err := resourceAwsEksNodeGroupParseId(d.Id())
clusterName, nodeGroupName, err := tfeks.NodeGroupParseResourceID(d.Id())

if err != nil {
return err
}
Expand All @@ -420,7 +427,7 @@ func resourceAwsEksNodeGroupUpdate(d *schema.ResourceData, meta interface{}) err
output, err := conn.UpdateNodegroupConfig(input)

if err != nil {
return fmt.Errorf("error updating EKS Node Group (%s) config: %s", d.Id(), err)
return fmt.Errorf("error updating EKS Node Group (%s) config: %w", d.Id(), err)
}

if output == nil || output.Update == nil || output.Update.Id == nil {
Expand All @@ -431,7 +438,7 @@ func resourceAwsEksNodeGroupUpdate(d *schema.ResourceData, meta interface{}) err

err = waitForEksNodeGroupUpdate(conn, clusterName, nodeGroupName, updateID, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("error waiting for EKS Node Group (%s) config update (%s): %s", d.Id(), updateID, err)
return fmt.Errorf("error waiting for EKS Node Group (%s) config update (%s): %w", d.Id(), updateID, err)
}
}

Expand Down Expand Up @@ -474,7 +481,7 @@ func resourceAwsEksNodeGroupUpdate(d *schema.ResourceData, meta interface{}) err
output, err := conn.UpdateNodegroupVersion(input)

if err != nil {
return fmt.Errorf("error updating EKS Node Group (%s) version: %s", d.Id(), err)
return fmt.Errorf("error updating EKS Node Group (%s) version: %w", d.Id(), err)
}

if output == nil || output.Update == nil || output.Update.Id == nil {
Expand All @@ -485,14 +492,14 @@ func resourceAwsEksNodeGroupUpdate(d *schema.ResourceData, meta interface{}) err

err = waitForEksNodeGroupUpdate(conn, clusterName, nodeGroupName, updateID, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("error waiting for EKS Node Group (%s) version update (%s): %s", d.Id(), updateID, err)
return fmt.Errorf("error waiting for EKS Node Group (%s) version update (%s): %w", d.Id(), updateID, err)
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := keyvaluetags.EksUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
return fmt.Errorf("error updating tags: %w", err)
}
}

Expand All @@ -502,7 +509,8 @@ func resourceAwsEksNodeGroupUpdate(d *schema.ResourceData, meta interface{}) err
func resourceAwsEksNodeGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).eksconn

clusterName, nodeGroupName, err := resourceAwsEksNodeGroupParseId(d.Id())
clusterName, nodeGroupName, err := tfeks.NodeGroupParseResourceID(d.Id())

if err != nil {
return err
}
Expand All @@ -519,11 +527,11 @@ func resourceAwsEksNodeGroupDelete(d *schema.ResourceData, meta interface{}) err
}

if err != nil {
return fmt.Errorf("error deleting EKS Node Group (%s): %s", d.Id(), err)
return fmt.Errorf("error deleting EKS Node Group (%s): %w", d.Id(), err)
}

if err := waitForEksNodeGroupDeletion(conn, clusterName, nodeGroupName, d.Timeout(schema.TimeoutDelete)); err != nil {
return fmt.Errorf("error waiting for EKS Node Group (%s) deletion: %s", d.Id(), err)
return fmt.Errorf("error waiting for EKS Node Group (%s) deletion: %w", d.Id(), err)
}

return nil
Expand Down Expand Up @@ -718,7 +726,7 @@ func refreshEksNodeGroupStatus(conn *eks.EKS, clusterName string, nodeGroupName
nodeGroup := output.Nodegroup

if nodeGroup == nil {
return nodeGroup, "", fmt.Errorf("EKS Node Group (%s) missing", resourceAwsEksNodeGroupCreateId(clusterName, nodeGroupName))
return nodeGroup, "", fmt.Errorf("EKS Node Group (%s) missing", tfeks.NodeGroupCreateResourceID(clusterName, nodeGroupName))
}

status := aws.StringValue(nodeGroup.Status)
Expand All @@ -727,7 +735,7 @@ func refreshEksNodeGroupStatus(conn *eks.EKS, clusterName string, nodeGroupName
// unexpected state 'CREATE_FAILED', wanted target 'ACTIVE'. last error: %!s(<nil>)
if status == eks.NodegroupStatusCreateFailed || status == eks.NodegroupStatusDeleteFailed {
if nodeGroup.Health == nil || len(nodeGroup.Health.Issues) == 0 || nodeGroup.Health.Issues[0] == nil {
return nodeGroup, status, fmt.Errorf("unable to find additional information about %s status, check EKS Node Group (%s) health", status, resourceAwsEksNodeGroupCreateId(clusterName, nodeGroupName))
return nodeGroup, status, fmt.Errorf("unable to find additional information about %s status, check EKS Node Group (%s) health", status, tfeks.NodeGroupCreateResourceID(clusterName, nodeGroupName))
}

issue := nodeGroup.Health.Issues[0]
Expand All @@ -754,7 +762,7 @@ func refreshEksNodeGroupUpdateStatus(conn *eks.EKS, clusterName string, nodeGrou
}

if output == nil || output.Update == nil {
return nil, "", fmt.Errorf("EKS Node Group (%s) update (%s) missing", resourceAwsEksNodeGroupCreateId(clusterName, nodeGroupName), updateID)
return nil, "", fmt.Errorf("EKS Node Group (%s) update (%s) missing", tfeks.NodeGroupCreateResourceID(clusterName, nodeGroupName), updateID)
}

return output.Update, aws.StringValue(output.Update.Status), nil
Expand Down Expand Up @@ -812,19 +820,3 @@ func waitForEksNodeGroupUpdate(conn *eks.EKS, clusterName, nodeGroupName string,

return fmt.Errorf("EKS Node Group (%s) update (%s) status (%s) not successful: Errors:\n%s", clusterName, updateID, aws.StringValue(update.Status), strings.Join(detailedErrors, "\n"))
}

func resourceAwsEksNodeGroupParseId(id string) (string, string, error) {
// inverse of resourceAwsEksNodeGroupCreateId()
parts := strings.Split(id, ":")

if len(parts) != 2 {
return "", "", fmt.Errorf("unexpected format of ID (%s), expected cluster-name:node-group-name", id)
}

return parts[0], parts[1], nil
}

func resourceAwsEksNodeGroupCreateId(clusterName, nodeGroupName string) string {
// inverse of resourceAwsEksNodeGroupParseId()
return fmt.Sprintf("%s:%s", clusterName, nodeGroupName)
}
Loading