Skip to content

Commit eab4e06

Browse files
committed
privatelink: selectHostedZoneVPC to return a pointer to the selected vpc
1 parent 5749994 commit eab4e06

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

pkg/controller/privatelink/actuator/awsactuator/awshubactuator.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (a *AWSHubActuator) ensureHostedZone(cd *hivev1.ClusterDeployment, metadata
217217
return false, "", err
218218
}
219219

220-
newHzID, err := a.createHostedZone(&selectedVPC, apiDomain)
220+
newHzID, err := a.createHostedZone(selectedVPC, apiDomain)
221221
if err != nil {
222222
return false, "", err
223223
}
@@ -713,9 +713,7 @@ func (a *AWSHubActuator) getEndpointVPC(cd *hivev1.ClusterDeployment, metadata *
713713
return endpointVPC, nil
714714
}
715715

716-
func (a *AWSHubActuator) selectHostedZoneVPC(cd *hivev1.ClusterDeployment, metadata *hivev1.ClusterMetadata, logger log.FieldLogger) (hivev1.AWSAssociatedVPC, error) {
717-
selectedVPC := hivev1.AWSAssociatedVPC{}
718-
716+
func (a *AWSHubActuator) selectHostedZoneVPC(cd *hivev1.ClusterDeployment, metadata *hivev1.ClusterMetadata, logger log.FieldLogger) (*hivev1.AWSAssociatedVPC, error) {
719717
// For clusterdeployments that are on AWS, use the VPCEndpoint VPC
720718
if cd.Status.Platform != nil &&
721719
cd.Status.Platform.AWS != nil &&
@@ -724,29 +722,29 @@ func (a *AWSHubActuator) selectHostedZoneVPC(cd *hivev1.ClusterDeployment, metad
724722

725723
endpointVPC, err := a.getEndpointVPC(cd, metadata)
726724
if err != nil {
727-
return selectedVPC, errors.Wrap(err, "error getting Endpoint VPC")
725+
return nil, errors.Wrap(err, "error getting Endpoint VPC")
728726
}
729727

730728
if endpointVPC.VPCID == "" {
731-
return selectedVPC, errors.New("unable to select Endpoint VPC: Endpoint not found")
729+
return nil, errors.New("unable to select Endpoint VPC: Endpoint not found")
732730
}
733731

734-
return endpointVPC, nil
732+
return &endpointVPC, nil
735733
}
736734

737735
associatedVPCS, err := a.getAssociatedVPCs(cd, metadata, logger)
738736
if err != nil {
739-
return selectedVPC, errors.Wrap(err, "error getting associated VPCs")
737+
return nil, errors.Wrap(err, "error getting associated VPCs")
740738
}
741739

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

750748
// No VPCs found that match the criteria, return an error.
751-
return selectedVPC, errors.New("unable to find an associatedVPC that uses the primary AWS PrivateLink credentials")
749+
return nil, errors.New("unable to find an associatedVPC that uses the primary AWS PrivateLink credentials")
752750
}

pkg/controller/privatelink/actuator/awsactuator/awshubactuator_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ func Test_selectHostedZoneVPC(t *testing.T) {
17921792

17931793
AWSClientConfig func(*mock.MockClient)
17941794

1795-
expect hivev1.AWSAssociatedVPC
1795+
expect *hivev1.AWSAssociatedVPC
17961796
expectError string
17971797
}{{ // There should be an error if VPCEndpointID is set and getEndpointVPC fails
17981798
name: "VPCEndpointID, getEndpointVPC failure",
@@ -1808,7 +1808,6 @@ func Test_selectHostedZoneVPC(t *testing.T) {
18081808
AWSClientConfig: func(m *mock.MockClient) {
18091809
m.EXPECT().DescribeVpcEndpoints(gomock.Any()).Return(nil, awserr.New("AccessDenied", "not authorized to DescribeVpcEndpoints", nil))
18101810
},
1811-
expect: hivev1.AWSAssociatedVPC{},
18121811
expectError: "error getting Endpoint VPC: error getting the VPC Endpoint: AccessDenied: not authorized to DescribeVpcEndpoints",
18131812
}, { // There should be an error if VPCEndPointID is set and getEndpointVPC returns an empty VPCID
18141813
name: "VPCEndpointID, getEndpointVPC return empty vpcid",
@@ -1826,7 +1825,6 @@ func Test_selectHostedZoneVPC(t *testing.T) {
18261825
VpcEndpoints: []*ec2.VpcEndpoint{{VpcId: aws.String("")}},
18271826
}, nil)
18281827
},
1829-
expect: hivev1.AWSAssociatedVPC{},
18301828
expectError: "unable to select Endpoint VPC: Endpoint not found",
18311829
}, { // The AWS VPCEndpointID VPC should be used when set
18321830
name: "VPCEndpointID, success",
@@ -1844,7 +1842,7 @@ func Test_selectHostedZoneVPC(t *testing.T) {
18441842
VpcEndpoints: []*ec2.VpcEndpoint{mockEndpoint},
18451843
}, nil)
18461844
},
1847-
expect: hivev1.AWSAssociatedVPC{
1845+
expect: &hivev1.AWSAssociatedVPC{
18481846
AWSPrivateLinkVPC: hivev1.AWSPrivateLinkVPC{
18491847
VPCID: *mockEndpoint.VpcId,
18501848
Region: testRegion,
@@ -1863,7 +1861,7 @@ func Test_selectHostedZoneVPC(t *testing.T) {
18631861
AWSPrivateLinkVPC: hivev1.AWSPrivateLinkVPC{VPCID: "vpc-2", Region: testRegion},
18641862
}},
18651863
},
1866-
expect: hivev1.AWSAssociatedVPC{
1864+
expect: &hivev1.AWSAssociatedVPC{
18671865
AWSPrivateLinkVPC: hivev1.AWSPrivateLinkVPC{
18681866
VPCID: "vpc-2",
18691867
Region: testRegion,
@@ -1881,7 +1879,7 @@ func Test_selectHostedZoneVPC(t *testing.T) {
18811879
CredentialsSecretRef: &corev1.LocalObjectReference{Name: "credential-1"},
18821880
}},
18831881
},
1884-
expect: hivev1.AWSAssociatedVPC{
1882+
expect: &hivev1.AWSAssociatedVPC{
18851883
AWSPrivateLinkVPC: hivev1.AWSPrivateLinkVPC{
18861884
VPCID: "vpc-2",
18871885
Region: testRegion,

0 commit comments

Comments
 (0)