Skip to content

Commit

Permalink
azure: Create resource group during PreProvision
Browse files Browse the repository at this point in the history
Creating a resource group in PreProvision stage of CAPZ to
enable users to set the ManagedBy field.
  • Loading branch information
rna-afk committed Mar 7, 2024
1 parent 38f1304 commit eb94835
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/infrastructure/azure/azure.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package azure

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/Azure/go-autorest/autorest/to"

"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/infrastructure/clusterapi"
azuretypes "github.com/openshift/installer/pkg/types/azure"
)
Expand All @@ -12,3 +20,34 @@ type Provider struct{}

// Name gives the name of the provider, Azure.
func (*Provider) Name() string { return azuretypes.Name }

func (*Provider) PreProvision(ctx context.Context, in clusterapi.PreProvisionInput) {
if in.InstallConfig.Config.Azure.ResourceGroupName == "" {
createResourceGroup(ctx, in.InstallConfig, in.InfraID)
}
}

// createResourceGroup creates the resource group required for Azure installation.
func createResourceGroup(ctx context.Context, ic *installconfig.InstallConfig, infraID string) error {
rgName := fmt.Sprintf("%s-rg", infraID)
managedBy := ic.Config.Azure.ManagedBy
session, err := ic.Azure.Session()
if err != nil {
return err
}
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return err
}
rgClient, err := armresources.NewResourceGroupsClient(session.Credentials.SubscriptionID, cred, nil)
if err != nil {
return err
}
param := armresources.ResourceGroup{
Location: to.StringPtr(ic.Config.Azure.Region),
ManagedBy: &managedBy,
}

_, err = rgClient.CreateOrUpdate(ctx, rgName, param, nil)
return err
}

0 comments on commit eb94835

Please sign in to comment.