Skip to content

Commit

Permalink
Get VNET name and RG info from the VNET ID
Browse files Browse the repository at this point in the history
Signed-off-by: Bryan Cox <brcox@redhat.com>
  • Loading branch information
bryan-cox committed May 7, 2024
1 parent e05760b commit 344741c
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 130 deletions.
3 changes: 2 additions & 1 deletion cmd/cluster/azure/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewCreateCommand(opts *core.CreateOptions) *cobra.Command {
cmd.Flags().Int32Var(&opts.AzurePlatform.DiskSizeGB, "root-disk-size", opts.AzurePlatform.DiskSizeGB, "The size of the root disk for machines in the NodePool (minimum 16)")
cmd.Flags().StringSliceVar(&opts.AzurePlatform.AvailabilityZones, "availability-zones", opts.AzurePlatform.AvailabilityZones, "The availability zones in which NodePools will be created. Must be left unspecified if the region does not support AZs. If set, one nodepool per zone will be created.")
cmd.Flags().StringVar(&opts.AzurePlatform.ResourceGroupName, "resource-group-name", opts.AzurePlatform.ResourceGroupName, "A resource group name to create the HostedCluster infrastructure resources under.")
cmd.Flags().StringVar(&opts.AzurePlatform.VnetID, "vnet-id", opts.AzurePlatform.VnetID, "An existing VNET ID.")
cmd.Flags().StringVar(&opts.AzurePlatform.DiskEncryptionSetID, "disk-encryption-set-id", opts.AzurePlatform.DiskEncryptionSetID, "The Disk Encryption Set ID to use to encrypt the OS disks for the VMs.")
cmd.Flags().StringVar(&opts.AzurePlatform.NetworkSecurityGroup, "network-security-group", opts.AzurePlatform.NetworkSecurityGroup, "The name of the Network Security Group to use in Virtual Network created for HostedCluster.")
cmd.Flags().BoolVar(&opts.AzurePlatform.EnableEphemeralOSDisk, "enable-ephemeral-disk", opts.AzurePlatform.EnableEphemeralOSDisk, "If enabled, the Azure VMs in the default NodePool will be setup with ephemeral OS disks")
Expand Down Expand Up @@ -95,6 +96,7 @@ func applyPlatformSpecificsValues(ctx context.Context, exampleOptions *apifixtur
CredentialsFile: opts.AzurePlatform.CredentialsFile,
BaseDomain: opts.BaseDomain,
RHCOSImage: rhcosImage,
VnetID: opts.AzurePlatform.VnetID,
ResourceGroupName: opts.AzurePlatform.ResourceGroupName,
NetworkSecurityGroup: opts.AzurePlatform.NetworkSecurityGroup,
ResourceGroupTags: opts.AzurePlatform.ResourceGroupTags,
Expand All @@ -113,7 +115,6 @@ func applyPlatformSpecificsValues(ctx context.Context, exampleOptions *apifixtur
exampleOptions.Azure = &apifixtures.ExampleAzureOptions{
Location: infra.Location,
ResourceGroupName: infra.ResourceGroupName,
VnetName: infra.VnetName,
VnetID: infra.VNetID,
SubnetID: infra.SubnetID,
SubnetName: infra.SubnetName,
Expand Down
1 change: 1 addition & 0 deletions cmd/cluster/core/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type AzurePlatformOptions struct {
DiskSizeGB int32
AvailabilityZones []string
ResourceGroupName string
VnetID string
DiskEncryptionSetID string
NetworkSecurityGroup string
EnableEphemeralOSDisk bool
Expand Down
43 changes: 5 additions & 38 deletions cmd/infra/azure/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type CreateInfraOptions struct {
OutputFile string
RHCOSImage string
ResourceGroupName string
VnetID string
NetworkSecurityGroup string
ResourceGroupTags map[string]string
SubnetID string
Expand Down Expand Up @@ -167,9 +168,9 @@ func (o *CreateInfraOptions) Run(ctx context.Context, l logr.Logger) (*CreateInf
}
l.Info("Successfully assigned contributor role to managed identity", "name", identityID)

// Retrieve a client's existing virtual network if a vnet resource group was provided; otherwise, create a new VNET with a network security group
if len(o.VnetResourceGroupName) > 0 {
vnet, err := getClientsVirtualNetwork(ctx, subscriptionID, o.VnetResourceGroupName, o.ClientVnetName, azureCreds)
// Retrieve a client's existing virtual network if a VNET ID was provided; otherwise, create a new VNET with a network security group
if len(o.VnetID) > 0 {
vnet, err := azureutil.GetVnetInfoFromVnetID(ctx, o.VnetID, subscriptionID, azureCreds)
if err != nil {
return nil, err
}
Expand All @@ -180,7 +181,7 @@ func (o *CreateInfraOptions) Run(ctx context.Context, l logr.Logger) (*CreateInf
result.VnetName = *vnet.Name
l.Info("Successfully retrieved existing vnet", "name", result.VnetName)

// Extract network security group name if one exists
// Extract network security group name
if vnet.Properties.Subnets[0].Properties.NetworkSecurityGroup != nil && vnet.Properties.Subnets[0].Properties.NetworkSecurityGroup.ID != nil {
result.SecurityGroupID = *vnet.Properties.Subnets[0].Properties.NetworkSecurityGroup.ID
securityGroupName, err := azureutil.GetNetworkSecurityGroupNameFromNetworkSecurityGroupID(*vnet.Properties.Subnets[0].Properties.NetworkSecurityGroup.ID)
Expand Down Expand Up @@ -464,40 +465,6 @@ func createVirtualNetwork(ctx context.Context, subscriptionID string, resourceGr
return vnet, nil
}

func getClientsVirtualNetwork(ctx context.Context, subscriptionID string, vnetResourceGroupName string, clientVnetName string, azureCreds azcore.TokenCredential) (armnetwork.VirtualNetworksClientGetResponse, error) {
networksClient, err := armnetwork.NewVirtualNetworksClient(subscriptionID, azureCreds, nil)
if err != nil {
return armnetwork.VirtualNetworksClientGetResponse{}, fmt.Errorf("failed to create new virtual networks client: %w", err)
}

vnet, err := networksClient.Get(ctx, vnetResourceGroupName, clientVnetName, nil)
if err != nil {
return armnetwork.VirtualNetworksClientGetResponse{}, fmt.Errorf("failed to get virtual network: %w", err)
}

if vnet.ID == nil {
return armnetwork.VirtualNetworksClientGetResponse{}, fmt.Errorf("virtual network has no ID")
}

if vnet.Name == nil {
return armnetwork.VirtualNetworksClientGetResponse{}, fmt.Errorf("virtual network has no name")
}

if vnet.Properties.Subnets == nil || len(vnet.Properties.Subnets) == 0 {
return armnetwork.VirtualNetworksClientGetResponse{}, fmt.Errorf("no subnets found for resource group '%s'", vnetResourceGroupName)
}

if vnet.Properties.Subnets[0].ID == nil {
return armnetwork.VirtualNetworksClientGetResponse{}, fmt.Errorf("no subnet ID found for resource group '%s'", vnetResourceGroupName)
}

if vnet.Properties.Subnets[0].Name == nil {
return armnetwork.VirtualNetworksClientGetResponse{}, fmt.Errorf("no subnet name found for resource group '%s'", vnetResourceGroupName)
}

return vnet, nil
}

// createPrivateDNSZone creates the private DNS zone
func createPrivateDNSZone(ctx context.Context, subscriptionID string, resourceGroupName string, name string, baseDomain string, azureCreds azcore.TokenCredential) (string, string, error) {
privateZoneClient, err := armprivatedns.NewPrivateZonesClient(subscriptionID, azureCreds, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,23 @@ func azureConfigWithoutCredentials(hcp *hyperv1.HostedControlPlane, credentialsS
return AzureConfig{}, fmt.Errorf("failed to determine security group name from SecurityGroupID: %w", err)
}

vnetName, vnetResourceGroup, err := azureutil.GetVnetNameAndResourceGroupFromVnetID(hcp.Spec.Platform.Azure.VnetID)
if err != nil {
return AzureConfig{}, fmt.Errorf("failed to determine vnet name from VnetID: %w", err)
}

azureConfig := AzureConfig{
Cloud: hcp.Spec.Platform.Azure.Cloud,
TenantID: string(credentialsSecret.Data["AZURE_TENANT_ID"]),
UseManagedIdentityExtension: true,
SubscriptionID: hcp.Spec.Platform.Azure.SubscriptionID,
ResourceGroup: hcp.Spec.Platform.Azure.ResourceGroupName,
Location: hcp.Spec.Platform.Azure.Location,
VnetName: hcp.Spec.Platform.Azure.VnetName,
VnetResourceGroup: hcp.Spec.Platform.Azure.ResourceGroupName,
VnetName: vnetName,
VnetResourceGroup: vnetResourceGroup,
SubnetName: subnetName,
SecurityGroupName: securityGroupName,
SecurityGroupResourceGroup: hcp.Spec.Platform.Azure.ResourceGroupName,
SecurityGroupResourceGroup: vnetResourceGroup,
LoadBalancerName: hcp.Spec.InfraID,
CloudProviderBackoff: true,
CloudProviderBackoffDuration: 6,
Expand All @@ -89,13 +94,6 @@ func azureConfigWithoutCredentials(hcp *hyperv1.HostedControlPlane, credentialsS
DisableOutboundSNAT: true,
}

// In ARO HCP, the VNET and NSG will be in the network resource group; it will not be in the resource group
// containing all other cloud infrastructure resources.
if hcp.Spec.Platform.Azure.VnetResourceGroupName != "" {
azureConfig.VnetResourceGroup = hcp.Spec.Platform.Azure.VnetResourceGroupName
azureConfig.SecurityGroupResourceGroup = hcp.Spec.Platform.Azure.VnetResourceGroupName
}

return azureConfig, nil
}

Expand Down
16 changes: 8 additions & 8 deletions examples/fixtures/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,14 @@ func (o ExampleOptions) Resources() *ExampleResources {
platformSpec = hyperv1.PlatformSpec{
Type: hyperv1.AzurePlatform,
Azure: &hyperv1.AzurePlatformSpec{
Credentials: corev1.LocalObjectReference{Name: credentialSecret.Name},
Location: o.Azure.Location,
ResourceGroupName: o.Azure.ResourceGroupName,
VnetID: o.Azure.VnetID,
SubnetID: o.Azure.SubnetID,
SubscriptionID: o.Azure.Creds.SubscriptionID,
MachineIdentityID: o.Azure.MachineIdentityID,
SecurityGroupID: o.Azure.SecurityGroupID,
Credentials: corev1.LocalObjectReference{Name: credentialSecret.Name},
Location: o.Azure.Location,
ResourceGroupName: o.Azure.ResourceGroupName,
VnetID: o.Azure.VnetID,
SubnetID: o.Azure.SubnetID,
SubscriptionID: o.Azure.Creds.SubscriptionID,
MachineIdentityID: o.Azure.MachineIdentityID,
SecurityGroupID: o.Azure.SecurityGroupID,
},
}

Expand Down
1 change: 0 additions & 1 deletion examples/fixtures/example_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type ExampleAzureOptions struct {
Creds util.AzureCreds
Location string
ResourceGroupName string
VnetName string
VnetID string
SubnetID string
SubnetName string
Expand Down

0 comments on commit 344741c

Please sign in to comment.