Skip to content

Commit 85759ce

Browse files
authored
Merge pull request #5114 from cnmcavoy/cmcavoy/fix-subnet-sorting
🐛 Fix subnet sorting with multiple vpcs when launching an instance
2 parents b32622d + b6eda5c commit 85759ce

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

pkg/cloud/services/ec2/instances.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,10 @@ func (s *Service) findSubnet(scope *scope.MachineScope) (string, error) {
370370

371371
filtered = append(filtered, subnet)
372372
}
373-
// prefer a subnet in the cluster VPC if multiple match
373+
// keep AWS returned orderz stable, but prefer a subnet in the cluster VPC
374374
clusterVPC := s.scope.VPC().ID
375375
sort.SliceStable(filtered, func(i, j int) bool {
376-
return strings.Compare(*filtered[i].VpcId, clusterVPC) > strings.Compare(*filtered[j].VpcId, clusterVPC)
376+
return *filtered[i].VpcId == clusterVPC
377377
})
378378
if len(filtered) == 0 {
379379
errMessage = fmt.Sprintf("failed to run machine %q, found %d subnets matching criteria but post-filtering failed.",

pkg/cloud/services/ec2/instances_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,17 +656,25 @@ func TestCreateInstance(t *testing.T) {
656656
}})).Return(&ec2.DescribeSubnetsOutput{
657657
Subnets: []*ec2.Subnet{
658658
{
659-
VpcId: aws.String("vpc-bar"),
659+
VpcId: aws.String("vpc-incorrect-1"),
660+
SubnetId: aws.String("subnet-5"),
661+
AvailabilityZone: aws.String("us-east-1c"),
662+
CidrBlock: aws.String("10.0.12.0/24"),
663+
MapPublicIpOnLaunch: aws.Bool(false),
664+
},
665+
{
666+
VpcId: aws.String("vpc-incorrect-2"),
660667
SubnetId: aws.String("subnet-4"),
661668
AvailabilityZone: aws.String("us-east-1c"),
662669
CidrBlock: aws.String("10.0.10.0/24"),
663670
MapPublicIpOnLaunch: aws.Bool(false),
664671
},
665672
{
666-
VpcId: aws.String("vpc-foo"),
667-
SubnetId: aws.String("subnet-3"),
668-
AvailabilityZone: aws.String("us-east-1c"),
669-
CidrBlock: aws.String("10.0.11.0/24"),
673+
VpcId: aws.String("vpc-foo"),
674+
SubnetId: aws.String("subnet-3"),
675+
AvailabilityZone: aws.String("us-east-1c"),
676+
CidrBlock: aws.String("10.0.11.0/24"),
677+
MapPublicIpOnLaunch: aws.Bool(false),
670678
},
671679
},
672680
}, nil)

0 commit comments

Comments
 (0)