Skip to content

Commit 63e4167

Browse files
committed
pkg/asset/manifests/azure: save cidr blocks
When using a pre-existing network, save CIDR blocks for the virtual network and subnets https://issues.redhat.com/browse/OCPBUGS-59105
1 parent d7dc751 commit 63e4167

File tree

1 file changed

+113
-42
lines changed

1 file changed

+113
-42
lines changed

pkg/asset/manifests/azure/cluster.go

Lines changed: 113 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
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"
13+
"github.com/sirupsen/logrus"
1214
corev1 "k8s.io/api/core/v1"
1315
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416
"k8s.io/utils/ptr"
@@ -29,18 +31,22 @@ import (
2931
// GenerateClusterAssets generates the manifests for the cluster-api.
3032
func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID *installconfig.ClusterID) (*capiutils.GenerateClusterAssetsOutput, error) {
3133
manifests := []*asset.RuntimeFile{}
32-
mainCIDR := capiutils.CIDRFromInstallConfig(installConfig)
34+
mainCIDR := capiutils.CIDRFromInstallConfig(installConfig).String()
3335

3436
session, err := installConfig.Azure.Session()
3537
if err != nil {
3638
return nil, errors.Wrap(err, "failed to create Azure session")
3739
}
3840

39-
subnets, err := cidr.SplitIntoSubnetsIPv4(mainCIDR.String(), 2)
41+
subnets, err := cidr.SplitIntoSubnetsIPv4(mainCIDR, 2)
4042
if err != nil {
4143
return nil, errors.Wrap(err, "failed to split CIDR into subnets")
4244
}
4345

46+
virtualNetworkAddressPrefixes := []string{mainCIDR}
47+
controlPlaneAddressPrefixes := []string{subnets[0].String()}
48+
computeAddressPrefixes := []string{subnets[1].String()}
49+
4450
// CAPZ expects the capz-system to be created.
4551
azureNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "capz-system"}}
4652
azureNamespace.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Namespace"))
@@ -56,7 +62,7 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
5662

5763
source := "*"
5864
if installConfig.Config.Publish == types.InternalPublishingStrategy {
59-
source = mainCIDR.String()
65+
source = mainCIDR
6066
}
6167

6268
securityGroup := capz.SecurityGroup{
@@ -116,37 +122,31 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
116122
lbip := capz.DefaultInternalLBIPAddress
117123
lbip = getIPWithinCIDR(subnets, lbip)
118124

119-
if controlPlaneSub := installConfig.Config.Azure.ControlPlaneSubnet; controlPlaneSub != "" {
120-
client, err := installConfig.Azure.Client()
125+
if controlPlaneSubnetName := installConfig.Config.Azure.ControlPlaneSubnet; controlPlaneSubnetName != "" {
126+
controlPlaneAddressPrefixes = []string{}
127+
controlPlaneSubnet, err := getSubnet(installConfig, clusterID, "controlPlane", controlPlaneSubnetName)
121128
if err != nil {
122-
return nil, fmt.Errorf("failed to get azure client: %w", err)
129+
return nil, fmt.Errorf("failed to get control plane subnet: %w", err)
123130
}
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-
}
131+
subnetList, err := getSubnetAddressPrefixes(controlPlaneSubnet)
132+
if err != nil {
133+
return nil, fmt.Errorf("failed to get control plane subnet address prefixes: %w", err)
140134
}
135+
controlPlaneAddressPrefixes = stringifyAddressPrefixes(subnetList)
136+
lbip = getIPWithinCIDR(subnetList, lbip)
137+
}
141138

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)
139+
if computeSubnetName := installConfig.Config.Azure.ComputeSubnet; computeSubnetName != "" {
140+
computeAddressPrefixes = []string{}
141+
computeSubnet, err := getSubnet(installConfig, clusterID, "compute", computeSubnetName)
142+
if err != nil {
143+
return nil, fmt.Errorf("failed to get compute subnet: %w", err)
148144
}
149-
lbip = getIPWithinCIDR(subnetList, lbip)
145+
subnetList, err := getSubnetAddressPrefixes(computeSubnet)
146+
if err != nil {
147+
return nil, fmt.Errorf("failed to get compute subnet address prefixes: %w", err)
148+
}
149+
computeAddressPrefixes = stringifyAddressPrefixes(subnetList)
150150
}
151151

