From ab4e161fbbff13d53ab3ae27a60c82a988c7afd9 Mon Sep 17 00:00:00 2001 From: Aswin Suryanarayanan Date: Tue, 12 Nov 2024 15:32:58 -0500 Subject: [PATCH] Restore for loop in aws cloud prepare for owned filters (#1044) * Restore for loop in aws cloud prepare for owned filters While retrieving vpc the owned filters should be tried one after the other than together. Signed-off-by: Aswin Suryanarayanan --- pkg/aws/aws_cloud_test.go | 2 ++ pkg/aws/aws_suite_test.go | 18 +++++++++++++++++- pkg/aws/ocpgwdeployer_test.go | 1 + pkg/aws/vpcs.go | 20 +++++++++++++------- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/pkg/aws/aws_cloud_test.go b/pkg/aws/aws_cloud_test.go index 4ffac4eb..08da4834 100644 --- a/pkg/aws/aws_cloud_test.go +++ b/pkg/aws/aws_cloud_test.go @@ -40,6 +40,7 @@ func testOpenPorts() { JustBeforeEach(func() { t.expectDescribeVpcs(t.vpcID) + t.expectDescribeVpcsSigs(t.vpcID) t.expectDescribePublicSubnets(t.subnets...) retError = t.cloud.OpenPorts([]api.PortSpec{ @@ -116,6 +117,7 @@ func testClosePorts() { JustBeforeEach(func() { t.expectDescribeVpcs(t.vpcID) + t.expectDescribeVpcsSigs(t.vpcID) t.expectDescribePublicSubnets(t.subnets...) t.expectDescribePublicSubnetsSigs(t.subnets...) diff --git a/pkg/aws/aws_suite_test.go b/pkg/aws/aws_suite_test.go index dfa54184..8bf50ebe 100644 --- a/pkg/aws/aws_suite_test.go +++ b/pkg/aws/aws_suite_test.go @@ -111,8 +111,24 @@ func (f *fakeAWSClientBase) expectDescribeVpcs(vpcID string) { }, { Name: ptr.To(clusterFilterTagName), Values: []string{"owned"}, + }}}).Matches))).Return(&ec2.DescribeVpcsOutput{Vpcs: vpcs}, nil).Maybe() +} + +func (f *fakeAWSClientBase) expectDescribeVpcsSigs(vpcID string) { + var vpcs []types.Vpc + if vpcID != "" { + vpcs = []types.Vpc{ + { + VpcId: ptr.To(vpcID), + }, + } + } + + f.awsClient.EXPECT().DescribeVpcs(mock.Anything, mock.MatchedBy(((&filtersMatcher{expectedFilters: []types.Filter{{ + Name: ptr.To("tag:Name"), + Values: []string{infraID + "-vpc"}, }, { - Name: ptr.To(providerAWSTagPrefix + infraID), + Name: ptr.To(clusterFilterTagNameSigs), Values: []string{"owned"}, }}}).Matches))).Return(&ec2.DescribeVpcsOutput{Vpcs: vpcs}, nil).Maybe() } diff --git a/pkg/aws/ocpgwdeployer_test.go b/pkg/aws/ocpgwdeployer_test.go index a4b93a83..9c5c6925 100644 --- a/pkg/aws/ocpgwdeployer_test.go +++ b/pkg/aws/ocpgwdeployer_test.go @@ -284,6 +284,7 @@ func newGatewayDeployerTestDriver() *gatewayDeployerTestDriver { t.expectDescribeInstances(instanceImageID) t.expectDescribeSecurityGroups(workerSGName, workerGroupID) t.expectDescribePublicSubnets(t.subnets...) + t.expectDescribeVpcsSigs(t.vpcID) t.expectDescribePublicSubnetsSigs(t.subnets...) var err error diff --git a/pkg/aws/vpcs.go b/pkg/aws/vpcs.go index 9e038a58..4c0621d8 100644 --- a/pkg/aws/vpcs.go +++ b/pkg/aws/vpcs.go @@ -42,14 +42,20 @@ func (ac *awsCloud) getVpcID() (string, error) { ownedFilters := ac.filterByCurrentCluster() vpcName := ac.withAWSInfo("{infraID}-vpc") - filters := []types.Filter{ - ac.filterByName(vpcName), - } - filters = append(filters, ownedFilters...) + for i := range ownedFilters { + filters := []types.Filter{ + ac.filterByName(vpcName), + ownedFilters[i], + } - result, err = ac.client.DescribeVpcs(context.TODO(), &ec2.DescribeVpcsInput{Filters: filters}) - if err != nil { - return "", errors.Wrap(err, "error describing AWS VPCs") + result, err = ac.client.DescribeVpcs(context.TODO(), &ec2.DescribeVpcsInput{Filters: filters}) + if err != nil { + return "", errors.Wrap(err, "error describing AWS VPCs") + } + + if len(result.Vpcs) != 0 { + break + } } if len(result.Vpcs) == 0 {