Skip to content

Commit

Permalink
enable per-sub msi client
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Francis <[email protected]>
  • Loading branch information
jackfrancis committed Apr 9, 2024
1 parent 743d95f commit c2d5e10
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
13 changes: 13 additions & 0 deletions azure/services/identities/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ func NewClient(auth azure.Authorizer) (Client, error) {
return &AzureClient{factory.NewUserAssignedIdentitiesClient()}, nil
}

// NewClientBySub creates a new MSI client with a given subscriptionID

Check failure on line 54 in azure/services/identities/client.go

View workflow job for this annotation

GitHub Actions / coverage

Comment should end in a period (godot)
func NewClientBySub(auth azure.Authorizer, subscriptionID string) (Client, error) {
opts, err := azure.ARMClientOptions(auth.CloudEnvironment())
if err != nil {
return nil, errors.Wrap(err, "failed to create identities client options")
}
factory, err := armmsi.NewClientFactory(subscriptionID, auth.Token(), opts)
if err != nil {
return nil, errors.Wrap(err, "failed to create armmsi client factory")
}
return &AzureClient{factory.NewUserAssignedIdentitiesClient()}, nil
}

// Get returns a managed service identity.
func (ac *AzureClient) Get(ctx context.Context, resourceGroupName, name string) (armmsi.Identity, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "identities.AzureClient.Get")
Expand Down
13 changes: 12 additions & 1 deletion azure/services/virtualmachines/virtualmachines.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,18 @@ func (s *Service) checkUserAssignedIdentities(ctx context.Context, specIdentitie

// Create a map of the expected identities. The ProviderID is converted to match the format of the VM identity.
for _, expectedIdentity := range specIdentities {
expectedClientID, err := s.identitiesGetter.GetClientID(ctx, expectedIdentity.ProviderID)
var identitiesClient identities.Client = s.identitiesGetter

Check failure on line 179 in azure/services/virtualmachines/virtualmachines.go

View workflow job for this annotation

GitHub Actions / coverage

ST1023: should omit type identities.Client from declaration; it will be inferred from the right-hand side (stylecheck)
parsed, err := azureutil.ParseResourceID(expectedIdentity.ProviderID)
if err != nil {
return err
}
if parsed.SubscriptionID != s.Scope.SubscriptionID() {
identitiesClient, err = identities.NewClientBySub(s.Scope, parsed.SubscriptionID)
if err != nil {
return errors.Wrapf(err, "failed to create identities client from subscription ID %s", parsed.SubscriptionID)
}
}
expectedClientID, err := identitiesClient.GetClientID(ctx, expectedIdentity.ProviderID)
if err != nil {
return errors.Wrap(err, "failed to get client ID")
}
Expand Down
16 changes: 14 additions & 2 deletions controllers/azurejson_machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -215,11 +216,22 @@ func (r *AzureJSONMachineReconciler) Reconcile(ctx context.Context, req ctrl.Req
// Construct secret for this machine
userAssignedIdentityIfExists := ""
if len(azureMachine.Spec.UserAssignedIdentities) > 0 {
idsClient, err := identities.NewClient(clusterScope)
var identitiesClient identities.Client
identitiesClient, err := identities.NewClient(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create identities client")
}
userAssignedIdentityIfExists, err = idsClient.GetClientID(
parsed, err := azureutil.ParseResourceID(azureMachine.Spec.UserAssignedIdentities[0].ProviderID)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to parse ProviderID %s", azureMachine.Spec.UserAssignedIdentities[0].ProviderID)
}
if parsed.SubscriptionID != clusterScope.SubscriptionID() {
identitiesClient, err = identities.NewClientBySub(clusterScope, parsed.SubscriptionID)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to create identities client from subscription ID %s", parsed.SubscriptionID)
}
}
userAssignedIdentityIfExists, err = identitiesClient.GetClientID(
ctx, azureMachine.Spec.UserAssignedIdentities[0].ProviderID)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to get user-assigned identity ClientID")
Expand Down
16 changes: 14 additions & 2 deletions controllers/azurejson_machinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
Expand Down Expand Up @@ -148,11 +149,22 @@ func (r *AzureJSONMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl
// Construct secret for this machine
userAssignedIdentityIfExists := ""
if len(azureMachinePool.Spec.UserAssignedIdentities) > 0 {
idsClient, err := getClient(clusterScope)
var identitiesClient identities.Client
identitiesClient, err := getClient(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create identities client")
}
userAssignedIdentityIfExists, err = idsClient.GetClientID(
parsed, err := azureutil.ParseResourceID(azureMachinePool.Spec.UserAssignedIdentities[0].ProviderID)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to parse ProviderID %s", azureMachinePool.Spec.UserAssignedIdentities[0].ProviderID)
}
if parsed.SubscriptionID != clusterScope.SubscriptionID() {
identitiesClient, err = identities.NewClientBySub(clusterScope, parsed.SubscriptionID)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to create identities client from subscription ID %s", parsed.SubscriptionID)
}
}
userAssignedIdentityIfExists, err = identitiesClient.GetClientID(
ctx, azureMachinePool.Spec.UserAssignedIdentities[0].ProviderID)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to get user-assigned identity ClientID")
Expand Down
16 changes: 14 additions & 2 deletions controllers/azurejson_machinetemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -175,11 +176,22 @@ func (r *AzureJSONTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Re
// Construct secret for this machine template
userAssignedIdentityIfExists := ""
if len(azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities) > 0 {
idsClient, err := identities.NewClient(clusterScope)
var identitiesClient identities.Client
identitiesClient, err := identities.NewClient(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create identities client")
}
userAssignedIdentityIfExists, err = idsClient.GetClientID(
parsed, err := azureutil.ParseResourceID(azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities[0].ProviderID)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to parse ProviderID %s", azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities[0].ProviderID)
}
if parsed.SubscriptionID != clusterScope.SubscriptionID() {
identitiesClient, err = identities.NewClientBySub(clusterScope, parsed.SubscriptionID)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to create identities client from subscription ID %s", parsed.SubscriptionID)
}
}
userAssignedIdentityIfExists, err = identitiesClient.GetClientID(
ctx, azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities[0].ProviderID)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to get user-assigned identity ClientID")
Expand Down

0 comments on commit c2d5e10

Please sign in to comment.