Skip to content

Commit

Permalink
Paginate DescribeInstanceTypeOfferings response (#6832)
Browse files Browse the repository at this point in the history
  • Loading branch information
TiberiuGC authored Jul 20, 2023
1 parent 9f14b30 commit b963046
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/az/az.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func FilterBasedOnAvailability(ctx context.Context, zones []string, np []api.Nod
}

func GetInstanceTypeOfferings(ctx context.Context, ec2API awsapi.EC2, instances []string, zones []string) (map[string]map[string]struct{}, error) {
output, err := ec2API.DescribeInstanceTypeOfferings(ctx, &ec2.DescribeInstanceTypeOfferingsInput{
var instanceTypeOfferings []ec2types.InstanceTypeOffering
p := ec2.NewDescribeInstanceTypeOfferingsPaginator(ec2API, &ec2.DescribeInstanceTypeOfferingsInput{
Filters: []ec2types.Filter{
{
Name: aws.String("instance-type"),
Expand All @@ -134,14 +135,18 @@ func GetInstanceTypeOfferings(ctx context.Context, ec2API awsapi.EC2, instances
LocationType: ec2types.LocationTypeAvailabilityZone,
MaxResults: aws.Int32(100),
})
if err != nil {
return nil, fmt.Errorf("unable to list offerings for instance types: %w", err)
for p.HasMorePages() {
output, err := p.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("unable to list offerings for instance types %w", err)
}
instanceTypeOfferings = append(instanceTypeOfferings, output.InstanceTypeOfferings...)
}

// zoneToInstanceMap['us-west-1b']['t2.small']=struct{}{}
// zoneToInstanceMap['us-west-1b']['t2.large']=struct{}{}
zoneToInstanceMap := make(map[string]map[string]struct{})
for _, offer := range output.InstanceTypeOfferings {
for _, offer := range instanceTypeOfferings {
if _, ok := zoneToInstanceMap[aws.ToString(offer.Location)]; !ok {
zoneToInstanceMap[aws.ToString(offer.Location)] = make(map[string]struct{})
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/az/az_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ var _ = Describe("AZ", func() {
createAvailabilityZone(region, ec2types.AvailabilityZoneStateAvailable, "zone4"),
},
}, nil)
// split DescribeInstanceTypeOfferings response in two pages so we unit test the use of the paginator at the same time
p.MockEC2().On("DescribeInstanceTypeOfferings", mock.Anything, &ec2.DescribeInstanceTypeOfferingsInput{
Filters: []ec2types.Filter{
{
Expand All @@ -215,6 +216,7 @@ var _ = Describe("AZ", func() {
LocationType: ec2types.LocationTypeAvailabilityZone,
MaxResults: aws.Int32(100),
}).Return(&ec2.DescribeInstanceTypeOfferingsOutput{
NextToken: aws.String("token"),
InstanceTypeOfferings: []ec2types.InstanceTypeOffering{
{
InstanceType: "t2.small",
Expand All @@ -231,6 +233,24 @@ var _ = Describe("AZ", func() {
Location: aws.String("zone4"),
LocationType: "availability-zone",
},
},
}, nil)
p.MockEC2().On("DescribeInstanceTypeOfferings", mock.Anything, &ec2.DescribeInstanceTypeOfferingsInput{
Filters: []ec2types.Filter{
{
Name: aws.String("instance-type"),
Values: []string{"t2.small", "t2.medium"},
},
{
Name: aws.String("location"),
Values: []string{"zone1", "zone2", "zone3", "zone4"},
},
},
LocationType: ec2types.LocationTypeAvailabilityZone,
MaxResults: aws.Int32(100),
NextToken: aws.String("token"),
}).Return(&ec2.DescribeInstanceTypeOfferingsOutput{
InstanceTypeOfferings: []ec2types.InstanceTypeOffering{
{
InstanceType: "t2.medium",
Location: aws.String("zone1"),
Expand Down

0 comments on commit b963046

Please sign in to comment.