Skip to content

Commit 4068682

Browse files
committed
CORS-4056: migrate AWS SDK to v2 in asset/machines/aws
Part of CORS-3819, this focuses on helper funcs to query instance types for machines. The changes also replaced the deprecated use of sets.String with the generic sets.Set[string] to satisfy golint.
1 parent 565c043 commit 4068682

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

pkg/asset/machines/aws/instance_types.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package aws
22

33
import (
44
"context"
5+
"fmt"
56

6-
"github.com/aws/aws-sdk-go/aws"
7-
"github.com/aws/aws-sdk-go/aws/session"
8-
"github.com/aws/aws-sdk-go/service/ec2"
7+
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/service/ec2"
9+
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
910
"github.com/pkg/errors"
1011
"k8s.io/apimachinery/pkg/util/sets"
1112

@@ -19,12 +20,15 @@ func PreferredInstanceType(ctx context.Context, meta *awsconfig.Metadata, types
1920
return "", errors.New("at least one instance type required, empty instance types given")
2021
}
2122

22-
sess, err := meta.Session(ctx)
23+
client, err := awsconfig.NewEC2Client(ctx, awsconfig.EndpointOptions{
24+
Region: meta.Region,
25+
Endpoints: meta.Services,
26+
})
2327
if err != nil {
24-
return types[0], err
28+
return "", fmt.Errorf("failed to create EC2 client: %w", err)
2529
}
2630

27-
found, err := getInstanceTypeZoneInfo(ctx, sess, meta.Region, types, zones)
31+
found, err := getInstanceTypeZoneInfo(ctx, client, types, zones)
2832
if err != nil {
2933
return types[0], err
3034
}
@@ -41,49 +45,50 @@ func PreferredInstanceType(ctx context.Context, meta *awsconfig.Metadata, types
4145
// FilterZonesBasedOnInstanceType return a filtered list of zones where the particular instance type is available. This is mainly necessary for ARM, where the instance type m6g is not
4246
// available in all availability zones.
4347
func FilterZonesBasedOnInstanceType(ctx context.Context, meta *awsconfig.Metadata, instanceType string, zones []string) ([]string, error) {
44-
sess, err := meta.Session(ctx)
48+
client, err := awsconfig.NewEC2Client(ctx, awsconfig.EndpointOptions{
49+
Region: meta.Region,
50+
Endpoints: meta.Services,
51+
})
4552
if err != nil {
46-
return zones, err
53+
return nil, fmt.Errorf("failed to create EC2 client: %w", err)
4754
}
4855

4956
types := []string{instanceType}
50-
found, err := getInstanceTypeZoneInfo(ctx, sess, meta.Region, types, zones)
57+
found, err := getInstanceTypeZoneInfo(ctx, client, types, zones)
5158
if err != nil {
5259
return zones, err
5360
}
5461

55-
return found[instanceType].Intersection(sets.NewString(zones...)).List(), nil
62+
return found[instanceType].Intersection(sets.New(zones...)).UnsortedList(), nil
5663
}
5764

58-
func getInstanceTypeZoneInfo(ctx context.Context, session *session.Session, region string, types []string, zones []string) (map[string]sets.String, error) {
59-
found := map[string]sets.String{}
60-
61-
client := ec2.New(session, aws.NewConfig().WithRegion(region))
62-
resp, err := client.DescribeInstanceTypeOfferingsWithContext(ctx, &ec2.DescribeInstanceTypeOfferingsInput{
63-
Filters: []*ec2.Filter{
65+
func getInstanceTypeZoneInfo(ctx context.Context, client *ec2.Client, types []string, zones []string) (map[string]sets.Set[string], error) {
66+
found := map[string]sets.Set[string]{}
67+
resp, err := client.DescribeInstanceTypeOfferings(ctx, &ec2.DescribeInstanceTypeOfferingsInput{
68+
Filters: []ec2types.Filter{
6469
{
6570
Name: aws.String("location"),
66-
Values: aws.StringSlice(zones),
71+
Values: zones,
6772
},
6873
{
6974
Name: aws.String("instance-type"),
70-
Values: aws.StringSlice(types),
75+
Values: types,
7176
},
7277
},
73-
LocationType: aws.String("availability-zone"),
78+
LocationType: ec2types.LocationTypeAvailabilityZone,
7479
})
7580
if err != nil {
7681
return found, err
7782
}
7883

7984
// iterate through the offerings and create a map of instance type keys to location values
8085
for _, offering := range resp.InstanceTypeOfferings {
81-
f, ok := found[aws.StringValue(offering.InstanceType)]
86+
f, ok := found[string(offering.InstanceType)]
8287
if !ok {
83-
f = sets.NewString()
84-
found[aws.StringValue(offering.InstanceType)] = f
88+
f = sets.New[string]()
89+
found[string(offering.InstanceType)] = f
8590
}
86-
f.Insert(aws.StringValue(offering.Location))
91+
f.Insert(aws.ToString(offering.Location))
8792
}
8893
return found, nil
8994
}

0 commit comments

Comments
 (0)