Skip to content

Commit 2bacfcd

Browse files
committed
azure: Allow for installs in existing vnets
Adding the option to allow for existing vnets installation in CAPZ.
1 parent 5b289b7 commit 2bacfcd

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

pkg/asset/manifests/azure/cluster.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/openshift/installer/pkg/asset/installconfig"
1515
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
1616
"github.com/openshift/installer/pkg/asset/manifests/capiutils/cidr"
17+
"github.com/openshift/installer/pkg/types"
1718
)
1819

1920
// GenerateClusterAssets generates the manifests for the cluster-api.
@@ -41,8 +42,13 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
4142

4243
resourceGroup := installConfig.Config.Platform.Azure.ClusterResourceGroupName(clusterID.InfraID)
4344
controlPlaneSubnet := installConfig.Config.Platform.Azure.ControlPlaneSubnetName(clusterID.InfraID)
44-
networkSecurityGroup := installConfig.Config.Platform.Azure.NetworkSecurityGroupName(clusterID.InfraID)
4545
computeSubnet := installConfig.Config.Platform.Azure.ComputeSubnetName(clusterID.InfraID)
46+
networkSecurityGroup := installConfig.Config.Platform.Azure.NetworkSecurityGroupName(clusterID.InfraID)
47+
48+
source := "*"
49+
if installConfig.Config.Publish == types.InternalPublishingStrategy {
50+
source = mainCIDR.String()
51+
}
4652

4753
securityGroup := capz.SecurityGroup{
4854
Name: networkSecurityGroup,
@@ -55,14 +61,13 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
5561
Priority: 101,
5662
SourcePorts: ptr.To("*"),
5763
DestinationPorts: ptr.To("6443"),
58-
Source: ptr.To("*"),
64+
Source: ptr.To(source),
5965
Destination: ptr.To("*"),
6066
Action: capz.SecurityRuleActionAllow,
6167
},
6268
},
6369
},
6470
}
65-
6671
azureCluster := &capz.AzureCluster{
6772
ObjectMeta: metav1.ObjectMeta{
6873
Name: clusterID.InfraID,
@@ -86,6 +91,13 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
8691
PrivateDNSZoneName: installConfig.Config.ClusterDomain(),
8792
},
8893
Vnet: capz.VnetSpec{
94+
ResourceGroup: installConfig.Config.Azure.NetworkResourceGroupName,
95+
Name: installConfig.Config.Azure.VirtualNetwork,
96+
// The ID is set to virtual network here for existing vnets here. This is to force CAPZ to consider this resource as
97+
// "not managed" which would prevent the creation of an additional nsg and route table in the network resource group.
98+
// The ID field is not used for any other purpose in CAPZ except to set the "managed" status.
99+
// See https://github.com/kubernetes-sigs/cluster-api-provider-azure/blob/main/azure/scope/cluster.go#L585
100+
// https://github.com/kubernetes-sigs/cluster-api-provider-azure/commit/0f321e4089a3f4dc37f8420bf2ef6762c398c400
89101
ID: installConfig.Config.Azure.VirtualNetwork,
90102
VnetClassSpec: capz.VnetClassSpec{
91103
CIDRBlocks: []string{

pkg/infrastructure/azure/azure.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
1313
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
14+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1415
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v3"
1516
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4"
1617
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/msi/armmsi"
@@ -223,6 +224,33 @@ func (p *Provider) PreProvision(ctx context.Context, in clusterapi.PreProvisionI
223224
return fmt.Errorf("failed to create role assignment: %w", err)
224225
}
225226

227+
// Creating a dummy nsg for existing vnets installation to appease the ingress operator.
228+
if in.InstallConfig.Config.Azure.VirtualNetwork != "" {
229+
networkClientFactory, err := armnetwork.NewClientFactory(subscriptionID, tokenCredential, nil)
230+
if err != nil {
231+
return fmt.Errorf("failed to create azure network factory: %w", err)
232+
}
233+
securityGroupName := in.InstallConfig.Config.Platform.Azure.NetworkSecurityGroupName(in.InfraID)
234+
securityGroupsClient := networkClientFactory.NewSecurityGroupsClient()
235+
pollerResp, err := securityGroupsClient.BeginCreateOrUpdate(
236+
ctx,
237+
resourceGroupName,
238+
securityGroupName,
239+
armnetwork.SecurityGroup{
240+
Location: to.Ptr(platform.Region),
241+
Tags: tags,
242+
},
243+
nil)
244+
if err != nil {
245+
return fmt.Errorf("failed to create network security group: %w", err)
246+
}
247+
nsg, err := pollerResp.PollUntilDone(ctx, nil)
248+
if err != nil {
249+
return fmt.Errorf("failed to create network security group: %w", err)
250+
}
251+
logrus.Infof("nsg=%s", *nsg.ID)
252+
}
253+
226254
return nil
227255
}
228256

0 commit comments

Comments
 (0)