diff --git a/pkg/asset/installconfig/aws/subnet.go b/pkg/asset/installconfig/aws/subnet.go index 3581f20cbc2..bf3022a9c41 100644 --- a/pkg/asset/installconfig/aws/subnet.go +++ b/pkg/asset/installconfig/aws/subnet.go @@ -36,40 +36,52 @@ func subnets(ctx context.Context, session *session.Session, region string, ids [ for _, id := range ids { idPointers = append(idPointers, aws.String(id)) } - results, err := client.DescribeSubnetsWithContext( // FIXME: port to DescribeSubnetsPagesWithContext once we bump our vendored AWS package past v1.19.30 + + var lastError error + err = client.DescribeSubnetsPagesWithContext( ctx, &ec2.DescribeSubnetsInput{SubnetIds: idPointers}, + func(results *ec2.DescribeSubnetsOutput, lastPage bool) bool { + for _, subnet := range results.Subnets { + if subnet.SubnetId == nil { + continue + } + if subnet.SubnetArn == nil { + lastError = errors.Errorf("%s has no ARN", *subnet.SubnetId) + return false + } + if subnet.VpcId == nil { + lastError = errors.Errorf("%s has no VPC", *subnet.SubnetId) + return false + } + if subnet.AvailabilityZone == nil { + lastError = errors.Errorf("%s has not availability zone", *subnet.SubnetId) + return false + } + + if vpc == "" { + vpc = *subnet.VpcId + vpcFromSubnet = *subnet.SubnetId + } else if *subnet.VpcId != vpc { + lastError = errors.Errorf("all subnets must belong to the same VPC: %s is from %s, but %s is from %s", *subnet.SubnetId, *subnet.VpcId, vpcFromSubnet, vpc) + return false + } + + metas[*subnet.SubnetId] = Subnet{ + ARN: *subnet.SubnetArn, + Zone: *subnet.AvailabilityZone, + CIDR: *subnet.CidrBlock, + } + } + return !lastPage + }, ) + if err == nil { + err = lastError + } if err != nil { return vpc, nil, nil, errors.Wrap(err, "describing subnets") } - for _, subnet := range results.Subnets { - if subnet.SubnetId == nil { - continue - } - if subnet.SubnetArn == nil { - return vpc, nil, nil, errors.Errorf("%s has no ARN", *subnet.SubnetId) - } - if subnet.VpcId == nil { - return vpc, nil, nil, errors.Errorf("%s has no VPC", *subnet.SubnetId) - } - if subnet.AvailabilityZone == nil { - return vpc, nil, nil, errors.Errorf("%s has no availability zone", *subnet.SubnetId) - } - - if vpc == "" { - vpc = *subnet.VpcId - vpcFromSubnet = *subnet.SubnetId - } else if *subnet.VpcId != vpc { - return vpc, nil, nil, errors.Errorf("all subnets must belong to the same VPC: %s is from %s, but %s is from %s", *subnet.SubnetId, *subnet.VpcId, vpcFromSubnet, vpc) - } - - metas[*subnet.SubnetId] = Subnet{ - ARN: *subnet.SubnetArn, - Zone: *subnet.AvailabilityZone, - CIDR: *subnet.CidrBlock, - } - } var routeTables []*ec2.RouteTable err = client.DescribeRouteTablesPagesWithContext( diff --git a/pkg/destroy/aws/aws.go b/pkg/destroy/aws/aws.go index c4346c09a12..6366339335a 100644 --- a/pkg/destroy/aws/aws.go +++ b/pkg/destroy/aws/aws.go @@ -1297,8 +1297,8 @@ func deleteEC2Subnet(ctx context.Context, client *ec2.EC2, id string, logger log } func deleteEC2SubnetsByVPC(ctx context.Context, client *ec2.EC2, vpc string, failFast bool, logger logrus.FieldLogger) error { - // FIXME: port to DescribeSubnetsPages once we bump our vendored AWS package past v1.19.30 - results, err := client.DescribeSubnetsWithContext( + var lastError error + err := client.DescribeSubnetsPagesWithContext( ctx, &ec2.DescribeSubnetsInput{ Filters: []*ec2.Filter{ @@ -1308,26 +1308,27 @@ func deleteEC2SubnetsByVPC(ctx context.Context, client *ec2.EC2, vpc string, fai }, }, }, + func(results *ec2.DescribeSubnetsOutput, lastPage bool) bool { + for _, subnet := range results.Subnets { + err := deleteEC2Subnet(ctx, client, *subnet.SubnetId, logger.WithField("subnet", *subnet.SubnetId)) + if err != nil { + err = errors.Wrapf(err, "deleting EC2 subnet %s", *subnet.SubnetId) + if lastError != nil { + logger.Debug(lastError) + } + lastError = err + if failFast { + return false + } + } + } + return !lastPage + }, ) if err != nil { return err } - var lastError error - for _, subnet := range results.Subnets { - err := deleteEC2Subnet(ctx, client, *subnet.SubnetId, logger.WithField("subnet", *subnet.SubnetId)) - if err != nil { - err = errors.Wrapf(err, "deleting EC2 subnet %s", *subnet.SubnetId) - if failFast { - return err - } - if lastError != nil { - logger.Debug(lastError) - } - lastError = err - } - } - return lastError }