Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
willie-yao committed Dec 1, 2023
1 parent baa8629 commit 166b2e6
Show file tree
Hide file tree
Showing 13 changed files with 955 additions and 185 deletions.
31 changes: 0 additions & 31 deletions azure/scope/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/Azure/go-autorest/autorest"
azureautorest "github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
)

// AzureClients contains all the Azure clients used by the scopes.
Expand Down Expand Up @@ -80,36 +79,6 @@ func (c *AzureClients) HashKey() string {
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}

func (c *AzureClients) setCredentials(subscriptionID, environmentName string) error {
settings, err := c.getSettingsFromEnvironment(environmentName)
if err != nil {
return err
}

if subscriptionID == "" {
subscriptionID = settings.GetSubscriptionID()
if subscriptionID == "" {
return fmt.Errorf("error creating azure services. subscriptionID is not set in cluster or AZURE_SUBSCRIPTION_ID env var")
}
}

c.EnvironmentSettings = settings
c.ResourceManagerEndpoint = settings.Environment.ResourceManagerEndpoint
c.ResourceManagerVMDNSSuffix = settings.Environment.ResourceManagerVMDNSSuffix
c.Values[auth.ClientID] = strings.TrimSuffix(c.Values[auth.ClientID], "\n")
c.Values[auth.ClientSecret] = strings.TrimSuffix(c.Values[auth.ClientSecret], "\n")
c.Values[auth.SubscriptionID] = strings.TrimSuffix(subscriptionID, "\n")
c.Values[auth.TenantID] = strings.TrimSuffix(c.Values[auth.TenantID], "\n")

if c.Authorizer == nil {
c.Authorizer, err = azureutil.GetAuthorizer(settings)
if err != nil {
return err
}
}
return nil
}

func (c *AzureClients) setCredentialsWithProvider(ctx context.Context, subscriptionID, environmentName string, credentialsProvider CredentialsProvider) error {
if credentialsProvider == nil {
return fmt.Errorf("credentials provider cannot have an empty value")
Expand Down
81 changes: 0 additions & 81 deletions azure/scope/clients_test.go

This file was deleted.

21 changes: 7 additions & 14 deletions azure/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,13 @@ func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterSc
return nil, errors.New("failed to generate new scope from nil AzureCluster")
}

if params.AzureCluster.Spec.IdentityRef == nil {
err := params.AzureClients.setCredentials(params.AzureCluster.Spec.SubscriptionID, params.AzureCluster.Spec.AzureEnvironment)
if err != nil {
return nil, errors.Wrap(err, "failed to configure azure settings and credentials from environment")
}
} else {
credentialsProvider, err := NewAzureClusterCredentialsProvider(ctx, params.Client, params.AzureCluster)
if err != nil {
return nil, errors.Wrap(err, "failed to init credentials provider")
}
err = params.AzureClients.setCredentialsWithProvider(ctx, params.AzureCluster.Spec.SubscriptionID, params.AzureCluster.Spec.AzureEnvironment, credentialsProvider)
if err != nil {
return nil, errors.Wrap(err, "failed to configure azure settings and credentials for Identity")
}
credentialsProvider, err := NewAzureClusterCredentialsProvider(ctx, params.Client, params.AzureCluster)
if err != nil {
return nil, errors.Wrap(err, "failed to init credentials provider")
}
err = params.AzureClients.setCredentialsWithProvider(ctx, params.AzureCluster.Spec.SubscriptionID, params.AzureCluster.Spec.AzureEnvironment, credentialsProvider)
if err != nil {
return nil, errors.Wrap(err, "failed to configure azure settings and credentials for Identity")
}

if params.Cache == nil {
Expand Down
Loading

0 comments on commit 166b2e6

Please sign in to comment.