77 "net"
88 "strings"
99
10+ aznetwork "github.com/Azure/azure-sdk-for-go/profiles/2020-09-01/network/mgmt/network"
1011 "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1112 "github.com/pkg/errors"
1213 corev1 "k8s.io/api/core/v1"
@@ -29,18 +30,22 @@ import (
2930// GenerateClusterAssets generates the manifests for the cluster-api.
3031func GenerateClusterAssets (installConfig * installconfig.InstallConfig , clusterID * installconfig.ClusterID ) (* capiutils.GenerateClusterAssetsOutput , error ) {
3132 manifests := []* asset.RuntimeFile {}
32- mainCIDR := capiutils .CIDRFromInstallConfig (installConfig )
33+ mainCIDR := capiutils .CIDRFromInstallConfig (installConfig ). String ()
3334
3435 session , err := installConfig .Azure .Session ()
3536 if err != nil {
3637 return nil , errors .Wrap (err , "failed to create Azure session" )
3738 }
3839
39- subnets , err := cidr .SplitIntoSubnetsIPv4 (mainCIDR . String () , 2 )
40+ subnets , err := cidr .SplitIntoSubnetsIPv4 (mainCIDR , 2 )
4041 if err != nil {
4142 return nil , errors .Wrap (err , "failed to split CIDR into subnets" )
4243 }
4344
45+ virtualNetworkAddressPrefixes := []string {mainCIDR }
46+ controlPlaneAddressPrefixes := []string {subnets [0 ].String ()}
47+ computeAddressPrefixes := []string {subnets [1 ].String ()}
48+
4449 // CAPZ expects the capz-system to be created.
4550 azureNamespace := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "capz-system" }}
4651 azureNamespace .SetGroupVersionKind (corev1 .SchemeGroupVersion .WithKind ("Namespace" ))
@@ -56,7 +61,7 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
5661
5762 source := "*"
5863 if installConfig .Config .Publish == types .InternalPublishingStrategy {
59- source = mainCIDR . String ()
64+ source = mainCIDR
6065 }
6166
6267 securityGroup := capz.SecurityGroup {
@@ -116,37 +121,31 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
116121 lbip := capz .DefaultInternalLBIPAddress
117122 lbip = getIPWithinCIDR (subnets , lbip )
118123
119- if controlPlaneSub := installConfig .Config .Azure .ControlPlaneSubnet ; controlPlaneSub != "" {
120- client , err := installConfig .Azure .Client ()
124+ if controlPlaneSubnetName := installConfig .Config .Azure .ControlPlaneSubnet ; controlPlaneSubnetName != "" {
125+ controlPlaneAddressPrefixes = []string {}
126+ controlPlaneSubnet , err := getSubnet (installConfig , clusterID , "controlPlane" , controlPlaneSubnetName )
121127 if err != nil {
122- return nil , fmt .Errorf ("failed to get azure client : %w" , err )
128+ return nil , fmt .Errorf ("failed to get control plane subnet : %w" , err )
123129 }
124- ctx := context .TODO ()
125- controlPlaneSubnet , err := client .GetControlPlaneSubnet (ctx , installConfig .Config .Azure .NetworkResourceGroupName , installConfig .Config .Azure .VirtualNetwork , controlPlaneSub )
126- if err != nil || controlPlaneSubnet == nil {
127- return nil , fmt .Errorf ("failed to get azure control plane subnet: %w" , err )
128- } else if controlPlaneSubnet .AddressPrefixes == nil && controlPlaneSubnet .AddressPrefix == nil {
129- return nil , fmt .Errorf ("failed to get azure control plane subnet addresses: %w" , err )
130- }
131- subnetList := []* net.IPNet {}
132- if controlPlaneSubnet .AddressPrefixes != nil {
133- for _ , sub := range * controlPlaneSubnet .AddressPrefixes {
134- _ , ipnet , err := net .ParseCIDR (sub )
135- if err != nil {
136- return nil , fmt .Errorf ("failed to get translate azure control plane subnet addresses: %w" , err )
137- }
138- subnetList = append (subnetList , ipnet )
139- }
130+ subnetList , err := getSubnetAddressPrefixes (controlPlaneSubnet )
131+ if err != nil {
132+ return nil , fmt .Errorf ("failed to get control plane subnet address prefixes: %w" , err )
140133 }
134+ controlPlaneAddressPrefixes = stringifyAddressPrefixes (subnetList )
135+ lbip = getIPWithinCIDR (subnetList , lbip )
136+ }
141137
142- if controlPlaneSubnet .AddressPrefix != nil {
143- _ , ipnet , err := net .ParseCIDR (* controlPlaneSubnet .AddressPrefix )
144- if err != nil {
145- return nil , fmt .Errorf ("failed to get translate azure control plane subnet address prefix: %w" , err )
146- }
147- subnetList = append (subnetList , ipnet )
138+ if computeSubnetName := installConfig .Config .Azure .ComputeSubnet ; computeSubnetName != "" {
139+ computeAddressPrefixes = []string {}
140+ computeSubnet , err := getSubnet (installConfig , clusterID , "compute" , computeSubnetName )
141+ if err != nil {
142+ return nil , fmt .Errorf ("failed to get compute subnet: %w" , err )
148143 }
149- lbip = getIPWithinCIDR (subnetList , lbip )
144+ subnetList , err := getSubnetAddressPrefixes (computeSubnet )
145+ if err != nil {
146+ return nil , fmt .Errorf ("failed to get compute subnet address prefixes: %w" , err )
147+ }
148+ computeAddressPrefixes = stringifyAddressPrefixes (subnetList )
150149 }
151150
152151 apiServerLB .FrontendIPs = []capz.FrontendIP {{
@@ -156,6 +155,8 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
156155 },
157156 }}
158157 if installConfig .Config .Azure .VirtualNetwork != "" {
158+ virtualNetworkAddressPrefixes := []string {}
159+
159160 client , err := installConfig .Azure .Client ()
160161 if err != nil {
161162 return nil , fmt .Errorf ("failed to get azure client: %w" , err )
@@ -175,18 +176,21 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
175176 apiServerLB .FrontendIPs [0 ].FrontendIPClass = capz.FrontendIPClass {
176177 PrivateIPAddress : lbip ,
177178 }
179+ if virtualNetwork .AddressSpace != nil && virtualNetwork .AddressSpace .AddressPrefixes != nil {
180+ for _ , addressPrefix := range * virtualNetwork .AddressSpace .AddressPrefixes {
181+ virtualNetworkAddressPrefixes = append (virtualNetworkAddressPrefixes , addressPrefix )
182+ }
183+ }
178184 }
179185
180186 azEnv := string (installConfig .Azure .CloudName )
181187
182188 computeSubnetSpec := capz.SubnetSpec {
183189 ID : nodeSubnetID ,
184190 SubnetClassSpec : capz.SubnetClassSpec {
185- Name : computeSubnet ,
186- Role : capz .SubnetNode ,
187- CIDRBlocks : []string {
188- subnets [1 ].String (),
189- },
191+ Name : computeSubnet ,
192+ Role : capz .SubnetNode ,
193+ CIDRBlocks : computeAddressPrefixes ,
190194 },
191195 SecurityGroup : securityGroup ,
192196 }
@@ -229,21 +233,17 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
229233 // https://github.com/kubernetes-sigs/cluster-api-provider-azure/commit/0f321e4089a3f4dc37f8420bf2ef6762c398c400
230234 ID : virtualNetworkID ,
231235 VnetClassSpec : capz.VnetClassSpec {
232- CIDRBlocks : []string {
233- mainCIDR .String (),
234- },
236+ CIDRBlocks : virtualNetworkAddressPrefixes ,
235237 },
236238 },
237239 APIServerLB : & apiServerLB ,
238240 ControlPlaneOutboundLB : controlPlaneOutboundLB ,
239241 Subnets : capz.Subnets {
240242 {
241243 SubnetClassSpec : capz.SubnetClassSpec {
242- Name : controlPlaneSubnet ,
243- Role : capz .SubnetControlPlane ,
244- CIDRBlocks : []string {
245- subnets [0 ].String (),
246- },
244+ Name : controlPlaneSubnet ,
245+ Role : capz .SubnetControlPlane ,
246+ CIDRBlocks : controlPlaneAddressPrefixes ,
247247 },
248248 SecurityGroup : securityGroup ,
249249 },
@@ -335,6 +335,71 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
335335 }, nil
336336}
337337
338+ func getSubnet (installConfig * installconfig.InstallConfig , clusterID * installconfig.ClusterID , subnetType , subnetName string ) (* aznetwork.Subnet , error ) {
339+ var subnet * aznetwork.Subnet = nil
340+
341+ azClient , err := installConfig .Azure .Client ()
342+ if err != nil {
343+ return nil , fmt .Errorf ("failed to get azure client: %w" , err )
344+ }
345+ ctx := context .TODO ()
346+
347+ if subnetType == "controlPlane" {
348+ subnet , err = azClient .GetControlPlaneSubnet (ctx ,
349+ installConfig .Config .Azure .NetworkResourceGroupName ,
350+ installConfig .Config .Azure .VirtualNetwork ,
351+ subnetName ,
352+ )
353+ } else if subnetType == "compute" {
354+ subnet , err = azClient .GetComputeSubnet (ctx ,
355+ installConfig .Config .Azure .NetworkResourceGroupName ,
356+ installConfig .Config .Azure .VirtualNetwork ,
357+ subnetName ,
358+ )
359+ }
360+
361+ if err != nil {
362+ return nil , fmt .Errorf ("failed to get subnet: %w" , err )
363+ }
364+ if subnet == nil {
365+ return nil , fmt .Errorf ("failed to get subnet" )
366+ }
367+ if subnet .AddressPrefixes == nil && subnet .AddressPrefix == nil {
368+ return nil , fmt .Errorf ("failed to get subnet addresses: %w" , err )
369+ }
370+ return subnet , nil
371+ }
372+
373+ func getSubnetAddressPrefixes (subnet * aznetwork.Subnet ) ([]* net.IPNet , error ) {
374+ subnetList := []* net.IPNet {}
375+ if subnet .AddressPrefixes != nil {
376+ for _ , sub := range * subnet .AddressPrefixes {
377+ _ , ipnet , err := net .ParseCIDR (sub )
378+ if err != nil {
379+ return subnetList , fmt .Errorf ("failed to get translate azure subnet addresses: %w" , err )
380+ }
381+ subnetList = append (subnetList , ipnet )
382+ }
383+ }
384+ if subnet .AddressPrefix != nil {
385+ _ , ipnet , err := net .ParseCIDR (* subnet .AddressPrefix )
386+ if err != nil {
387+ return subnetList , fmt .Errorf ("failed to get translate azure subnet address prefix: %w" , err )
388+ }
389+ subnetList = append (subnetList , ipnet )
390+ }
391+
392+ return subnetList , nil
393+ }
394+
395+ func stringifyAddressPrefixes (addressPrefixes []* net.IPNet ) []string {
396+ strAddressPrefixes := []string {}
397+ for _ , addressPrefix := range addressPrefixes {
398+ strAddressPrefixes = append (strAddressPrefixes , addressPrefix .String ())
399+ }
400+ return strAddressPrefixes
401+ }
402+
338403func getIPWithinCIDR (subnets []* net.IPNet , ip string ) string {
339404 if subnets == nil || ip == "" {
340405 return ""
0 commit comments