Skip to content

Commit

Permalink
Restore for loop in aws cloud prepare for owned filters (#1044)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
aswinsuryan authored Nov 12, 2024
1 parent f834a99 commit ab4e161
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 2 additions & 0 deletions pkg/aws/aws_cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -116,6 +117,7 @@ func testClosePorts() {

JustBeforeEach(func() {
t.expectDescribeVpcs(t.vpcID)
t.expectDescribeVpcsSigs(t.vpcID)
t.expectDescribePublicSubnets(t.subnets...)
t.expectDescribePublicSubnetsSigs(t.subnets...)

Expand Down
18 changes: 17 additions & 1 deletion pkg/aws/aws_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions pkg/aws/ocpgwdeployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 13 additions & 7 deletions pkg/aws/vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ab4e161

Please sign in to comment.