Skip to content

Commit

Permalink
Merge pull request #5114 from cnmcavoy/cmcavoy/fix-subnet-sorting
Browse files Browse the repository at this point in the history
🐛 Fix subnet sorting with multiple vpcs when launching an instance
  • Loading branch information
k8s-ci-robot authored Nov 4, 2024
2 parents b32622d + b6eda5c commit 85759ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pkg/cloud/services/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@ func (s *Service) findSubnet(scope *scope.MachineScope) (string, error) {

filtered = append(filtered, subnet)
}
// prefer a subnet in the cluster VPC if multiple match
// keep AWS returned orderz stable, but prefer a subnet in the cluster VPC
clusterVPC := s.scope.VPC().ID
sort.SliceStable(filtered, func(i, j int) bool {
return strings.Compare(*filtered[i].VpcId, clusterVPC) > strings.Compare(*filtered[j].VpcId, clusterVPC)
return *filtered[i].VpcId == clusterVPC
})
if len(filtered) == 0 {
errMessage = fmt.Sprintf("failed to run machine %q, found %d subnets matching criteria but post-filtering failed.",
Expand Down
18 changes: 13 additions & 5 deletions pkg/cloud/services/ec2/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,17 +656,25 @@ func TestCreateInstance(t *testing.T) {
}})).Return(&ec2.DescribeSubnetsOutput{
Subnets: []*ec2.Subnet{
{
VpcId: aws.String("vpc-bar"),
VpcId: aws.String("vpc-incorrect-1"),
SubnetId: aws.String("subnet-5"),
AvailabilityZone: aws.String("us-east-1c"),
CidrBlock: aws.String("10.0.12.0/24"),
MapPublicIpOnLaunch: aws.Bool(false),
},
{
VpcId: aws.String("vpc-incorrect-2"),
SubnetId: aws.String("subnet-4"),
AvailabilityZone: aws.String("us-east-1c"),
CidrBlock: aws.String("10.0.10.0/24"),
MapPublicIpOnLaunch: aws.Bool(false),
},
{
VpcId: aws.String("vpc-foo"),
SubnetId: aws.String("subnet-3"),
AvailabilityZone: aws.String("us-east-1c"),
CidrBlock: aws.String("10.0.11.0/24"),
VpcId: aws.String("vpc-foo"),
SubnetId: aws.String("subnet-3"),
AvailabilityZone: aws.String("us-east-1c"),
CidrBlock: aws.String("10.0.11.0/24"),
MapPublicIpOnLaunch: aws.Bool(false),
},
},
}, nil)
Expand Down

0 comments on commit 85759ce

Please sign in to comment.