Skip to content

Commit

Permalink
privatelink: selectHostedZoneVPC to return a pointer to the selected vpc
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuever committed Dec 16, 2024
1 parent 20cdfee commit b90104b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
18 changes: 8 additions & 10 deletions pkg/controller/privatelink/actuator/awsactuator/awshubactuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (a *AWSHubActuator) ensureHostedZone(cd *hivev1.ClusterDeployment, metadata
return false, "", err
}

newHzID, err := a.createHostedZone(&selectedVPC, apiDomain)
newHzID, err := a.createHostedZone(selectedVPC, apiDomain)
if err != nil {
return false, "", err
}
Expand Down Expand Up @@ -709,9 +709,7 @@ func (a *AWSHubActuator) getEndpointVPC(cd *hivev1.ClusterDeployment, metadata *
return endpointVPC, nil
}

func (a *AWSHubActuator) selectHostedZoneVPC(cd *hivev1.ClusterDeployment, metadata *hivev1.ClusterMetadata, logger log.FieldLogger) (hivev1.AWSAssociatedVPC, error) {
selectedVPC := hivev1.AWSAssociatedVPC{}

func (a *AWSHubActuator) selectHostedZoneVPC(cd *hivev1.ClusterDeployment, metadata *hivev1.ClusterMetadata, logger log.FieldLogger) (*hivev1.AWSAssociatedVPC, error) {
// For clusterdeployments that are on AWS, use the VPCEndpoint VPC
if cd.Status.Platform != nil &&
cd.Status.Platform.AWS != nil &&
Expand All @@ -720,29 +718,29 @@ func (a *AWSHubActuator) selectHostedZoneVPC(cd *hivev1.ClusterDeployment, metad

endpointVPC, err := a.getEndpointVPC(cd, metadata)
if err != nil {
return selectedVPC, errors.Wrap(err, "error getting Endpoint VPC")
return nil, errors.Wrap(err, "error getting Endpoint VPC")
}

if endpointVPC.VPCID == "" {
return selectedVPC, errors.New("unable to select Endpoint VPC: Endpoint not found")
return nil, errors.New("unable to select Endpoint VPC: Endpoint not found")
}

return endpointVPC, nil
return &endpointVPC, nil
}

associatedVPCS, err := a.getAssociatedVPCs(cd, metadata, logger)
if err != nil {
return selectedVPC, errors.Wrap(err, "error getting associated VPCs")
return nil, errors.Wrap(err, "error getting associated VPCs")
}

// Select the first associatedVPC that uses the primary AWS PrivateLink credential.
// This is necessary because a Hosted Zone can only be created using a VPC owned by the same account.
for _, associatedVPC := range associatedVPCS {
if associatedVPC.CredentialsSecretRef == nil || *associatedVPC.CredentialsSecretRef == a.config.CredentialsSecretRef {
return associatedVPC, nil
return &associatedVPC, nil
}
}

// No VPCs found that match the criteria, return an error.
return selectedVPC, errors.New("unable to find an associatedVPC that uses the primary AWS PrivateLink credentials")
return nil, errors.New("unable to find an associatedVPC that uses the primary AWS PrivateLink credentials")
}
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,7 @@ func Test_selectHostedZoneVPC(t *testing.T) {

AWSClientConfig func(*mock.MockClient)

expect hivev1.AWSAssociatedVPC
expect *hivev1.AWSAssociatedVPC
expectError string
}{{ // There should be an error if VPCEndpointID is set and getEndpointVPC fails
name: "VPCEndpointID, getEndpointVPC failure",
Expand All @@ -1808,7 +1808,6 @@ func Test_selectHostedZoneVPC(t *testing.T) {
AWSClientConfig: func(m *mock.MockClient) {
m.EXPECT().DescribeVpcEndpoints(gomock.Any()).Return(nil, awserr.New("AccessDenied", "not authorized to DescribeVpcEndpoints", nil))
},
expect: hivev1.AWSAssociatedVPC{},
expectError: "error getting Endpoint VPC: error getting the VPC Endpoint: AccessDenied: not authorized to DescribeVpcEndpoints",
}, { // There should be an error if VPCEndPointID is set and getEndpointVPC returns an empty VPCID
name: "VPCEndpointID, getEndpointVPC return empty vpcid",
Expand All @@ -1826,7 +1825,6 @@ func Test_selectHostedZoneVPC(t *testing.T) {
VpcEndpoints: []*ec2.VpcEndpoint{{VpcId: aws.String("")}},
}, nil)
},
expect: hivev1.AWSAssociatedVPC{},
expectError: "unable to select Endpoint VPC: Endpoint not found",
}, { // The AWS VPCEndpointID VPC should be used when set
name: "VPCEndpointID, success",
Expand All @@ -1844,7 +1842,7 @@ func Test_selectHostedZoneVPC(t *testing.T) {
VpcEndpoints: []*ec2.VpcEndpoint{mockEndpoint},
}, nil)
},
expect: hivev1.AWSAssociatedVPC{
expect: &hivev1.AWSAssociatedVPC{
AWSPrivateLinkVPC: hivev1.AWSPrivateLinkVPC{
VPCID: *mockEndpoint.VpcId,
Region: testRegion,
Expand All @@ -1863,7 +1861,7 @@ func Test_selectHostedZoneVPC(t *testing.T) {
AWSPrivateLinkVPC: hivev1.AWSPrivateLinkVPC{VPCID: "vpc-2", Region: testRegion},
}},
},
expect: hivev1.AWSAssociatedVPC{
expect: &hivev1.AWSAssociatedVPC{
AWSPrivateLinkVPC: hivev1.AWSPrivateLinkVPC{
VPCID: "vpc-2",
Region: testRegion,
Expand All @@ -1881,7 +1879,7 @@ func Test_selectHostedZoneVPC(t *testing.T) {
CredentialsSecretRef: &corev1.LocalObjectReference{Name: "credential-1"},
}},
},
expect: hivev1.AWSAssociatedVPC{
expect: &hivev1.AWSAssociatedVPC{
AWSPrivateLinkVPC: hivev1.AWSPrivateLinkVPC{
VPCID: "vpc-2",
Region: testRegion,
Expand Down

0 comments on commit b90104b

Please sign in to comment.