@@ -25,26 +25,31 @@ import (
2525 "strings"
2626
2727 "github.com/Azure/azure-sdk-for-go/sdk/azcore"
28- azureautorest "github.com/Azure/go-autorest/autorest/azure"
29- "github.com/Azure/go-autorest/autorest/azure/auth"
28+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
3029
3130 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
31+ "sigs.k8s.io/cluster-api-provider-azure/azure"
3232)
3333
3434// AzureClients contains all the Azure clients used by the scopes.
3535type AzureClients struct {
36- auth. EnvironmentSettings
36+ environmentSettings
3737
3838 TokenCredential azcore.TokenCredential
39- ResourceManagerEndpoint string
4039 ResourceManagerVMDNSSuffix string
4140
4241 authType infrav1.IdentityType
4342}
4443
44+ type environmentSettings struct {
45+ Values map [string ]string
46+ CloudType string
47+ CloudSettings cloud.Configuration
48+ }
49+
4550// CloudEnvironment returns the Azure environment the controller runs in.
4651func (c * AzureClients ) CloudEnvironment () string {
47- return c .Environment . Name
52+ return c .environmentSettings . CloudType
4853}
4954
5055// TenantID returns the Azure tenant id the controller runs in.
@@ -86,45 +91,40 @@ func (c *AzureClients) setCredentialsWithProvider(ctx context.Context, subscript
8691 return fmt .Errorf ("credentials provider cannot have an empty value" )
8792 }
8893
89- settings , err := c .getSettingsFromEnvironment (environmentName )
90- if err != nil {
91- return err
92- }
94+ c .environmentSettings = c .getSettingsFromEnvironment (environmentName )
9395
9496 if subscriptionID == "" {
95- subscriptionID = settings . GetSubscriptionID ()
97+ subscriptionID = c . environmentSettings . Values [ "AZURE_SUBSCRIPTION_ID" ]
9698 if subscriptionID == "" {
9799 return fmt .Errorf ("error creating azure services. subscriptionID is not set in cluster or AZURE_SUBSCRIPTION_ID env var" )
98100 }
99101 }
100102
101- c .EnvironmentSettings = settings
102- c .ResourceManagerEndpoint = settings .Environment .ResourceManagerEndpoint
103- c .ResourceManagerVMDNSSuffix = settings .Environment .ResourceManagerVMDNSSuffix
104- c .Values ["AZURE_SUBSCRIPTION_ID" ] = strings .TrimSuffix (subscriptionID , "\n " )
105- c .Values ["AZURE_TENANT_ID" ] = strings .TrimSuffix (credentialsProvider .GetTenantID (), "\n " )
106- c .Values ["AZURE_CLIENT_ID" ] = strings .TrimSuffix (credentialsProvider .GetClientID (), "\n " )
103+ c .environmentSettings .Values ["AZURE_SUBSCRIPTION_ID" ] = strings .TrimSuffix (subscriptionID , "\n " )
104+ c .environmentSettings .Values ["AZURE_TENANT_ID" ] = strings .TrimSuffix (credentialsProvider .GetTenantID (), "\n " )
105+ c .environmentSettings .Values ["AZURE_CLIENT_ID" ] = strings .TrimSuffix (credentialsProvider .GetClientID (), "\n " )
107106
108107 clientSecret , err := credentialsProvider .GetClientSecret (ctx )
109108 if err != nil {
110109 return err
111110 }
112- c .Values ["AZURE_CLIENT_SECRET" ] = strings .TrimSuffix (clientSecret , "\n " )
111+ c .environmentSettings . Values ["AZURE_CLIENT_SECRET" ] = strings .TrimSuffix (clientSecret , "\n " )
113112
114113 c .authType = credentialsProvider .Type ()
115114
116- tokenCredential , err := credentialsProvider .GetTokenCredential (ctx , c .ResourceManagerEndpoint , c . Environment . ActiveDirectoryEndpoint , c . Environment . TokenAudience )
115+ tokenCredential , err := credentialsProvider .GetTokenCredential (ctx , c .CloudSettings )
117116 if err != nil {
118117 return err
119118 }
120119 c .TokenCredential = tokenCredential
121120 return err
122121}
123122
124- func (c * AzureClients ) getSettingsFromEnvironment (environmentName string ) ( s auth. EnvironmentSettings , err error ) {
125- s = auth. EnvironmentSettings {
126- Values : map [string ]string {} ,
123+ func (c * AzureClients ) getSettingsFromEnvironment (environmentName string ) environmentSettings {
124+ s := environmentSettings {
125+ Values : make ( map [string ]string ) ,
127126 }
127+
128128 s .Values ["AZURE_ENVIRONMENT" ] = environmentName
129129 setValue (s , "AZURE_SUBSCRIPTION_ID" )
130130 setValue (s , "AZURE_TENANT_ID" )
@@ -137,19 +137,33 @@ func (c *AzureClients) getSettingsFromEnvironment(environmentName string) (s aut
137137 setValue (s , "AZURE_PASSWORD" )
138138 setValue (s , "AZURE_AD_RESOURCE" )
139139 if v := s .Values ["AZURE_ENVIRONMENT" ]; v == "" {
140- s .Environment = azureautorest .PublicCloud
140+ s .CloudType = azure .PublicCloudName
141+ s .CloudSettings = cloud .AzurePublic
141142 } else {
142- s .Environment , err = azureautorest .EnvironmentFromName (v )
143- }
144- if s .Values ["AZURE_AD_RESOURCE" ] == "" {
145- s .Values ["AZURE_AD_RESOURCE" ] = s .Environment .ResourceManagerEndpoint
143+ s .CloudType , s .CloudSettings = getCloudEnvironment (os .Getenv ("AZURE_ENVIRONMENT" ))
146144 }
147- return
145+ return s
148146}
149147
150148// setValue adds the specified environment variable value to the Values map if it exists.
151- func setValue (settings auth. EnvironmentSettings , key string ) {
149+ func setValue (settings environmentSettings , key string ) {
152150 if v := os .Getenv (key ); v != "" {
153151 settings .Values [key ] = v
154152 }
155153}
154+
155+ func getCloudEnvironment (cloudType string ) (string , cloud.Configuration ) {
156+ cloudType = strings .ToUpper (cloudType )
157+ switch cloudType {
158+ case "AZUREPUBLICCLOUD" :
159+ return azure .PublicCloudName , cloud .AzurePublic
160+ case "AZURECLOUD" :
161+ return azure .PublicCloudName , cloud .AzurePublic
162+ case "AZURECHINACLOUD" :
163+ return azure .ChinaCloudName , cloud .AzureChina
164+ case "AZUREUSGOVERNMENT" :
165+ return azure .USGovernmentCloudName , cloud .AzureGovernment
166+ default :
167+ return azure .PublicCloudName , cloud .AzurePublic
168+ }
169+ }
0 commit comments