Skip to content

Commit

Permalink
feat: Udpate EKS to use aws-sdk-go-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
bryantbiggs committed Oct 25, 2023
1 parent f606f30 commit 83dbd0f
Show file tree
Hide file tree
Showing 38 changed files with 1,087 additions and 1,168 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/directoryservice v1.18.7
github.com/aws/aws-sdk-go-v2/service/docdbelastic v1.3.2
github.com/aws/aws-sdk-go-v2/service/ec2 v1.126.0
github.com/aws/aws-sdk-go-v2/service/eks v1.29.7
github.com/aws/aws-sdk-go-v2/service/emrserverless v1.11.2
github.com/aws/aws-sdk-go-v2/service/finspace v1.12.4
github.com/aws/aws-sdk-go-v2/service/fis v1.16.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ github.com/aws/aws-sdk-go-v2/service/dynamodb v1.22.1 h1:TYq4EU2vEEluoaBG0RCPnbi
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.22.1/go.mod h1:1EJb9/tJwI7iqiStZBcmHijQxcgp7dlPuD2YgoZIrJQ=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.126.0 h1:EGYP4IDYHYe4IcpCUxEAIVKr9nZXvtql4HNhEPK1Y3w=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.126.0/go.mod h1:raUdIDoNuDPn9dMG3cCmIm8RoWOmZUqQPzuw8xpmB8Y=
github.com/aws/aws-sdk-go-v2/service/eks v1.29.7 h1:MRBXts9pc/3RdaeLXR4HwmVhqMP70sOEYHpkgmAWDbU=
github.com/aws/aws-sdk-go-v2/service/eks v1.29.7/go.mod h1:Nt5l6Vn68Hv0JWJ6dcQDKuBAKAfHUZSC9Ln8X/1fUMY=
github.com/aws/aws-sdk-go-v2/service/emrserverless v1.11.2 h1:diyMrawOZ56CavFS//UFFjk2LY1ooXeTqAecXsjtXwI=
github.com/aws/aws-sdk-go-v2/service/emrserverless v1.11.2/go.mod h1:ZrmnnT6zI3+0XsQIGCu/vXhIFk4Vwu4WKqeMDSzm4z4=
github.com/aws/aws-sdk-go-v2/service/finspace v1.12.4 h1:xML1DGju5bsRtFejZfHuWtaut3WkYrGTHMf2G3T3de0=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/service/dms/event_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go/aws"
dms "github.com/aws/aws-sdk-go/service/databasemigrationservice"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -389,11 +389,11 @@ resource "aws_dms_event_subscription" "test" {
}

func testAccPreCheckEKS(ctx context.Context, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).EKSConn(ctx)
client := acctest.Provider.Meta().(*conns.AWSClient).EKSClient(ctx)

input := &eks.ListClustersInput{}

_, err := conn.ListClustersWithContext(ctx, input)
_, err := client.ListClusters(ctx, input)