152152
apiServerLB.FrontendIPs = []capz.FrontendIP{{
@@ -156,6 +156,8 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
156156
},
157157
}}
158158
if installConfig.Config.Azure.VirtualNetwork != "" {
159+
virtualNetworkAddressPrefixes := []string{}
160+
159161
client, err := installConfig.Azure.Client()
160162
if err != nil {
161163
return nil, fmt.Errorf("failed to get azure client: %w", err)
@@ -175,18 +177,21 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
175177
apiServerLB.FrontendIPs[0].FrontendIPClass = capz.FrontendIPClass{
176178
PrivateIPAddress: lbip,
177179
}
180+
if virtualNetwork.AddressSpace != nil && virtualNetwork.AddressSpace.AddressPrefixes != nil {
181+
for _, addressPrefix := range *virtualNetwork.AddressSpace.AddressPrefixes {
182+
virtualNetworkAddressPrefixes = append(virtualNetworkAddressPrefixes, addressPrefix)
183+
}
184+
}
178185
}
179186

180187
azEnv := string(installConfig.Azure.CloudName)
181188

182189
computeSubnetSpec := capz.SubnetSpec{
183190
ID: nodeSubnetID,
184191
SubnetClassSpec: capz.SubnetClassSpec{
185-
Name: computeSubnet,
186-
Role: capz.SubnetNode,
187-
CIDRBlocks: []string{
188-
subnets[1].String(),
189-
},
192+
Name: computeSubnet,
193+
Role: capz.SubnetNode,
194+
CIDRBlocks: computeAddressPrefixes,
190195
},
191196
SecurityGroup: securityGroup,
192197
}
@@ -229,21 +234,17 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
229234
// https://github.com/kubernetes-sigs/cluster-api-provider-azure/commit/0f321e4089a3f4dc37f8420bf2ef6762c398c400
230235
ID: virtualNetworkID,
231236
VnetClassSpec: capz.VnetClassSpec{
232-
CIDRBlocks: []string{
233-
mainCIDR.String(),
234-
},
237+
CIDRBlocks: virtualNetworkAddressPrefixes,
235238
},
236239
},
237240
APIServerLB: &apiServerLB,
238241
ControlPlaneOutboundLB: controlPlaneOutboundLB,
239242
Subnets: capz.Subnets{
240243
{
241244
SubnetClassSpec: capz.SubnetClassSpec{
242-
Name: controlPlaneSubnet,
243-
Role: capz.SubnetControlPlane,
244-
CIDRBlocks: []string{
245-
subnets[0].String(),
246-
},
245+
Name: controlPlaneSubnet,
246+
Role: capz.SubnetControlPlane,
247+
CIDRBlocks: controlPlaneAddressPrefixes,
247248
},
248249
SecurityGroup: securityGroup,
249250
},
@@ -335,6 +336,76 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
335336
}, nil
336337
}
337338

339+
func dump(i interface{}) {
340+
data, _ := json.MarshalIndent(i, "", " ")
341+
logrus.Debugf("XXX: data=%s", data)
342+
}
343+
344+
func getSubnet(installConfig *installconfig.InstallConfig, clusterID *installconfig.ClusterID, subnetType, subnetName string) (*aznetwork.Subnet, error) {
345+
var subnet *aznetwork.Subnet = nil
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+
338409
func getIPWithinCIDR(subnets []*net.IPNet, ip string) string {
339410
if subnets == nil || ip == "" {
340411
return ""

0 commit comments

Comments
 (0)