@@ -12,12 +12,16 @@ import (
1212)
1313
1414type lbInput struct {
15- infraID string
16- region string
17- resourceGroup string
18- subscriptionID string
19- lbClient * armnetwork.LoadBalancersClient
20- tags map [string ]* string
15+ loadBalancerName string
16+ infraID string
17+ region string
18+ resourceGroup string
19+ subscriptionID string
20+ frontendIPConfigName string
21+ backendAddressPoolName string
22+ idPrefix string
23+ lbClient * armnetwork.LoadBalancersClient
24+ tags map [string ]* string
2125}
2226
2327type pipInput struct {
@@ -92,15 +96,87 @@ func createPublicIP(ctx context.Context, in *pipInput) (*armnetwork.PublicIPAddr
9296 return & resp .PublicIPAddress , nil
9397}
9498
99+ func createAPILoadBalancer (ctx context.Context , pip * armnetwork.PublicIPAddress , in * lbInput ) (* armnetwork.LoadBalancer , error ) {
100+ probeName := "api-probe"
101+
102+ pollerResp , err := in .lbClient .BeginCreateOrUpdate (ctx ,
103+ in .resourceGroup ,
104+ in .loadBalancerName ,
105+ armnetwork.LoadBalancer {
106+ Location : to .Ptr (in .region ),
107+ SKU : & armnetwork.LoadBalancerSKU {
108+ Name : to .Ptr (armnetwork .LoadBalancerSKUNameStandard ),
109+ Tier : to .Ptr (armnetwork .LoadBalancerSKUTierRegional ),
110+ },
111+ Properties : & armnetwork.LoadBalancerPropertiesFormat {
112+ FrontendIPConfigurations : []* armnetwork.FrontendIPConfiguration {
113+ {
114+ Name : & in .frontendIPConfigName ,
115+ Properties : & armnetwork.FrontendIPConfigurationPropertiesFormat {
116+ PrivateIPAllocationMethod : to .Ptr (armnetwork .IPAllocationMethodDynamic ),
117+ PublicIPAddress : pip ,
118+ },
119+ },
120+ },
121+ BackendAddressPools : []* armnetwork.BackendAddressPool {
122+ {
123+ Name : & in .backendAddressPoolName ,
124+ },
125+ },
126+ Probes : []* armnetwork.Probe {
127+ {
128+ Name : & probeName ,
129+ Properties : & armnetwork.ProbePropertiesFormat {
130+ Protocol : to .Ptr (armnetwork .ProbeProtocolHTTPS ),
131+ Port : to.Ptr [int32 ](6443 ),
132+ IntervalInSeconds : to.Ptr [int32 ](5 ),
133+ NumberOfProbes : to.Ptr [int32 ](2 ),
134+ RequestPath : to .Ptr ("/readyz" ),
135+ },
136+ },
137+ },
138+ LoadBalancingRules : []* armnetwork.LoadBalancingRule {
139+ {
140+ Name : to .Ptr ("api-v4" ),
141+ Properties : & armnetwork.LoadBalancingRulePropertiesFormat {
142+ Protocol : to .Ptr (armnetwork .TransportProtocolTCP ),
143+ FrontendPort : to.Ptr [int32 ](6443 ),
144+ BackendPort : to.Ptr [int32 ](6443 ),
145+ IdleTimeoutInMinutes : to.Ptr [int32 ](30 ),
146+ EnableFloatingIP : to .Ptr (false ),
147+ LoadDistribution : to .Ptr (armnetwork .LoadDistributionDefault ),
148+ FrontendIPConfiguration : & armnetwork.SubResource {
149+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/frontendIPConfigurations/%s" , in .idPrefix , in .loadBalancerName , in .frontendIPConfigName )),
150+ },
151+ BackendAddressPool : & armnetwork.SubResource {
152+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/backendAddressPools/%s" , in .idPrefix , in .loadBalancerName , in .backendAddressPoolName )),
153+ },
154+ Probe : & armnetwork.SubResource {
155+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/probes/%s" , in .idPrefix , in .loadBalancerName , probeName )),
156+ },
157+ },
158+ },
159+ },
160+ },
161+ Tags : in .tags ,
162+ }, nil )
163+
164+ if err != nil {
165+ return nil , fmt .Errorf ("cannot create load balancer: %w" , err )
166+ }
167+
168+ resp , err := pollerResp .PollUntilDone (ctx , nil )
169+ if err != nil {
170+ return nil , err
171+ }
172+ return & resp .LoadBalancer , nil
173+ }
174+
95175func updateOutboundLoadBalancerToAPILoadBalancer (ctx context.Context , pip * armnetwork.PublicIPAddress , in * lbInput ) (* armnetwork.LoadBalancer , error ) {
96- loadBalancerName := in .infraID
97176 probeName := "api-probe"
98- frontEndIPConfigName := "public-lb-ip-v4"
99- backEndAddressPoolName := in .infraID
100- idPrefix := fmt .Sprintf ("subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers" , in .subscriptionID , in .resourceGroup )
101177
102178 // Get the CAPI-created outbound load balancer so we can modify it.
103- extLB , err := in .lbClient .Get (ctx , in .resourceGroup , loadBalancerName , nil )
179+ extLB , err := in .lbClient .Get (ctx , in .resourceGroup , in . loadBalancerName , nil )
104180 if err != nil {
105181 return nil , fmt .Errorf ("failed to get external load balancer: %w" , err )
106182 }
@@ -112,20 +188,20 @@ func updateOutboundLoadBalancerToAPILoadBalancer(ctx context.Context, pip *armne
112188 // API server.
113189 extLB .Properties .FrontendIPConfigurations = append (extLB .Properties .FrontendIPConfigurations ,
114190 & armnetwork.FrontendIPConfiguration {
115- Name : & frontEndIPConfigName ,
191+ Name : & in . frontendIPConfigName ,
116192 Properties : & armnetwork.FrontendIPConfigurationPropertiesFormat {
117193 PrivateIPAllocationMethod : to .Ptr (armnetwork .IPAllocationMethodDynamic ),
118194 PublicIPAddress : pip ,
119195 },
120196 })
121197 extLB .Properties .BackendAddressPools = append (extLB .Properties .BackendAddressPools ,
122198 & armnetwork.BackendAddressPool {
123- Name : & backEndAddressPoolName ,
199+ Name : & in . backendAddressPoolName ,
124200 })
125201
126202 pollerResp , err := in .lbClient .BeginCreateOrUpdate (ctx ,
127203 in .resourceGroup ,
128- loadBalancerName ,
204+ in . loadBalancerName ,
129205 armnetwork.LoadBalancer {
130206 Location : to .Ptr (in .region ),
131207 SKU : & armnetwork.LoadBalancerSKU {
@@ -158,13 +234,13 @@ func updateOutboundLoadBalancerToAPILoadBalancer(ctx context.Context, pip *armne
158234 EnableFloatingIP : to .Ptr (false ),
159235 LoadDistribution : to .Ptr (armnetwork .LoadDistributionDefault ),
160236 FrontendIPConfiguration : & armnetwork.SubResource {
161- ID : to .Ptr (fmt .Sprintf ("/%s/%s/frontendIPConfigurations/%s" , idPrefix , loadBalancerName , frontEndIPConfigName )),
237+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/frontendIPConfigurations/%s" , in . idPrefix , in . loadBalancerName , in . frontendIPConfigName )),
162238 },
163239 BackendAddressPool : & armnetwork.SubResource {
164- ID : to .Ptr (fmt .Sprintf ("/%s/%s/backendAddressPools/%s" , idPrefix , loadBalancerName , backEndAddressPoolName )),
240+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/backendAddressPools/%s" , in . idPrefix , in . loadBalancerName , in . backendAddressPoolName )),
165241 },
166242 Probe : & armnetwork.SubResource {
167- ID : to .Ptr (fmt .Sprintf ("/%s/%s/probes/%s" , idPrefix , loadBalancerName , probeName )),
243+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/probes/%s" , in . idPrefix , in . loadBalancerName , probeName )),
168244 },
169245 },
170246 },
@@ -186,13 +262,10 @@ func updateOutboundLoadBalancerToAPILoadBalancer(ctx context.Context, pip *armne
186262}
187263
188264func updateInternalLoadBalancer (ctx context.Context , in * lbInput ) (* armnetwork.LoadBalancer , error ) {
189- loadBalancerName := fmt .Sprintf ("%s-internal" , in .infraID )
190265 mcsProbeName := "sint-probe"
191- backEndAddressPoolName := fmt .Sprintf ("%s-internal" , in .infraID )
192- idPrefix := fmt .Sprintf ("subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers" , in .subscriptionID , in .resourceGroup )
193266
194267 // Get the CAPI-created internal load balancer so we can modify it.
195- lbResp , err := in .lbClient .Get (ctx , in .resourceGroup , loadBalancerName , nil )
268+ lbResp , err := in .lbClient .Get (ctx , in .resourceGroup , in . loadBalancerName , nil )
196269 if err != nil {
197270 return nil , fmt .Errorf ("could not get internal load balancer: %w" , err )
198271 }
@@ -225,13 +298,13 @@ func updateInternalLoadBalancer(ctx context.Context, in *lbInput) (*armnetwork.L
225298 EnableFloatingIP : to .Ptr (false ),
226299 LoadDistribution : to .Ptr (armnetwork .LoadDistributionDefault ),
227300 FrontendIPConfiguration : & armnetwork.SubResource {
228- ID : to .Ptr (fmt .Sprintf ("/%s/%s/frontendIPConfigurations/%s" , idPrefix , loadBalancerName , existingFrontEndIPConfigName )),
301+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/frontendIPConfigurations/%s" , in . idPrefix , in . loadBalancerName , existingFrontEndIPConfigName )),
229302 },
230303 BackendAddressPool : & armnetwork.SubResource {
231- ID : to .Ptr (fmt .Sprintf ("/%s/%s/backendAddressPools/%s" , idPrefix , loadBalancerName , backEndAddressPoolName )),
304+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/backendAddressPools/%s" , in . idPrefix , in . loadBalancerName , in . backendAddressPoolName )),
232305 },
233306 Probe : & armnetwork.SubResource {
234- ID : to .Ptr (fmt .Sprintf ("/%s/%s/probes/%s" , idPrefix , loadBalancerName , mcsProbeName )),
307+ ID : to .Ptr (fmt .Sprintf ("/%s/%s/probes/%s" , in . idPrefix , in . loadBalancerName , mcsProbeName )),
235308 },
236309 },
237310 }
@@ -240,7 +313,7 @@ func updateInternalLoadBalancer(ctx context.Context, in *lbInput) (*armnetwork.L
240313 intLB .Properties .LoadBalancingRules = append (intLB .Properties .LoadBalancingRules , mcsRule )
241314 pollerResp , err := in .lbClient .BeginCreateOrUpdate (ctx ,
242315 in .resourceGroup ,
243- loadBalancerName ,
316+ in . loadBalancerName ,
244317 intLB ,
245318 nil )
246319 if err != nil {
0 commit comments