Skip to content

Commit

Permalink
Merge pull request #5086 from cPu1/migrate-ec2-sdk-v2
Browse files Browse the repository at this point in the history
Migrate usage of EC2 to aws-sdk-go-v2
  • Loading branch information
cPu1 authored Apr 12, 2022
2 parents 2610b1a + 00153f2 commit 506e8da
Show file tree
Hide file tree
Showing 83 changed files with 20,646 additions and 46,976 deletions.
2 changes: 1 addition & 1 deletion integration/tests/crud/creategetdelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ var _ = Describe("(Integration) Create, Get, Scale & Delete", func() {
}
ctl, err := eks.New(context.TODO(), &api.ProviderConfig{Region: params.Region}, cfg)
Expect(err).NotTo(HaveOccurred())
cl, err := ctl.GetCluster(params.ClusterName)
cl, err := ctl.GetCluster(context.Background(), params.ClusterName)
Expect(err).NotTo(HaveOccurred())
awsSession := NewSession(params.Region)
ec2 := awsec2.New(awsSession)
Expand Down
2 changes: 1 addition & 1 deletion pkg/actions/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type Cluster interface {
Upgrade(dryRun bool) error
Upgrade(ctx context.Context, dryRun bool) error
Delete(ctx context.Context, waitInterval time.Duration, wait, force, disableNodegroupEviction bool, parallel int) error
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/actions/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type NodeGroupDrainer interface {
}
type vpcCniDeleter func(clusterName string, ctl *eks.ClusterProvider, clientSet kubernetes.Interface)

func deleteSharedResources(cfg *api.ClusterConfig, ctl *eks.ClusterProvider, stackManager manager.StackManager, clusterOperable bool, clientSet kubernetes.Interface) error {
func deleteSharedResources(ctx context.Context, cfg *api.ClusterConfig, ctl *eks.ClusterProvider, stackManager manager.StackManager, clusterOperable bool, clientSet kubernetes.Interface) error {
if clusterOperable {
if err := deleteFargateProfiles(cfg.Metadata, ctl, stackManager); err != nil {
return err
Expand All @@ -49,7 +49,7 @@ func deleteSharedResources(cfg *api.ClusterConfig, ctl *eks.ClusterProvider, sta
return nil
}

ssh.DeleteKeys(cfg.Metadata.Name, ctl.Provider.EC2())
ssh.DeleteKeys(ctx, ctl.Provider.EC2(), cfg.Metadata.Name)

kubeconfig.MaybeDeleteConfig(cfg.Metadata)

Expand Down
5 changes: 3 additions & 2 deletions pkg/actions/cluster/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/ec2"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
awseks "github.com/aws/aws-sdk-go/service/eks"
"github.com/kris-nova/logger"

Expand Down Expand Up @@ -44,7 +45,7 @@ func GetClusters(ctx context.Context, provider api.ClusterProvider, listAllRegio
}

var clusters []Description
authorizedRegionsList, err := provider.EC2().DescribeRegions(&ec2.DescribeRegionsInput{})
authorizedRegionsList, err := provider.EC2().DescribeRegions(ctx, &ec2.DescribeRegionsInput{})
if err != nil {
return nil, fmt.Errorf("failed to describe regions: %w", err)
}
Expand Down
27 changes: 15 additions & 12 deletions pkg/actions/cluster/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"

"github.com/aws/aws-sdk-go/aws"
awsec2 "github.com/aws/aws-sdk-go/service/ec2"
awseks "github.com/aws/aws-sdk-go/service/eks"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"

"github.com/weaveworks/eksctl/pkg/actions/cluster"
"github.com/weaveworks/eksctl/pkg/actions/cluster/fakes"
Expand Down Expand Up @@ -56,7 +59,7 @@ var _ = Describe("Get", func() {
stackManager.HasClusterStackFromListReturnsOnCall(2, false, fmt.Errorf("foo"))
})
It("returns the clusters in that region", func() {
clusters, err := cluster.GetClusters(context.TODO(), intialProvider, false, 100)
clusters, err := cluster.GetClusters(context.Background(), intialProvider, false, 100)
Expect(err).NotTo(HaveOccurred())
Expect(clusters).To(ConsistOf(
cluster.Description{
Expand Down Expand Up @@ -97,7 +100,7 @@ var _ = Describe("Get", func() {
})

It("errors", func() {
_, err := cluster.GetClusters(context.TODO(), intialProvider, false, 100)
_, err := cluster.GetClusters(context.Background(), intialProvider, false, 100)
Expect(err).To(MatchError(`failed to list cluster stacks in region "us-west-2": foo`))
})
})
Expand All @@ -111,7 +114,7 @@ var _ = Describe("Get", func() {
})

It("errors", func() {
_, err := cluster.GetClusters(context.TODO(), intialProvider, false, 100)
_, err := cluster.GetClusters(context.Background(), intialProvider, false, 100)
Expect(err).To(MatchError(`failed to list clusters in region "us-west-2": foo`))
})
})
Expand Down Expand Up @@ -141,8 +144,8 @@ var _ = Describe("Get", func() {
BeforeEach(func() {
awsProvider.ReturnsOnCall(0, &eks.ClusterProvider{Provider: providerRegion1}, nil)
awsProvider.ReturnsOnCall(1, &eks.ClusterProvider{Provider: providerRegion2}, nil)
intialProvider.MockEC2().On("DescribeRegions", &awsec2.DescribeRegionsInput{}).Return(&awsec2.DescribeRegionsOutput{
Regions: []*awsec2.Region{
intialProvider.MockEC2().On("DescribeRegions", mock.Anything, &ec2.DescribeRegionsInput{}).Return(&ec2.DescribeRegionsOutput{
Regions: []ec2types.Region{
{
RegionName: aws.String("us-west-1"),
},
Expand Down Expand Up @@ -173,7 +176,7 @@ var _ = Describe("Get", func() {
})

It("returns the clusters across all authorised regions", func() {
clusters, err := cluster.GetClusters(context.TODO(), intialProvider, true, 100)
clusters, err := cluster.GetClusters(context.Background(), intialProvider, true, 100)
Expect(err).NotTo(HaveOccurred())
Expect(clusters).To(ConsistOf(
cluster.Description{
Expand Down Expand Up @@ -212,11 +215,11 @@ var _ = Describe("Get", func() {

When("DescribeRegion errors", func() {
BeforeEach(func() {
intialProvider.MockEC2().On("DescribeRegions", &awsec2.DescribeRegionsInput{}).Return(nil, fmt.Errorf("foo"))
intialProvider.MockEC2().On("DescribeRegions", mock.Anything, &ec2.DescribeRegionsInput{}).Return(nil, fmt.Errorf("foo"))
})

It("errors", func() {
_, err := cluster.GetClusters(context.TODO(), intialProvider, true, 100)
_, err := cluster.GetClusters(context.Background(), intialProvider, true, 100)
Expect(err).To(MatchError(`failed to describe regions: foo`))
})
})
Expand All @@ -225,8 +228,8 @@ var _ = Describe("Get", func() {
BeforeEach(func() {
awsProvider.ReturnsOnCall(0, &eks.ClusterProvider{Provider: providerRegion1}, nil)
awsProvider.ReturnsOnCall(1, nil, fmt.Errorf("foo"))
intialProvider.MockEC2().On("DescribeRegions", &awsec2.DescribeRegionsInput{}).Return(&awsec2.DescribeRegionsOutput{
Regions: []*awsec2.Region{
intialProvider.MockEC2().On("DescribeRegions", mock.Anything, &ec2.DescribeRegionsInput{}).Return(&ec2.DescribeRegionsOutput{
Regions: []ec2types.Region{
{
RegionName: aws.String("us-west-1"),
},
Expand All @@ -248,7 +251,7 @@ var _ = Describe("Get", func() {
})

It("returns the clusters in the regions it was successful in", func() {
clusters, err := cluster.GetClusters(context.TODO(), intialProvider, true, 100)
clusters, err := cluster.GetClusters(context.Background(), intialProvider, true, 100)
Expect(err).NotTo(HaveOccurred())
Expect(clusters).To(ConsistOf(
cluster.Description{
Expand Down
12 changes: 6 additions & 6 deletions pkg/actions/cluster/owned.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func NewOwnedCluster(cfg *api.ClusterConfig, ctl *eks.ClusterProvider, clusterSt
}
}

func (c *OwnedCluster) Upgrade(dryRun bool) error {
if err := vpc.UseFromClusterStack(c.ctl.Provider, c.clusterStack, c.cfg); err != nil {
func (c *OwnedCluster) Upgrade(ctx context.Context, dryRun bool) error {
if err := vpc.UseFromClusterStack(ctx, c.ctl.Provider, c.clusterStack, c.cfg); err != nil {
return errors.Wrapf(err, "getting VPC configuration for cluster %q", c.cfg.Metadata.Name)
}

Expand All @@ -51,7 +51,7 @@ func (c *OwnedCluster) Upgrade(dryRun bool) error {
return err
}

stackUpdateRequired, err := c.stackManager.AppendNewClusterStackResource(dryRun)
stackUpdateRequired, err := c.stackManager.AppendNewClusterStackResource(ctx, dryRun)
if err != nil {
return err
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (c *OwnedCluster) Delete(ctx context.Context, _ time.Duration, wait, force,
}
}

if err := deleteSharedResources(c.cfg, c.ctl, c.stackManager, clusterOperable, clientSet); err != nil {
if err := deleteSharedResources(ctx, c.cfg, c.ctl, c.stackManager, clusterOperable, clientSet); err != nil {
if err != nil {
if force {
logger.Warning("error occurred during deletion: %v", err)
Expand All @@ -129,12 +129,12 @@ func (c *OwnedCluster) Delete(ctx context.Context, _ time.Duration, wait, force,
deleteOIDCProvider := clusterOperable && oidcSupported
tasks, err := c.stackManager.NewTasksToDeleteClusterWithNodeGroups(ctx, c.clusterStack, allStacks, deleteOIDCProvider, oidc, kubernetes.NewCachedClientSet(clientSet), wait, func(errs chan error, _ string) error {
logger.Info("trying to cleanup dangling network interfaces")
if err := c.ctl.LoadClusterVPC(c.cfg, c.stackManager); err != nil {
if err := c.ctl.LoadClusterVPC(ctx, c.cfg, c.stackManager); err != nil {
return errors.Wrapf(err, "getting VPC configuration for cluster %q", c.cfg.Metadata.Name)
}

go func() {
errs <- vpc.CleanupNetworkInterfaces(c.ctl.Provider.EC2(), c.cfg)
errs <- vpc.CleanupNetworkInterfaces(ctx, c.ctl.Provider.EC2(), c.cfg)
close(errs)
}()
return nil
Expand Down
19 changes: 10 additions & 9 deletions pkg/actions/cluster/owned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"time"

"github.com/aws/aws-sdk-go-v2/service/ec2"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
awseks "github.com/aws/aws-sdk-go/service/eks"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -89,9 +90,9 @@ var _ = Describe("Delete", func() {
}}},
}, nil)

p.MockEC2().On("DescribeKeyPairs", mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)
p.MockEC2().On("DescribeKeyPairs", mock.Anything, mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)

p.MockEC2().On("DescribeSecurityGroupsWithContext", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)
p.MockEC2().On("DescribeSecurityGroups", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)

fakeStackManager.NewTasksToDeleteClusterWithNodeGroupsReturns(&tasks.TaskTree{
Tasks: []tasks.Task{&tasks.GenericTask{Doer: func() error {
Expand Down Expand Up @@ -160,9 +161,9 @@ var _ = Describe("Delete", func() {

fakeStackManager.ListNodeGroupStacksReturns([]manager.NodeGroupStack{{NodeGroupName: "ng-1"}}, nil)

p.MockEC2().On("DescribeKeyPairs", mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)
p.MockEC2().On("DescribeKeyPairs", mock.Anything, mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)

p.MockEC2().On("DescribeSecurityGroupsWithContext", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)
p.MockEC2().On("DescribeSecurityGroups", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)

fakeStackManager.NewTasksToDeleteClusterWithNodeGroupsReturns(&tasks.TaskTree{
Tasks: []tasks.Task{},
Expand Down Expand Up @@ -225,9 +226,9 @@ var _ = Describe("Delete", func() {
}, nil)
fakeStackManager.ListNodeGroupStacksReturns([]manager.NodeGroupStack{{NodeGroupName: "ng-1"}}, nil)

p.MockEC2().On("DescribeKeyPairs", mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)
p.MockEC2().On("DescribeKeyPairs", mock.Anything, mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)

p.MockEC2().On("DescribeSecurityGroupsWithContext", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)
p.MockEC2().On("DescribeSecurityGroups", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)

fakeStackManager.NewTasksToDeleteClusterWithNodeGroupsReturns(&tasks.TaskTree{
Tasks: []tasks.Task{},
Expand Down Expand Up @@ -288,9 +289,9 @@ var _ = Describe("Delete", func() {
}}},
}, nil)

p.MockEC2().On("DescribeKeyPairs", mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)
p.MockEC2().On("DescribeKeyPairs", mock.Anything, mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)

p.MockEC2().On("DescribeSecurityGroupsWithContext", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)
p.MockEC2().On("DescribeSecurityGroups", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)

fakeStackManager.NewTasksToDeleteClusterWithNodeGroupsReturns(&tasks.TaskTree{
Tasks: []tasks.Task{&tasks.GenericTask{Doer: func() error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/actions/cluster/unowned.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewUnownedCluster(cfg *api.ClusterConfig, ctl *eks.ClusterProvider, stackMa
}
}

func (c *UnownedCluster) Upgrade(dryRun bool) error {
func (c *UnownedCluster) Upgrade(_ context.Context, dryRun bool) error {
versionUpdateRequired, err := upgrade(c.cfg, c.ctl, dryRun)
if err != nil {
return err
Expand Down Expand Up @@ -89,7 +89,7 @@ func (c *UnownedCluster) Delete(ctx context.Context, waitInterval time.Duration,
}
}

if err := deleteSharedResources(c.cfg, c.ctl, c.stackManager, clusterOperable, clientSet); err != nil {
if err := deleteSharedResources(ctx, c.cfg, c.ctl, c.stackManager, clusterOperable, clientSet); err != nil {
if err != nil {
if force {
logger.Warning("error occurred during deletion: %v", err)
Expand Down
19 changes: 10 additions & 9 deletions pkg/actions/cluster/unowned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"time"

"github.com/aws/aws-sdk-go-v2/service/ec2"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ec2"
awseks "github.com/aws/aws-sdk-go/service/eks"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -111,9 +112,9 @@ var _ = Describe("Delete", func() {
}}},
}, nil)

p.MockEC2().On("DescribeKeyPairs", mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)
p.MockEC2().On("DescribeKeyPairs", mock.Anything, mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)

p.MockEC2().On("DescribeSecurityGroupsWithContext", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)
p.MockEC2().On("DescribeSecurityGroups", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)

fakeStackManager.GetFargateStackReturns(&cloudformation.Stack{StackName: aws.String("fargate-role")}, nil)
fakeStackManager.DeleteStackBySpecReturns(nil, nil)
Expand Down Expand Up @@ -199,9 +200,9 @@ var _ = Describe("Delete", func() {
Tasks: []tasks.Task{},
}, nil)

p.MockEC2().On("DescribeKeyPairs", mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)
p.MockEC2().On("DescribeKeyPairs", mock.Anything, mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)

p.MockEC2().On("DescribeSecurityGroupsWithContext", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)
p.MockEC2().On("DescribeSecurityGroups", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)

fakeStackManager.GetFargateStackReturns(nil, nil)
fakeStackManager.DeleteStackBySpecReturns(nil, nil)
Expand Down Expand Up @@ -302,9 +303,9 @@ var _ = Describe("Delete", func() {
Tasks: []tasks.Task{},
}, nil)

p.MockEC2().On("DescribeKeyPairs", mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)
p.MockEC2().On("DescribeKeyPairs", mock.Anything, mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)

p.MockEC2().On("DescribeSecurityGroupsWithContext", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)
p.MockEC2().On("DescribeSecurityGroups", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)

fakeStackManager.GetFargateStackReturns(nil, nil)
fakeStackManager.DeleteStackBySpecReturns(nil, nil)
Expand Down Expand Up @@ -385,9 +386,9 @@ var _ = Describe("Delete", func() {
}}},
}, nil)

p.MockEC2().On("DescribeKeyPairs", mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)
p.MockEC2().On("DescribeKeyPairs", mock.Anything, mock.Anything).Return(&ec2.DescribeKeyPairsOutput{}, nil)

p.MockEC2().On("DescribeSecurityGroupsWithContext", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)
p.MockEC2().On("DescribeSecurityGroups", mock.Anything, mock.Anything).Return(&ec2.DescribeSecurityGroupsOutput{}, nil)

p.MockEKS().On("ListNodegroups", mock.Anything).Return(&awseks.ListNodegroupsOutput{
Nodegroups: aws.StringSlice([]string{"ng-1", "ng-2"}),
Expand Down
7 changes: 5 additions & 2 deletions pkg/actions/fargate/create.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package fargate

import (
"context"

"github.com/weaveworks/eksctl/pkg/cfn/manager"

"github.com/pkg/errors"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/outputs"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/fargate"
)

func (m *Manager) Create() error {
func (m *Manager) Create(ctx context.Context) error {
ctl := m.ctl
cfg := m.cfg
if ok, err := ctl.CanOperate(cfg); !ok {
Expand Down Expand Up @@ -39,7 +42,7 @@ func (m *Manager) Create() error {
return errors.Wrap(err, "couldn't ensure fargate role exists")
}
}
if err := ctl.LoadClusterIntoSpecFromStack(cfg, m.stackManager); err != nil {
if err := ctl.LoadClusterIntoSpecFromStack(ctx, cfg, m.stackManager); err != nil {
return errors.Wrap(err, "couldn't load cluster into spec")
}
} else {
Expand Down
Loading

0 comments on commit 506e8da

Please sign in to comment.