From 181bdc93b14d58d34f4cf288a836548825980560 Mon Sep 17 00:00:00 2001 From: Cameron McAvoy Date: Wed, 4 Sep 2024 14:24:39 -0500 Subject: [PATCH 1/2] Add more data to unit test for sorting subnets with multiple vpcs Signed-off-by: Cameron McAvoy --- pkg/cloud/services/ec2/instances_test.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/cloud/services/ec2/instances_test.go b/pkg/cloud/services/ec2/instances_test.go index 654403427f..e53fd3feb6 100644 --- a/pkg/cloud/services/ec2/instances_test.go +++ b/pkg/cloud/services/ec2/instances_test.go @@ -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) From b6eda5cb13daac921ac2110f4a88f5a6c994f3b1 Mon Sep 17 00:00:00 2001 From: Cameron McAvoy Date: Wed, 4 Sep 2024 14:29:15 -0500 Subject: [PATCH 2/2] Fix subnet sorting with multiple vpcs when launching an instance Signed-off-by: Cameron McAvoy --- pkg/cloud/services/ec2/instances.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cloud/services/ec2/instances.go b/pkg/cloud/services/ec2/instances.go index ac11fea9fd..f5757e04a2 100644 --- a/pkg/cloud/services/ec2/instances.go +++ b/pkg/cloud/services/ec2/instances.go @@ -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.",