@@ -15,11 +15,19 @@ type lbInput struct {
1515 region string
1616 resourceGroup string
1717 subscriptionID string
18- pipClient * armnetwork.PublicIPAddressesClient
1918 lbClient * armnetwork.LoadBalancersClient
2019 tags map [string ]* string
2120}
2221
22+ type pipInput struct {
23+ infraID string
24+ name string
25+ region string
26+ resourceGroup string
27+ pipClient * armnetwork.PublicIPAddressesClient
28+ tags map [string ]* string
29+ }
30+
2331type vmInput struct {
2432 infraID string
2533 resourceGroup string
@@ -29,15 +37,13 @@ type vmInput struct {
2937 nicClient * armnetwork.InterfacesClient
3038}
3139
32- func createPublicIP (ctx context.Context , in * lbInput ) (* armnetwork.PublicIPAddress , error ) {
33- publicIPAddressName := fmt .Sprintf ("%s-pip-v4" , in .infraID )
34-
40+ func createPublicIP (ctx context.Context , in * pipInput ) (* armnetwork.PublicIPAddress , error ) {
3541 pollerResp , err := in .pipClient .BeginCreateOrUpdate (
3642 ctx ,
3743 in .resourceGroup ,
38- publicIPAddressName ,
44+ in . name ,
3945 armnetwork.PublicIPAddress {
40- Name : to .Ptr (publicIPAddressName ),
46+ Name : to .Ptr (in . name ),
4147 Location : to .Ptr (in .region ),
4248 SKU : & armnetwork.PublicIPAddressSKU {
4349 Name : to .Ptr (armnetwork .PublicIPAddressSKUNameStandard ),
@@ -65,13 +71,37 @@ func createPublicIP(ctx context.Context, in *lbInput) (*armnetwork.PublicIPAddre
6571 return & resp .PublicIPAddress , nil
6672}
6773
68- func createExternalLoadBalancer (ctx context.Context , pip * armnetwork.PublicIPAddress , in * lbInput ) (* armnetwork.LoadBalancer , error ) {
74+ func updateExternalLoadBalancer (ctx context.Context , pip * armnetwork.PublicIPAddress , in * lbInput ) (* armnetwork.LoadBalancer , error ) {
6975 loadBalancerName := in .infraID
7076 probeName := "api-probe"
7177 frontEndIPConfigName := "public-lb-ip-v4"
7278 backEndAddressPoolName := in .infraID
7379 idPrefix := fmt .Sprintf ("subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers" , in .subscriptionID , in .resourceGroup )
7480
81+ // Get the CAPI-created outbound load balancer so we can modify it.
82+ extLB , err := in .lbClient .Get (ctx , in .resourceGroup , loadBalancerName , nil )
83+ if err != nil {
84+ return nil , fmt .Errorf ("failed to get external load balancer: %w" , err )
85+ }
86+
87+ // Get the existing frontend configuration and backend address pool and
88+ // create an additional frontend configuration mand backend address
89+ // pool. Use the newly created public IP address with the additional
90+ // configuration so we can setup load balancing rules for the external
91+ // API server.
92+ extLB .Properties .FrontendIPConfigurations = append (extLB .Properties .FrontendIPConfigurations ,
93+ & armnetwork.FrontendIPConfiguration {
94+ Name : & frontEndIPConfigName ,
95+ Properties : & armnetwork.FrontendIPConfigurationPropertiesFormat {
96+ PrivateIPAllocationMethod : to .Ptr (armnetwork .IPAllocationMethodDynamic ),
97+ PublicIPAddress : pip ,
98+ },
99+ })
100+ extLB .Properties .BackendAddressPools = append (extLB .Properties .BackendAddressPools ,
101+ & armnetwork.BackendAddressPool {
102+ Name : & backEndAddressPoolName ,
103+ })
104+
75105 pollerResp , err := in .lbClient .BeginCreateOrUpdate (ctx ,
76106 in .resourceGroup ,
77107 loadBalancerName ,
@@ -82,20 +112,8 @@ func createExternalLoadBalancer(ctx context.Context, pip *armnetwork.PublicIPAdd
82112 Tier : to .Ptr (armnetwork .LoadBalancerSKUTierRegional ),
83113 },
84114 Properties : & armnetwork.LoadBalancerPropertiesFormat {
85- FrontendIPConfigurations : []* armnetwork.FrontendIPConfiguration {
86- {
87- Name : & frontEndIPConfigName ,
88- Properties : & armnetwork.FrontendIPConfigurationPropertiesFormat {
89- PrivateIPAllocationMethod : to .Ptr (armnetwork .IPAllocationMethodDynamic ),
90- PublicIPAddress : pip ,
91- },
92- },
93- },
94- BackendAddressPools : []* armnetwork.BackendAddressPool {
95- {
96- Name : & backEndAddressPoolName ,
97- },
98- },
115+ FrontendIPConfigurations : extLB .Properties .FrontendIPConfigurations ,
116+ BackendAddressPools : extLB .Properties .BackendAddressPools ,
99117 Probes : []* armnetwork.Probe {
100118 {
101119 Name : & probeName ,
@@ -130,12 +148,13 @@ func createExternalLoadBalancer(ctx context.Context, pip *armnetwork.PublicIPAdd
130148 },
131149 },
132150 },
151+ OutboundRules : extLB .Properties .OutboundRules ,
133152 },
134153 Tags : in .tags ,
135154 }, nil )
136155
137156 if err != nil {
138- return nil , fmt .Errorf ("cannot create load balancer: %w" , err )
157+ return nil , fmt .Errorf ("cannot update load balancer: %w" , err )
139158 }
140159
141160 resp , err := pollerResp .PollUntilDone (ctx , nil )
0 commit comments