Skip to content

Commit 8d801c8

Browse files
Merge pull request openshift#8732 from jhixson74/capz_private
CORS-3565: CAPZ private clusters
2 parents 9c70865 + 9e498ee commit 8d801c8

File tree

4 files changed

+85
-46
lines changed

4 files changed

+85
-46
lines changed

pkg/asset/machines/azure/azuremachines.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,37 @@ const (
2323
genV2Suffix string = "-gen2"
2424
)
2525

26+
// MachineInput defines the inputs needed to generate a machine asset.
27+
type MachineInput struct {
28+
Subnet string
29+
Role string
30+
UserDataSecret string
31+
HyperVGen string
32+
UseImageGallery bool
33+
Private bool
34+
UserTags map[string]string
35+
Platform *azure.Platform
36+
Pool *types.MachinePool
37+
}
38+
2639
// GenerateMachines returns manifests and runtime objects to provision the control plane (including bootstrap, if applicable) nodes using CAPI.
27-
func GenerateMachines(platform *azure.Platform, pool *types.MachinePool, userDataSecret string, clusterID string, role string, capabilities map[string]string, useImageGallery bool, userTags map[string]string, hyperVGen string, subnet string, resourceGroup string, subscriptionID string) ([]*asset.RuntimeFile, error) {
28-
if poolPlatform := pool.Platform.Name(); poolPlatform != azure.Name {
40+
func GenerateMachines(clusterID, resourceGroup, subscriptionID string, in *MachineInput) ([]*asset.RuntimeFile, error) {
41+
if poolPlatform := in.Pool.Platform.Name(); poolPlatform != azure.Name {
2942
return nil, fmt.Errorf("non-Azure machine-pool: %q", poolPlatform)
3043
}
31-
mpool := pool.Platform.Azure
44+
mpool := in.Pool.Platform.Azure
3245

3346
total := int64(1)
34-
if pool.Replicas != nil {
35-
total = *pool.Replicas
47+
if in.Pool.Replicas != nil {
48+
total = *in.Pool.Replicas
3649
}
3750

3851
if len(mpool.Zones) == 0 {
3952
// if no azs are given we set to []string{""} for convenience over later operations.
4053
// It means no-zoned for the machine API
4154
mpool.Zones = []string{""}
4255
}
43-
tags, err := CapzTagsFromUserTags(clusterID, userTags)
56+
tags, err := CapzTagsFromUserTags(clusterID, in.UserTags)
4457
if err != nil {
4558
return nil, fmt.Errorf("failed to create machineapi.TagSpecifications from UserTags: %w", err)
4659
}
@@ -64,17 +77,17 @@ func GenerateMachines(platform *azure.Platform, pool *types.MachinePool, userDat
6477
ThirdPartyImage: osImage.Plan != azure.ImageNoPurchasePlan,
6578
},
6679
}
67-
case useImageGallery:
80+
case in.UseImageGallery:
6881
// image gallery names cannot have dashes
6982
id := clusterID
70-
if hyperVGen == "V2" {
83+
if in.HyperVGen == "V2" {
7184
id += genV2Suffix
7285
}
7386
imageID := fmt.Sprintf("/resourceGroups/%s/providers/Microsoft.Compute/galleries/gallery_%s/images/%s/versions/latest", resourceGroup, galleryName, id)
7487
image = &capz.Image{ID: &imageID}
7588
default:
7689
imageID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/galleries/gallery_%s/images/%s", subscriptionID, resourceGroup, galleryName, clusterID)
77-
if hyperVGen == "V2" && platform.CloudName != azure.StackCloud {
90+
if in.HyperVGen == "V2" && in.Platform.CloudName != azure.StackCloud {
7891
imageID += genV2Suffix
7992
}
8093
image = &capz.Image{ID: &imageID}
@@ -92,7 +105,7 @@ func GenerateMachines(platform *azure.Platform, pool *types.MachinePool, userDat
92105
additionalCapabilities := &capz.AdditionalCapabilities{
93106
UltraSSDEnabled: &ultrassd,
94107
}
95-
if pool.Platform.Azure.DiskEncryptionSet != nil {
108+
if in.Pool.Platform.Azure.DiskEncryptionSet != nil {
96109
osDisk.ManagedDisk.DiskEncryptionSet = &capz.DiskEncryptionSetParameters{
97110
ID: mpool.OSDisk.DiskEncryptionSet.ToID(),
98111
}
@@ -120,7 +133,7 @@ func GenerateMachines(platform *azure.Platform, pool *types.MachinePool, userDat
120133
zone := mpool.Zones[int(idx)%len(mpool.Zones)]
121134
azureMachine := &capz.AzureMachine{
122135
ObjectMeta: metav1.ObjectMeta{
123-
Name: fmt.Sprintf("%s-%s-%d", clusterID, pool.Name, idx),
136+
Name: fmt.Sprintf("%s-%s-%d", clusterID, in.Pool.Name, idx),
124137
Labels: map[string]string{
125138
"cluster.x-k8s.io/control-plane": "",
126139
"cluster.x-k8s.io/cluster-name": clusterID,
@@ -139,7 +152,7 @@ func GenerateMachines(platform *azure.Platform, pool *types.MachinePool, userDat
139152
SecurityProfile: securityProfile,
140153
NetworkInterfaces: []capz.NetworkInterface{
141154
{
142-
SubnetName: subnet,
155+
SubnetName: in.Subnet,
143156
AcceleratedNetworking: ptr.To(mpool.VMNetworkingType == string(azure.VMnetworkingTypeAccelerated) || mpool.VMNetworkingType == string(azure.AcceleratedNetworkingEnabled)),
144157
},
145158
},
@@ -167,7 +180,7 @@ func GenerateMachines(platform *azure.Platform, pool *types.MachinePool, userDat
167180
Spec: capi.MachineSpec{
168181
ClusterName: clusterID,
169182
Bootstrap: capi.Bootstrap{
170-
DataSecretName: ptr.To(fmt.Sprintf("%s-%s", clusterID, role)),
183+
DataSecretName: ptr.To(fmt.Sprintf("%s-%s", clusterID, in.Role)),
171184
},
172185
InfrastructureRef: v1.ObjectReference{
173186
APIVersion: capz.GroupVersion.String(),
@@ -200,7 +213,7 @@ func GenerateMachines(platform *azure.Platform, pool *types.MachinePool, userDat
200213
OSDisk: osDisk,
201214
AdditionalTags: tags,
202215
DisableExtensionOperations: ptr.To(true),
203-
AllocatePublicIP: true,
216+
AllocatePublicIP: !in.Private,
204217
AdditionalCapabilities: additionalCapabilities,
205218
SecurityProfile: securityProfile,
206219
Identity: capz.VMIdentityUserAssigned,

pkg/asset/machines/clusterapi.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,22 @@ func (c *ClusterAPI) Generate(ctx context.Context, dependencies asset.Parents) e
277277
if err != nil {
278278
return err
279279
}
280-
// useImageGallery := installConfig.Azure.CloudName != azuretypes.StackCloud
281-
useImageGallery := false
282-
masterUserDataSecretName := "master-user-data"
283-
resourceGroupName := installConfig.Config.Azure.ClusterResourceGroupName(clusterID.InfraID)
284280

285-
azureMachines, err := azure.GenerateMachines(installConfig.Config.Platform.Azure, &pool, masterUserDataSecretName, clusterID.InfraID, "master", capabilities, useImageGallery, installConfig.Config.Platform.Azure.UserTags, hyperVGen, subnet, resourceGroupName, session.Credentials.SubscriptionID)
281+
azureMachines, err := azure.GenerateMachines(clusterID.InfraID,
282+
installConfig.Config.Azure.ClusterResourceGroupName(clusterID.InfraID),
283+
session.Credentials.SubscriptionID,
284+
&azure.MachineInput{
285+
Subnet: subnet,
286+
Role: "master",
287+
UserDataSecret: "master-user-data",
288+
HyperVGen: hyperVGen,
289+
UseImageGallery: false,
290+
Private: installConfig.Config.Publish == types.InternalPublishingStrategy,
291+
UserTags: installConfig.Config.Platform.Azure.UserTags,
292+
Platform: installConfig.Config.Platform.Azure,
293+
Pool: &pool,
294+
},
295+
)
286296
if err != nil {
287297
return fmt.Errorf("failed to create master machine objects: %w", err)
288298
}

pkg/asset/manifests/azure/cluster.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
4747
computeSubnet := installConfig.Config.Platform.Azure.ComputeSubnetName(clusterID.InfraID)
4848
networkSecurityGroup := installConfig.Config.Platform.Azure.NetworkSecurityGroupName(clusterID.InfraID)
4949

50-
source := "*"
51-
if installConfig.Config.Publish == types.InternalPublishingStrategy {
52-
source = mainCIDR.String()
53-
}
54-
5550
controlPlaneOutboundLB := &capz.LoadBalancerSpec{
5651
Name: clusterID.InfraID,
5752
FrontendIPsCount: to.Ptr(int32(1)),
@@ -60,6 +55,11 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
6055
controlPlaneOutboundLB = nil
6156
}
6257

58+
source := "*"
59+
if installConfig.Config.Publish == types.InternalPublishingStrategy {
60+
source = mainCIDR.String()
61+
}
62+
6363
securityGroup := capz.SecurityGroup{
6464
Name: networkSecurityGroup,
6565
SecurityGroupClass: capz.SecurityGroupClass{

pkg/infrastructure/azure/azure.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,11 @@ func (p *Provider) PostProvision(ctx context.Context, in clusterapi.PostProvisio
595595
return fmt.Errorf("failed to associate control plane VMs with external load balancer: %w", err)
596596
}
597597

598+
sshRuleName := fmt.Sprintf("%s_ssh_in", in.InfraID)
598599
if err = addSecurityGroupRule(ctx, &securityGroupInput{
599600
resourceGroupName: p.ResourceGroupName,
600601
securityGroupName: fmt.Sprintf("%s-nsg", in.InfraID),
601-
securityRuleName: "ssh_in",
602+
securityRuleName: sshRuleName,
602603
securityRulePort: "22",
603604
securityRulePriority: 220,
604605
networkClientFactory: p.NetworkClientFactory,
@@ -626,7 +627,7 @@ func (p *Provider) PostProvision(ctx context.Context, in clusterapi.PostProvisio
626627
resourceGroupName: p.ResourceGroupName,
627628
loadBalancerName: loadBalancerName,
628629
frontendIPConfigID: frontendIPConfigID,
629-
inboundNatRuleName: "ssh_in",
630+
inboundNatRuleName: sshRuleName,
630631
inboundNatRulePort: 22,
631632
networkClientFactory: p.NetworkClientFactory,
632633
})
@@ -639,7 +640,7 @@ func (p *Provider) PostProvision(ctx context.Context, in clusterapi.PostProvisio
639640
bootstrapNicName: fmt.Sprintf("%s-bootstrap-nic", in.InfraID),
640641
frontendIPConfigID: frontendIPConfigID,
641642
inboundNatRuleID: *inboundNatRule.ID,
642-
inboundNatRuleName: "ssh_in",
643+
inboundNatRuleName: sshRuleName,
643644
inboundNatRulePort: 22,
644645
networkClientFactory: p.NetworkClientFactory,
645646
})
@@ -673,26 +674,41 @@ func (p *Provider) PostDestroy(ctx context.Context, in clusterapi.PostDestroyerI
673674
return fmt.Errorf("error creating network client factory: %w", err)
674675
}
675676

676-
// XXX: why is in.Metadata.Azure.ResourceGroupName empty?
677-
err = deleteSecurityGroupRule(ctx, &securityGroupInput{
678-
resourceGroupName: fmt.Sprintf("%s-rg", in.Metadata.InfraID),
679-
securityGroupName: fmt.Sprintf("%s-nsg", in.Metadata.InfraID),
680-
securityRuleName: "ssh_in",
681-
securityRulePort: "22",
682-
networkClientFactory: networkClientFactory,
683-
})
684-
if err != nil {
685-
return fmt.Errorf("failed to delete security rule: %w", err)
686-
}
677+
resourceGroupName := fmt.Sprintf("%s-rg", in.Metadata.InfraID)
678+
securityGroupName := fmt.Sprintf("%s-nsg", in.Metadata.InfraID)
679+
sshRuleName := fmt.Sprintf("%s_ssh_in", in.Metadata.InfraID)
687680

688-
err = deleteInboundNatRule(ctx, &inboundNatRuleInput{
689-
resourceGroupName: fmt.Sprintf("%s-rg", in.Metadata.InfraID),
690-
loadBalancerName: in.Metadata.InfraID,
691-
inboundNatRuleName: "ssh_in",
692-
networkClientFactory: networkClientFactory,
693-
})
694-
if err != nil {
695-
return fmt.Errorf("failed to delete inbound nat rule: %w", err)
681+
// See if a security group rule exists with the name ${InfraID}_ssh_in.
682+
// If it does, this is a private cluster. If it does not, this is a
683+
// public cluster and we need to delete the SSH forward rule and
684+
// security group rule.
685+
_, err = networkClientFactory.NewSecurityRulesClient().Get(ctx,
686+
resourceGroupName,
687+
securityGroupName,
688+
sshRuleName,
689+
nil,
690+
)
691+
if err == nil {
692+
err = deleteSecurityGroupRule(ctx, &securityGroupInput{
693+
resourceGroupName: resourceGroupName,
694+
securityGroupName: securityGroupName,
695+
securityRuleName: sshRuleName,
696+
securityRulePort: "22",
697+
networkClientFactory: networkClientFactory,
698+
})
699+
if err != nil {
700+
return fmt.Errorf("failed to delete security rule: %w", err)
701+
}
702+
703+
err = deleteInboundNatRule(ctx, &inboundNatRuleInput{
704+
resourceGroupName: resourceGroupName,
705+
loadBalancerName: in.Metadata.InfraID,
706+
inboundNatRuleName: sshRuleName,
707+
networkClientFactory: networkClientFactory,
708+
})
709+
if err != nil {
710+
return fmt.Errorf("failed to delete inbound nat rule: %w", err)
711+
}
696712
}
697713

698714
return nil

0 commit comments

Comments
 (0)