@@ -179,7 +179,20 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
179179
180180 azEnv := string (installConfig .Azure .CloudName )
181181
182- computeSubnetSpec := capz.SubnetSpec {
182+ // Set default control plane subnets for default installs.
183+ defaultControlPlaneSubnet := capz.Subnets {
184+ {
185+ SubnetClassSpec : capz.SubnetClassSpec {
186+ Name : controlPlaneSubnet ,
187+ Role : capz .SubnetControlPlane ,
188+ CIDRBlocks : []string {
189+ subnets [0 ].String (),
190+ },
191+ },
192+ SecurityGroup : securityGroup ,
193+ },
194+ }
195+ defaultComputeSubnetSpec := capz.SubnetSpec {
183196 ID : nodeSubnetID ,
184197 SubnetClassSpec : capz.SubnetClassSpec {
185198 Name : computeSubnet ,
@@ -191,10 +204,53 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
191204 SecurityGroup : securityGroup ,
192205 }
193206
194- if installConfig .Config .Azure .OutboundType == azure .NATGatewaySingleZoneOutboundType {
195- computeSubnetSpec .NatGateway = capz.NatGateway {
196- NatGatewayClassSpec : capz.NatGatewayClassSpec {Name : fmt .Sprintf ("%s-natgw" , clusterID .InfraID )},
207+ subnetSpec := []capz.SubnetSpec {}
208+ hasControlPlaneSubnet := false
209+ hasComputePlaneSubnet := false
210+
211+ // Add the user specified subnets to the spec.
212+ // For single zone, alter the compute subnet to have a NATGateway and add default control plane subnet
213+ // configuration.
214+ for index , spec := range installConfig .Config .Azure .Subnets {
215+ specGen := capz.SubnetSpec {
216+ ID : "UNKNOWN" ,
217+ SubnetClassSpec : capz.SubnetClassSpec {
218+ Name : spec .Name ,
219+ Role : spec .Role ,
220+ },
221+ SecurityGroup : securityGroup ,
222+ }
223+ // The CIDR information is optional since it could be a byo subnet.
224+ if len (spec .SubnetCIDR ) != 0 {
225+ specGen .CIDRBlocks = spec .SubnetCIDR
197226 }
227+ // If role is compute node and outbound type is single node, add a NAT gateway to the subnet.
228+ // Only adding a NAT gateway to the first subnet.
229+ if ! hasComputePlaneSubnet && spec .Role == capz .SubnetNode && installConfig .Config .Azure .OutboundType == azure .NATGatewaySingleZoneOutboundType {
230+ specGen .NatGateway = capz.NatGateway {
231+ NatGatewayClassSpec : capz.NatGatewayClassSpec {Name : fmt .Sprintf ("%s-natgw" , clusterID .InfraID )},
232+ }
233+ } else if installConfig .Config .Azure .OutboundType == azure .NATGatewayMultiZoneOutboundType {
234+ if spec .NatGatewayName != "" {
235+ specGen .NatGateway = capz.NatGateway {
236+ NatGatewayIP : capz.PublicIPSpec {
237+ Name : fmt .Sprintf ("%s-natgw-public-ip-%d" , clusterID .InfraID , index ),
238+ },
239+ NatGatewayClassSpec : capz.NatGatewayClassSpec {Name : spec .NatGatewayName },
240+ }
241+ }
242+ }
243+ hasControlPlaneSubnet = hasControlPlaneSubnet || spec .Role == capz .SubnetControlPlane
244+ hasComputePlaneSubnet = hasComputePlaneSubnet || spec .Role == capz .SubnetNode
245+ subnetSpec = append (subnetSpec , specGen )
246+ }
247+ // Make sure there's at least one subnet for compute and control plane.
248+ // Ordinary installs will get the default setup.
249+ if ! hasComputePlaneSubnet {
250+ subnetSpec = append (subnetSpec , defaultComputeSubnetSpec )
251+ }
252+ if ! hasControlPlaneSubnet {
253+ subnetSpec = append (subnetSpec , defaultControlPlaneSubnet ... )
198254 }
199255
200256 azureCluster := & capz.AzureCluster {
@@ -236,19 +292,7 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
236292 },
237293 APIServerLB : & apiServerLB ,
238294 ControlPlaneOutboundLB : controlPlaneOutboundLB ,
239- Subnets : capz.Subnets {
240- {
241- SubnetClassSpec : capz.SubnetClassSpec {
242- Name : controlPlaneSubnet ,
243- Role : capz .SubnetControlPlane ,
244- CIDRBlocks : []string {
245- subnets [0 ].String (),
246- },
247- },
248- SecurityGroup : securityGroup ,
249- },
250- computeSubnetSpec ,
251- },
295+ Subnets : subnetSpec ,
252296 },
253297 },
254298 }
0 commit comments