Skip to content

Commit 483f3a9

Browse files
authored
Merge pull request #5521 from yiannistri/ec2-sdk-v2-migration
✨ Migrate ec2 to AWS SDK V2
2 parents 8d4c7f2 + c04842a commit 483f3a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+5830
-41151
lines changed

api/v1beta2/network_types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"sort"
2222
"time"
2323

24+
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
2425
"github.com/aws/aws-sdk-go/aws"
25-
"github.com/aws/aws-sdk-go/service/ec2"
2626
"k8s.io/utils/ptr"
2727
)
2828

@@ -667,11 +667,11 @@ func (s *SubnetSpec) IsEdgeWavelength() bool {
667667
}
668668

669669
// SetZoneInfo updates the subnets with zone information.
670-
func (s *SubnetSpec) SetZoneInfo(zones []*ec2.AvailabilityZone) error {
671-
zoneInfo := func(zoneName string) *ec2.AvailabilityZone {
670+
func (s *SubnetSpec) SetZoneInfo(zones []types.AvailabilityZone) error {
671+
zoneInfo := func(zoneName string) *types.AvailabilityZone {
672672
for _, zone := range zones {
673673
if aws.StringValue(zone.ZoneName) == zoneName {
674-
return zone
674+
return &zone
675675
}
676676
}
677677
return nil
@@ -826,7 +826,7 @@ func (s Subnets) GetUniqueZones() []string {
826826
}
827827

828828
// SetZoneInfo updates the subnets with zone information.
829-
func (s Subnets) SetZoneInfo(zones []*ec2.AvailabilityZone) error {
829+
func (s Subnets) SetZoneInfo(zones []types.AvailabilityZone) error {
830830
for i := range s {
831831
if err := s[i].SetZoneInfo(zones); err != nil {
832832
return err

api/v1beta2/network_types_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package v1beta2
1919
import (
2020
"testing"
2121

22-
"github.com/aws/aws-sdk-go/service/ec2"
22+
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
2323
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
"k8s.io/utils/ptr"
@@ -338,7 +338,7 @@ func TestSubnetSpec_SetZoneInfo(t *testing.T) {
338338
tests := []struct {
339339
name string
340340
spec *SubnetSpec
341-
zones []*ec2.AvailabilityZone
341+
zones []types.AvailabilityZone
342342
want *SubnetSpec
343343
wantErr string
344344
}{
@@ -350,7 +350,7 @@ func TestSubnetSpec_SetZoneInfo(t *testing.T) {
350350
s.ParentZoneName = nil
351351
return s
352352
}(),
353-
zones: []*ec2.AvailabilityZone{
353+
zones: []types.AvailabilityZone{
354354
{
355355
ZoneName: ptr.To[string]("us-east-1a"),
356356
ZoneType: ptr.To[string]("availability-zone"),
@@ -366,7 +366,7 @@ func TestSubnetSpec_SetZoneInfo(t *testing.T) {
366366
s.ParentZoneName = nil
367367
return s
368368
}(),
369-
zones: []*ec2.AvailabilityZone{
369+
zones: []types.AvailabilityZone{
370370
{
371371
ZoneName: ptr.To[string]("us-east-1b"),
372372
ZoneType: ptr.To[string]("availability-zone"),
@@ -386,7 +386,7 @@ func TestSubnetSpec_SetZoneInfo(t *testing.T) {
386386
s.ParentZoneName = nil
387387
return s
388388
}(),
389-
zones: []*ec2.AvailabilityZone{},
389+
zones: []types.AvailabilityZone{},
390390
wantErr: `unable to update zone information for subnet 'subnet-id-us-east-1a-private' and zone 'us-east-1a'`,
391391
},
392392
{
@@ -396,7 +396,7 @@ func TestSubnetSpec_SetZoneInfo(t *testing.T) {
396396
s.AvailabilityZone = ""
397397
return s
398398
}(),
399-
zones: []*ec2.AvailabilityZone{
399+
zones: []types.AvailabilityZone{
400400
{
401401
ZoneName: ptr.To[string]("us-east-1a"),
402402
ZoneType: ptr.To[string]("availability-zone"),
@@ -412,7 +412,7 @@ func TestSubnetSpec_SetZoneInfo(t *testing.T) {
412412
s.ParentZoneName = nil
413413
return s
414414
}(),
415-
zones: []*ec2.AvailabilityZone{
415+
zones: []types.AvailabilityZone{
416416
{
417417
ZoneName: ptr.To[string]("us-east-1b"),
418418
ZoneType: ptr.To[string]("availability-zone"),
@@ -436,7 +436,7 @@ func TestSubnetSpec_SetZoneInfo(t *testing.T) {
436436
s.ParentZoneName = nil
437437
return s
438438
}(),
439-
zones: []*ec2.AvailabilityZone{
439+
zones: []types.AvailabilityZone{
440440
{
441441
ZoneName: ptr.To[string]("us-east-1b"),
442442
ZoneType: ptr.To[string]("availability-zone"),

cmd/clusterawsadm/ami/copy.go

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ limitations under the License.
1717
package ami
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"strconv"
2223
"time"
2324

24-
"github.com/aws/aws-sdk-go/aws"
25-
"github.com/aws/aws-sdk-go/aws/session"
26-
"github.com/aws/aws-sdk-go/service/ec2"
25+
"github.com/aws/aws-sdk-go-v2/aws"
26+
"github.com/aws/aws-sdk-go-v2/config"
27+
"github.com/aws/aws-sdk-go-v2/service/ec2"
28+
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
2729
"github.com/go-logr/logr"
2830
"github.com/pkg/errors"
2931
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -48,14 +50,11 @@ type CopyInput struct {
4850

4951
// Copy will create an AWSAMI from a CopyInput.
5052
func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
51-
sourceSession, err := session.NewSessionWithOptions(session.Options{
52-
SharedConfigState: session.SharedConfigEnable,
53-
Config: aws.Config{Region: aws.String(input.SourceRegion)},
54-
})
53+
sourceCfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(input.SourceRegion))
5554
if err != nil {
5655
return nil, err
5756
}
58-
ec2Client := ec2.New(sourceSession)
57+
ec2Client := ec2.NewFromConfig(sourceCfg)
5958

6059
image, err := ec2service.DefaultAMILookup(ec2Client, input.OwnerID, input.OperatingSystem, input.KubernetesVersion, ec2service.Amd64ArchitectureTag, "")
6160
if err != nil {
@@ -64,10 +63,7 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
6463

6564
var newImageID, newImageName string
6665

67-
destSession, err := session.NewSessionWithOptions(session.Options{
68-
SharedConfigState: session.SharedConfigEnable,
69-
Config: aws.Config{Region: aws.String(input.DestinationRegion)},
70-
})
66+
destCfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(input.DestinationRegion))
7167
if err != nil {
7268
return nil, err
7369
}
@@ -79,15 +75,15 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
7975
destinationRegion: input.DestinationRegion,
8076
encrypted: input.Encrypted,
8177
kmsKeyID: input.KmsKeyID,
82-
sess: destSession,
78+
cfg: destCfg,
8379
log: input.Log,
8480
})
8581
} else {
8682
newImageName, newImageID, err = copyWithoutSnapshot(copyWithoutSnapshotInput{
8783
sourceRegion: input.SourceRegion,
8884
image: image,
8985
dryRun: input.DryRun,
90-
sess: destSession,
86+
cfg: destCfg,
9187
log: input.Log,
9288
})
9389
}
@@ -113,9 +109,7 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
113109
},
114110
}
115111

116-
if err == nil {
117-
input.Log.Info("Completed!")
118-
}
112+
input.Log.Info("Completed!")
119113

120114
return &ami, err
121115
}
@@ -124,13 +118,13 @@ type copyWithoutSnapshotInput struct {
124118
sourceRegion string
125119
dryRun bool
126120
log logr.Logger
127-
sess *session.Session
128-
image *ec2.Image
121+
cfg aws.Config
122+
image *types.Image
129123
}
130124

131125
func copyWithoutSnapshot(input copyWithoutSnapshotInput) (string, string, error) {
132-
imgName := aws.StringValue(input.image.Name)
133-
ec2Client := ec2.New(input.sess)
126+
imgName := aws.ToString(input.image.Name)
127+
ec2Client := ec2.NewFromConfig(input.cfg)
134128
in2 := &ec2.CopyImageInput{
135129
Description: input.image.Description,
136130
DryRun: aws.Bool(input.dryRun),
@@ -139,14 +133,14 @@ func copyWithoutSnapshot(input copyWithoutSnapshotInput) (string, string, error)
139133
SourceRegion: aws.String(input.sourceRegion),
140134
}
141135
log := input.log.WithValues("imageName", imgName)
142-
log.Info("Copying the retrieved image", "imageID", aws.StringValue(input.image.ImageId), "ownerID", aws.StringValue(input.image.OwnerId))
143-
out, err := ec2Client.CopyImage(in2)
136+
log.Info("Copying the retrieved image", "imageID", aws.ToString(input.image.ImageId), "ownerID", aws.ToString(input.image.OwnerId))
137+
out, err := ec2Client.CopyImage(context.TODO(), in2)
144138
if err != nil {
145-
fmt.Printf("version %q\n", out)
139+
fmt.Printf("version %v\n", out)
146140
return imgName, "", err
147141
}
148142

149-
return imgName, aws.StringValue(out.ImageId), nil
143+
return imgName, aws.ToString(out.ImageId), nil
150144
}
151145

152146
type copyWithSnapshotInput struct {
@@ -156,12 +150,12 @@ type copyWithSnapshotInput struct {
156150
dryRun bool
157151
encrypted bool
158152
log logr.Logger
159-
image *ec2.Image
160-
sess *session.Session
153+
image *types.Image
154+
cfg aws.Config
161155
}
162156

163157
func copyWithSnapshot(input copyWithSnapshotInput) (string, string, error) {
164-
ec2Client := ec2.New(input.sess)
158+
ec2Client := ec2.NewFromConfig(input.cfg)
165159
imgName := *input.image.Name + util.RandomString(3) + strconv.Itoa(int(time.Now().Unix()))
166160
log := input.log.WithValues("imageName", imgName)
167161

@@ -175,52 +169,57 @@ func copyWithSnapshot(input copyWithSnapshotInput) (string, string, error) {
175169
}
176170

177171
copyInput := &ec2.CopySnapshotInput{
178-
Description: input.image.Description,
179-
DestinationRegion: aws.String(input.destinationRegion),
180-
DryRun: aws.Bool(input.dryRun),
181-
Encrypted: aws.Bool(input.encrypted),
182-
SourceRegion: aws.String(input.sourceRegion),
183-
KmsKeyId: kmsKeyIDPtr,
184-
SourceSnapshotId: input.image.BlockDeviceMappings[0].Ebs.SnapshotId,
172+
Description: input.image.Description,
173+
DryRun: aws.Bool(input.dryRun),
174+
Encrypted: aws.Bool(input.encrypted),
175+
SourceRegion: aws.String(input.sourceRegion),
176+
KmsKeyId: kmsKeyIDPtr,
177+
SourceSnapshotId: input.image.BlockDeviceMappings[0].Ebs.SnapshotId,
185178
}
186179

187180
// Generate a presigned url from the CopySnapshotInput
188-
req, _ := ec2Client.CopySnapshotRequest(copyInput)
189-
str, err := req.Presign(15 * time.Minute)
181+
scl := ec2.NewPresignClient(ec2Client)
182+
str, err := scl.PresignCopySnapshot(context.TODO(), copyInput, ec2.WithPresignClientFromClientOptions(
183+
func(o *ec2.Options) {
184+
o.Region = input.destinationRegion
185+
},
186+
))
190187
if err != nil {
191188
return imgName, "", errors.Wrap(err, "Failed to generate presigned url")
192189
}
193-
copyInput.PresignedUrl = aws.String(str)
190+
copyInput.PresignedUrl = aws.String(str.URL)
194191

195-
out, err := ec2Client.CopySnapshot(copyInput)
192+
out, err := ec2Client.CopySnapshot(context.TODO(), copyInput, func(o *ec2.Options) {
193+
o.Region = input.destinationRegion
194+
})
196195
if err != nil {
197196
return imgName, "", errors.Wrap(err, "Failed copying snapshot")
198197
}
199198
log.Info("Copying snapshot, this may take a couple of minutes...",
200-
"sourceSnapshot", aws.StringValue(input.image.BlockDeviceMappings[0].Ebs.SnapshotId),
201-
"destinationSnapshot", aws.StringValue(out.SnapshotId),
199+
"sourceSnapshot", aws.ToString(input.image.BlockDeviceMappings[0].Ebs.SnapshotId),
200+
"destinationSnapshot", aws.ToString(out.SnapshotId),
202201
)
203202

204-
err = ec2Client.WaitUntilSnapshotCompleted(&ec2.DescribeSnapshotsInput{
203+
err = ec2.NewSnapshotCompletedWaiter(ec2Client).Wait(context.TODO(), &ec2.DescribeSnapshotsInput{
205204
DryRun: aws.Bool(input.dryRun),
206-
SnapshotIds: []*string{out.SnapshotId},
207-
})
205+
SnapshotIds: []string{aws.ToString(out.SnapshotId)},
206+
}, time.Hour*1)
208207
if err != nil {
209-
return imgName, "", errors.Wrap(err, fmt.Sprintf("Failed waiting for encrypted snapshot copy completion: %q\n", aws.StringValue(out.SnapshotId)))
208+
return imgName, "", errors.Wrap(err, fmt.Sprintf("Failed waiting for encrypted snapshot copy completion: %q\n", aws.ToString(out.SnapshotId)))
210209
}
211210

212-
ebsMapping := &ec2.BlockDeviceMapping{
211+
ebsMapping := types.BlockDeviceMapping{
213212
DeviceName: input.image.BlockDeviceMappings[0].DeviceName,
214-
Ebs: &ec2.EbsBlockDevice{
213+
Ebs: &types.EbsBlockDevice{
215214
SnapshotId: out.SnapshotId,
216215
},
217216
}
218217

219218
log.Info("Registering AMI")
220219

221-
registerOut, err := ec2Client.RegisterImage(&ec2.RegisterImageInput{
220+
registerOut, err := ec2Client.RegisterImage(context.TODO(), &ec2.RegisterImageInput{
222221
Architecture: input.image.Architecture,
223-
BlockDeviceMappings: []*ec2.BlockDeviceMapping{ebsMapping},
222+
BlockDeviceMappings: []types.BlockDeviceMapping{ebsMapping},
224223
Description: input.image.Description,
225224
DryRun: aws.Bool(input.dryRun),
226225
EnaSupport: input.image.EnaSupport,
@@ -229,12 +228,12 @@ func copyWithSnapshot(input copyWithSnapshotInput) (string, string, error) {
229228
RamdiskId: input.image.RamdiskId,
230229
RootDeviceName: input.image.RootDeviceName,
231230
SriovNetSupport: input.image.SriovNetSupport,
232-
VirtualizationType: input.image.VirtualizationType,
231+
VirtualizationType: aws.String(string(input.image.VirtualizationType)),
233232
})
234233

235234
if err != nil {
236235
return imgName, "", err
237236
}
238237

239-
return imgName, aws.StringValue(registerOut.ImageId), err
238+
return imgName, aws.ToString(registerOut.ImageId), err
240239
}

0 commit comments

Comments
 (0)