Skip to content

Commit 30e01b0

Browse files
Merge pull request openshift#8662 from rna-afk/capz_existing_vnet
CORS-3073: azure: Allow for installs in existing vnets
2 parents 8b7d5c6 + 2bacfcd commit 30e01b0

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
@@ -13,6 +13,7 @@ import (
1313
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
1414
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
1515
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
16+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1617
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v3"
1718
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4"
1819
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/msi/armmsi"
@@ -230,6 +231,33 @@ func (p *Provider) PreProvision(ctx context.Context, in clusterapi.PreProvisionI
230231
return fmt.Errorf("failed to create role assignment: %w", err)
231232
}
232233

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

0 commit comments

Comments
 (0)