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"
@@ -30,18 +31,22 @@ import (
3031// GenerateClusterAssets generates the manifests for the cluster-api.
3132func GenerateClusterAssets (installConfig * installconfig.InstallConfig , clusterID * installconfig.ClusterID ) (* capiutils.GenerateClusterAssetsOutput , error ) {
3233 manifests := []* asset.RuntimeFile {}
33- mainCIDR := capiutils .CIDRFromInstallConfig (installConfig )
34+ mainCIDR := capiutils .CIDRFromInstallConfig (installConfig ). String ()
3435
3536 session , err := installConfig .Azure .Session ()
3637 if err != nil {
3738 return nil , errors .Wrap (err , "failed to create Azure session" )
3839 }
3940
40- subnets , err := cidr .SplitIntoSubnetsIPv4 (mainCIDR . String () , 2 )
41+ subnets , err := cidr .SplitIntoSubnetsIPv4 (mainCIDR , 2 )
4142 if err != nil {
4243 return nil , errors .Wrap (err , "failed to split CIDR into subnets" )
4344 }
4445
46+ virtualNetworkAddressPrefixes := []string {mainCIDR }
47+ controlPlaneAddressPrefixes := []string {subnets [0 ].String ()}
48+ computeAddressPrefixes := []string {subnets [1 ].String ()}
49+
4550 // CAPZ expects the capz-system to be created.
4651 azureNamespace := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "capz-system" }}
4752 azureNamespace .SetGroupVersionKind (corev1 .SchemeGroupVersion .WithKind ("Namespace" ))
@@ -57,7 +62,7 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
5762
5863 source := "*"
5964 if installConfig .Config .Publish == types .InternalPublishingStrategy {
60- source = mainCIDR . String ()
65+ source = mainCIDR
6166 }
6267
6368 securityGroup := capz.SecurityGroup {
@@ -117,37 +122,29 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
117122 lbip := capz .DefaultInternalLBIPAddress
118123 lbip = getIPWithinCIDR (subnets , lbip )
119124
120- if controlPlaneSub := installConfig .Config .Azure .ControlPlaneSubnet ; controlPlaneSub != "" {
121- client , err := installConfig . Azure . Client ( )
125+ if controlPlaneSubnetName := installConfig .Config .Azure .ControlPlaneSubnet ; controlPlaneSubnetName != "" {
126+ controlPlaneSubnet , err := getSubnet ( installConfig , clusterID , "controlPlane" , controlPlaneSubnetName )
122127 if err != nil {
123- return nil , fmt .Errorf ("failed to get azure client : %w" , err )
128+ return nil , fmt .Errorf ("failed to get control plane subnet : %w" , err )
124129 }
125- ctx := context .TODO ()
126- controlPlaneSubnet , err := client .GetControlPlaneSubnet (ctx , installConfig .Config .Azure .NetworkResourceGroupName , installConfig .Config .Azure .VirtualNetwork , controlPlaneSub )
127- if err != nil || controlPlaneSubnet == nil {
128- return nil , fmt .Errorf ("failed to get azure control plane subnet: %w" , err )
129- } else if controlPlaneSubnet .AddressPrefixes == nil && controlPlaneSubnet .AddressPrefix == nil {
130- return nil , fmt .Errorf ("failed to get azure control plane subnet addresses: %w" , err )
131- }
132- subnetList := []* net.IPNet {}
133- if controlPlaneSubnet .AddressPrefixes != nil {
134- for _ , sub := range * controlPlaneSubnet .AddressPrefixes {
135- _ , ipnet , err := net .ParseCIDR (sub )
136- if err != nil {
137- return nil , fmt .Errorf ("failed to get translate azure control plane subnet addresses: %w" , err )
138- }
139- subnetList = append (subnetList , ipnet )
140- }
130+ subnetList , err := getSubnetAddressPrefixes (controlPlaneSubnet )
131+ if err != nil {
132+ return nil , fmt .Errorf ("failed to get control plane subnet address prefixes: %w" , err )
141133 }
134+ controlPlaneAddressPrefixes = stringifyAddressPrefixes (subnetList )
135+ lbip = getIPWithinCIDR (subnetList , lbip )
136+ }
142137
143- if controlPlaneSubnet .AddressPrefix != nil {
144- _ , ipnet , err := net .ParseCIDR (* controlPlaneSubnet .AddressPrefix )
145- if err != nil {
146- return nil , fmt .Errorf ("failed to get translate azure control plane subnet address prefix: %w" , err )
147- }
148- subnetList = append (subnetList , ipnet )
138+ if computeSubnetName := installConfig .Config .Azure .ComputeSubnet ; computeSubnetName != "" {
139+ computeSubnet , err := getSubnet (installConfig , clusterID , "compute" , computeSubnetName )
140+ if err != nil {
141+ return nil , fmt .Errorf ("failed to get compute subnet: %w" , err )
149142 }
150- lbip = getIPWithinCIDR (subnetList , lbip )
143+ subnetList , err := getSubnetAddressPrefixes (computeSubnet )
144+ if err != nil {
145+ return nil , fmt .Errorf ("failed to get compute subnet address prefixes: %w" , err )
146+ }
147+ computeAddressPrefixes = stringifyAddressPrefixes (subnetList )
151148 }
152149
153150 apiServerLB .FrontendIPs = []capz.FrontendIP {{
@@ -157,6 +154,8 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
157154 },
158155 }}
159156 if installConfig .Config .Azure .VirtualNetwork != "" {
157+ virtualNetworkAddressPrefixes = make ([]string , 0 )
158+
160159 client , err := installConfig .Azure .Client ()
161160 if err != nil {
162161 return nil , fmt .Errorf ("failed to get azure client: %w" , err )
@@ -176,18 +175,19 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
176175 apiServerLB .FrontendIPs [0 ].FrontendIPClass = capz.FrontendIPClass {
177176 PrivateIPAddress : lbip ,
178177 }
178+ if virtualNetwork .AddressSpace != nil && virtualNetwork .AddressSpace .AddressPrefixes != nil {
179+ virtualNetworkAddressPrefixes = append (virtualNetworkAddressPrefixes , * virtualNetwork .AddressSpace .AddressPrefixes ... )
180+ }
179181 }
180182
181183 azEnv := string (installConfig .Azure .CloudName )
182184
183185 computeSubnetSpec := capz.SubnetSpec {
184186 ID : nodeSubnetID ,
185187 SubnetClassSpec : capz.SubnetClassSpec {
186- Name : computeSubnet ,
187- Role : capz .SubnetNode ,
188- CIDRBlocks : []string {
189- subnets [1 ].String (),
190- },
188+ Name : computeSubnet ,
189+ Role : capz .SubnetNode ,
190+ CIDRBlocks : computeAddressPrefixes ,
191191 },
192192 SecurityGroup : securityGroup ,
193193 }
@@ -238,21 +238,17 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
238238 // https://github.com/kubernetes-sigs/cluster-api-provider-azure/commit/0f321e4089a3f4dc37f8420bf2ef6762c398c400
239239 ID : virtualNetworkID ,
240240 VnetClassSpec : capz.VnetClassSpec {
241- CIDRBlocks : []string {
242- mainCIDR .String (),
243- },
241+ CIDRBlocks : virtualNetworkAddressPrefixes ,
244242 },
245243 },
246244 APIServerLB : & apiServerLB ,
247245 ControlPlaneOutboundLB : controlPlaneOutboundLB ,
248246 Subnets : capz.Subnets {
249247 {
250248 SubnetClassSpec : capz.SubnetClassSpec {
251- Name : controlPlaneSubnet ,
252- Role : capz .SubnetControlPlane ,
253- CIDRBlocks : []string {
254- subnets [0 ].String (),
255- },
249+ Name : controlPlaneSubnet ,
250+ Role : capz .SubnetControlPlane ,
251+ CIDRBlocks : controlPlaneAddressPrefixes ,
256252 },
257253 SecurityGroup : securityGroup ,
258254 },
@@ -345,6 +341,71 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
345341 }, nil
346342}
347343
344+ func getSubnet (installConfig * installconfig.InstallConfig , clusterID * installconfig.ClusterID , subnetType , subnetName string ) (* aznetwork.Subnet , error ) {
345+ var subnet * aznetwork.Subnet
346+
347+ azClient , err := installConfig .Azure .Client ()
348+ if err != nil {
349+ return nil , fmt .Errorf ("failed to get azure client: %w" , err )
350+ }
351+ ctx := context .TODO ()
352+
353+ if subnetType == "controlPlane" {
354+ subnet , err = azClient .GetControlPlaneSubnet (ctx ,
355+ installConfig .Config .Azure .NetworkResourceGroupName ,
356+ installConfig .Config .Azure .VirtualNetwork ,
357+ subnetName ,
358+ )
359+ } else if subnetType == "compute" {
360+ subnet , err = azClient .GetComputeSubnet (ctx ,
361+ installConfig .Config .Azure .NetworkResourceGroupName ,
362+ installConfig .Config .Azure .VirtualNetwork ,
363+ subnetName ,
364+ )
365+ }
366+
367+ if err != nil {
368+ return nil , fmt .Errorf ("failed to get subnet: %w" , err )
369+ }
370+ if subnet == nil {
371+ return nil , fmt .Errorf ("failed to get subnet" )
372+ }
373+ if subnet .AddressPrefixes == nil && subnet .AddressPrefix == nil {
374+ return nil , fmt .Errorf ("failed to get subnet addresses: %w" , err )
375+ }
376+ return subnet , nil
377+ }
378+
379+ func getSubnetAddressPrefixes (subnet * aznetwork.Subnet ) ([]* net.IPNet , error ) {
380+ subnetList := []* net.IPNet {}
381+ if subnet .AddressPrefixes != nil {
382+ for _ , sub := range * subnet .AddressPrefixes {
383+ _ , ipnet , err := net .ParseCIDR (sub )
384+ if err != nil {
385+ return subnetList , fmt .Errorf ("failed to get translate azure subnet addresses: %w" , err )
386+ }
387+ subnetList = append (subnetList , ipnet )
388+ }
389+ }
390+ if subnet .AddressPrefix != nil {
391+ _ , ipnet , err := net .ParseCIDR (* subnet .AddressPrefix )
392+ if err != nil {
393+ return subnetList , fmt .Errorf ("failed to get translate azure subnet address prefix: %w" , err )
394+ }
395+ subnetList = append (subnetList , ipnet )
396+ }
397+
398+ return subnetList , nil
399+ }
400+
401+ func stringifyAddressPrefixes (addressPrefixes []* net.IPNet ) []string {
402+ strAddressPrefixes := []string {}
403+ for _ , addressPrefix := range addressPrefixes {
404+ strAddressPrefixes = append (strAddressPrefixes , addressPrefix .String ())
405+ }
406+ return strAddressPrefixes
407+ }
408+
348409func getIPWithinCIDR (subnets []* net.IPNet , ip string ) string {
349410 if subnets == nil || ip == "" {
350411 return ""
0 commit comments