if acctest.PreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
Expand Down
120 changes: 67 additions & 53 deletions internal/service/eks/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ package eks
import (
"context"
"log"
"strings"
"time"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
sdkid "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -89,26 +92,23 @@ func ResourceAddon() *schema.Resource {
Optional: true,
},
"resolve_conflicts": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(eks.ResolveConflicts_Values(), false),
Deprecated: `The "resolve_conflicts" attribute can't be set to "PRESERVE" on initial resource creation. Use "resolve_conflicts_on_create" and/or "resolve_conflicts_on_update" instead`,
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: enum.Validate[types.ResolveConflicts](),
Deprecated: `The "resolve_conflicts" attribute can't be set to "PRESERVE" on initial resource creation. Use "resolve_conflicts_on_create" and/or "resolve_conflicts_on_update" instead`,
},
"resolve_conflicts_on_create": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
eks.ResolveConflictsNone,
eks.ResolveConflictsOverwrite,
}, false),
ConflictsWith: []string{"resolve_conflicts"},
},
"resolve_conflicts_on_update": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(eks.ResolveConflicts_Values(), false),
ValidateFunc: validation.StringInSlice(enum.Slice(types.ResolveConflictsNone, types.ResolveConflictsOverwrite), false),
ConflictsWith: []string{"resolve_conflicts"},
},
"resolve_conflicts_on_update": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: enum.Validate[types.ResolveConflicts](),
ConflictsWith: []string{"resolve_conflicts"},
},
"service_account_role_arn": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -122,7 +122,7 @@ func ResourceAddon() *schema.Resource {

func resourceAddonCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSConn(ctx)
client := meta.(*conns.AWSClient).EKSClient(ctx)

addonName := d.Get("addon_name").(string)
clusterName := d.Get("cluster_name").(string)
Expand All @@ -143,9 +143,9 @@ func resourceAddonCreate(ctx context.Context, d *schema.ResourceData, meta inter
}

if v, ok := d.GetOk("resolve_conflicts"); ok {
input.ResolveConflicts = aws.String(v.(string))
input.ResolveConflicts = types.ResolveConflicts(v.(string))
} else if v, ok := d.GetOk("resolve_conflicts_on_create"); ok {
input.ResolveConflicts = aws.String(v.(string))
input.ResolveConflicts = types.ResolveConflicts(v.(string))
}

if v, ok := d.GetOk("service_account_role_arn"); ok {
Expand All @@ -154,15 +154,13 @@ func resourceAddonCreate(ctx context.Context, d *schema.ResourceData, meta inter

_, err := tfresource.RetryWhen(ctx, propagationTimeout,
func() (interface{}, error) {
return conn.CreateAddonWithContext(ctx, input)
return client.CreateAddon(ctx, input)
},
func(err error) (bool, error) {
if tfawserr.ErrMessageContains(err, eks.ErrCodeInvalidParameterException, "CREATE_FAILED") {
return true, err
}

if tfawserr.ErrMessageContains(err, eks.ErrCodeInvalidParameterException, "does not exist") {
return true, err
if errs.IsA[*types.InvalidParameterException](err) {
if strings.Contains(err.Error(), "CREATE_FAILED") || strings.Contains(err.Error(), "does not exist") {
return true, err
}
}

return false, err
Expand All @@ -175,7 +173,14 @@ func resourceAddonCreate(ctx context.Context, d *schema.ResourceData, meta inter

d.SetId(id)

if _, err := waitAddonCreated(ctx, conn, clusterName, addonName, d.Timeout(schema.TimeoutCreate)); err != nil {
waiter := eks.NewAddonActiveWaiter(client)
waiterParams := &eks.DescribeAddonInput{
AddonName: aws.String(addonName),
ClusterName: aws.String(clusterName),
}

err = waiter.Wait(ctx, waiterParams, d.Timeout(schema.TimeoutCreate))
if err != nil {
// Creating addon w/o setting resolve_conflicts to "OVERWRITE"
// might result in a failed creation, if unmanaged version of addon is already deployed
// and there are configuration conflicts:
Expand All @@ -194,20 +199,22 @@ func resourceAddonCreate(ctx context.Context, d *schema.ResourceData, meta inter

func resourceAddonRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSConn(ctx)
client := meta.(*conns.AWSClient).EKSClient(ctx)

clusterName, addonName, err := AddonParseResourceID(d.Id())

if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}

addon, err := FindAddonByClusterNameAndAddonName(ctx, conn, clusterName, addonName)
addon, err := FindAddonByClusterNameAndAddonName(ctx, client, clusterName, addonName)

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EKS Add-On (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
if !d.IsNewResource() && errs.IsA[*types.ResourceNotFoundException](err) {
if !d.IsNewResource() {
log.Printf("[WARN] EKS Add-On (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
}

if err != nil {
Expand All @@ -219,8 +226,8 @@ func resourceAddonRead(ctx context.Context, d *schema.ResourceData, meta interfa
d.Set("arn", addon.AddonArn)
d.Set("cluster_name", addon.ClusterName)
d.Set("configuration_values", addon.ConfigurationValues)
d.Set("created_at", aws.TimeValue(addon.CreatedAt).Format(time.RFC3339))
d.Set("modified_at", aws.TimeValue(addon.ModifiedAt).Format(time.RFC3339))
d.Set("created_at", aws.ToTime(addon.CreatedAt).Format(time.RFC3339))
d.Set("modified_at", aws.ToTime(addon.ModifiedAt).Format(time.RFC3339))
d.Set("service_account_role_arn", addon.ServiceAccountRoleArn)

setTagsOut(ctx, addon.Tags)
Expand All @@ -230,7 +237,7 @@ func resourceAddonRead(ctx context.Context, d *schema.ResourceData, meta interfa

func resourceAddonUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSConn(ctx)
client := meta.(*conns.AWSClient).EKSClient(ctx)

clusterName, addonName, err := AddonParseResourceID(d.Id())

Expand All @@ -253,16 +260,17 @@ func resourceAddonUpdate(ctx context.Context, d *schema.ResourceData, meta inter
input.ConfigurationValues = aws.String(d.Get("configuration_values").(string))
}

var conflictResolutionAttr, conflictResolution string
var conflictResolutionAttr string
var conflictResolution types.ResolveConflicts

if v, ok := d.GetOk("resolve_conflicts"); ok {
conflictResolutionAttr = "resolve_conflicts"
conflictResolution = v.(string)
input.ResolveConflicts = aws.String(v.(string))
conflictResolution = types.ResolveConflicts(v.(string))
input.ResolveConflicts = conflictResolution
} else if v, ok := d.GetOk("resolve_conflicts_on_update"); ok {
conflictResolutionAttr = "resolve_conflicts_on_update"
conflictResolution = v.(string)
input.ResolveConflicts = aws.String(v.(string))
conflictResolution = types.ResolveConflicts(v.(string))
input.ResolveConflicts = conflictResolution
}

// If service account role ARN is already provided, use it. Otherwise, the add-on uses
Expand All @@ -271,19 +279,19 @@ func resourceAddonUpdate(ctx context.Context, d *schema.ResourceData, meta inter
input.ServiceAccountRoleArn = aws.String(d.Get("service_account_role_arn").(string))
}

output, err := conn.UpdateAddonWithContext(ctx, input)
output, err := client.UpdateAddon(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating EKS Add-On (%s): %s", d.Id(), err)
}

updateID := aws.StringValue(output.Update.Id)
if _, err := waitAddonUpdateSuccessful(ctx, conn, clusterName, addonName, updateID, d.Timeout(schema.TimeoutUpdate)); err != nil {
if conflictResolution != eks.ResolveConflictsOverwrite {
updateID := aws.ToString(output.Update.Id)
if _, err := waitAddonUpdateSuccessful(ctx, client, clusterName, addonName, updateID, d.Timeout(schema.TimeoutUpdate)); err != nil {
if conflictResolution != types.ResolveConflictsOverwrite {
// Changing addon version w/o setting resolve_conflicts to "OVERWRITE"
// might result in a failed update if there are conflicts:
// ConfigurationConflict Apply failed with 1 conflict: conflict with "kubectl"...
return sdkdiag.AppendErrorf(diags, "waiting for EKS Add-On (%s) update (%s): %s. Consider setting attribute %q to %q", d.Id(), updateID, err, conflictResolutionAttr, eks.ResolveConflictsOverwrite)
return sdkdiag.AppendErrorf(diags, "waiting for EKS Add-On (%s) update (%s): %s. Consider setting attribute %q to %q", d.Id(), updateID, err, conflictResolutionAttr, string(types.ResolveConflictsOverwrite))
}

return sdkdiag.AppendErrorf(diags, "waiting for EKS Add-On (%s) update (%s): %s", d.Id(), updateID, err)
Expand All @@ -295,7 +303,7 @@ func resourceAddonUpdate(ctx context.Context, d *schema.ResourceData, meta inter

func resourceAddonDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSConn(ctx)
client := meta.(*conns.AWSClient).EKSClient(ctx)

clusterName, addonName, err := AddonParseResourceID(d.Id())

Expand All @@ -307,20 +315,26 @@ func resourceAddonDelete(ctx context.Context, d *schema.ResourceData, meta inter
AddonName: aws.String(addonName),
ClusterName: aws.String(clusterName),
}

if v, ok := d.GetOk("preserve"); ok {
input.Preserve = aws.Bool(v.(bool))
input.Preserve = v.(bool)
}

log.Printf("[DEBUG] Deleting EKS Add-On: %s", d.Id())
_, err = conn.DeleteAddonWithContext(ctx, input)
_, err = client.DeleteAddon(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting EKS Add-On (%s): %s", d.Id(), err)
}

if _, err := waitAddonDeleted(ctx, conn, clusterName, addonName, d.Timeout(schema.TimeoutDelete)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EKS Add-On (%s) delete: %s", d.Id(), err)
waiter := eks.NewAddonDeletedWaiter(client)
waiterParams := &eks.DescribeAddonInput{
AddonName: aws.String(addonName),
ClusterName: aws.String(clusterName),
}

err = waiter.Wait(ctx, waiterParams, d.Timeout(schema.TimeoutDelete))
if err != nil {
sdkdiag.AppendErrorf(diags, "waiting for EKS Add-On (%s) delete: %s", d.Id(), err)
}

return diags
Expand Down
10 changes: 5 additions & 5 deletions internal/service/eks/addon_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -60,14 +60,14 @@ func DataSourceAddon() *schema.Resource {
}

func dataSourceAddonRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).EKSConn(ctx)
client := meta.(*conns.AWSClient).EKSClient(ctx)
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

addonName := d.Get("addon_name").(string)
clusterName := d.Get("cluster_name").(string)
id := AddonCreateResourceID(clusterName, addonName)

addon, err := FindAddonByClusterNameAndAddonName(ctx, conn, clusterName, addonName)
addon, err := FindAddonByClusterNameAndAddonName(ctx, client, clusterName, addonName)

if err != nil {
return diag.Errorf("reading EKS Add-On (%s): %s", id, err)
Expand All @@ -77,8 +77,8 @@ func dataSourceAddonRead(ctx context.Context, d *schema.ResourceData, meta inter
d.Set("addon_version", addon.AddonVersion)
d.Set("arn", addon.AddonArn)
d.Set("configuration_values", addon.ConfigurationValues)
d.Set("created_at", aws.TimeValue(addon.CreatedAt).Format(time.RFC3339))
d.Set("modified_at", aws.TimeValue(addon.ModifiedAt).Format(time.RFC3339))
d.Set("created_at", aws.ToTime(addon.CreatedAt).Format(time.RFC3339))
d.Set("modified_at", aws.ToTime(addon.ModifiedAt).Format(time.RFC3339))
d.Set("service_account_role_arn", addon.ServiceAccountRoleArn)

if err := d.Set("tags", KeyValueTags(ctx, addon.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
Expand Down
Loading

0 comments on commit 83dbd0f

Please sign in to comment.