Skip to content

Commit 9143d13

Browse files
committed
WIP Remove autorest dependency
Signed-off-by: Bryan Cox <[email protected]>
1 parent 0f04d8d commit 9143d13

10 files changed

+123
-221
lines changed

azure/scope/clients.go

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3535
type 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.
4651
func (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+
}

azure/scope/cluster.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strconv"
2626
"strings"
2727

28+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
2829
asonetworkv1api20201101 "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101"
2930
asonetworkv1api20220701 "github.com/Azure/azure-service-operator/v2/api/network/v1api20220701"
3031
asoresourcesv1 "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601"
@@ -128,7 +129,7 @@ type ClusterCache struct {
128129

129130
// BaseURI returns the Azure ResourceManagerEndpoint.
130131
func (s *ClusterScope) BaseURI() string {
131-
return s.ResourceManagerEndpoint
132+
return s.AzureClients.CloudSettings.Services[cloud.ResourceManager].Endpoint
132133
}
133134

134135
// GetClient returns the controller-runtime client.
@@ -893,7 +894,7 @@ func (s *ClusterScope) GenerateFQDN(ipName string) string {
893894
return ""
894895
}
895896
hash := fmt.Sprintf("%x", h.Sum32())
896-
return strings.ToLower(fmt.Sprintf("%s-%s.%s.%s", s.ClusterName(), hash, s.Location(), s.AzureClients.ResourceManagerVMDNSSuffix))
897+
return strings.ToLower(fmt.Sprintf("%s-%s.%s", s.ClusterName(), hash, s.Location()))
897898
}
898899

899900
// GenerateLegacyFQDN generates an IP name and a fully qualified domain name, based on a hash, cluster name and cluster location.
@@ -904,7 +905,7 @@ func (s *ClusterScope) GenerateLegacyFQDN() (ip string, domain string) {
904905
return "", ""
905906
}
906907
ipName := fmt.Sprintf("%s-%x", s.ClusterName(), h.Sum32())
907-
fqdn := fmt.Sprintf("%s.%s.%s", ipName, s.Location(), s.AzureClients.ResourceManagerVMDNSSuffix)
908+
fqdn := fmt.Sprintf("%s.%s", ipName, s.Location())
908909
return ipName, fqdn
909910
}
910911

azure/scope/cluster_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
asonetworkv1api20201101 "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101"
2727
asonetworkv1api20220701 "github.com/Azure/azure-service-operator/v2/api/network/v1api20220701"
2828
asoresourcesv1 "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601"
29-
"github.com/Azure/go-autorest/autorest/azure/auth"
3029
"github.com/google/go-cmp/cmp"
3130
. "github.com/onsi/gomega"
3231
corev1 "k8s.io/api/core/v1"
@@ -56,6 +55,7 @@ import (
5655
const fakeClientID = "fake-client-id"
5756
const fakeTenantID = "fake-tenant-id"
5857
const fakeSubscriptionID = "123"
58+
const SubscriptionID = "AZURE_SUBSCRIPTION_ID"
5959

6060
func specToString(spec any) string {
6161
var sb strings.Builder
@@ -875,9 +875,9 @@ func TestNatGatewaySpecs(t *testing.T) {
875875
},
876876
},
877877
AzureClients: AzureClients{
878-
EnvironmentSettings: auth.EnvironmentSettings{
878+
environmentSettings: environmentSettings{
879879
Values: map[string]string{
880-
auth.SubscriptionID: "123",
880+
SubscriptionID: "123",
881881
},
882882
},
883883
},
@@ -949,9 +949,9 @@ func TestNatGatewaySpecs(t *testing.T) {
949949
},
950950
},
951951
AzureClients: AzureClients{
952-
EnvironmentSettings: auth.EnvironmentSettings{
952+
environmentSettings: environmentSettings{
953953
Values: map[string]string{
954-
auth.SubscriptionID: "123",
954+
SubscriptionID: "123",
955955
},
956956
},
957957
},
@@ -1041,9 +1041,9 @@ func TestNatGatewaySpecs(t *testing.T) {
10411041
},
10421042
},
10431043
AzureClients: AzureClients{
1044-
EnvironmentSettings: auth.EnvironmentSettings{
1044+
environmentSettings: environmentSettings{
10451045
Values: map[string]string{
1046-
auth.SubscriptionID: "123",
1046+
SubscriptionID: "123",
10471047
},
10481048
},
10491049
},
@@ -1341,9 +1341,9 @@ func TestSubnetSpecs(t *testing.T) {
13411341
},
13421342
},
13431343
AzureClients: AzureClients{
1344-
EnvironmentSettings: auth.EnvironmentSettings{
1344+
environmentSettings: environmentSettings{
13451345
Values: map[string]string{
1346-
auth.SubscriptionID: "123",
1346+
SubscriptionID: "123",
13471347
},
13481348
},
13491349
},
@@ -1422,9 +1422,9 @@ func TestSubnetSpecs(t *testing.T) {
14221422
},
14231423
},
14241424
AzureClients: AzureClients{
1425-
EnvironmentSettings: auth.EnvironmentSettings{
1425+
environmentSettings: environmentSettings{
14261426
Values: map[string]string{
1427-
auth.SubscriptionID: "123",
1427+
SubscriptionID: "123",
14281428
},
14291429
},
14301430
},
@@ -1718,9 +1718,9 @@ func TestAzureBastionSpec(t *testing.T) {
17181718
},
17191719
},
17201720
AzureClients: AzureClients{
1721-
EnvironmentSettings: auth.EnvironmentSettings{
1721+
environmentSettings: environmentSettings{
17221722
Values: map[string]string{
1723-
auth.SubscriptionID: "123",
1723+
SubscriptionID: "123",
17241724
},
17251725
},
17261726
},
@@ -3174,9 +3174,9 @@ func TestClusterScope_LBSpecs(t *testing.T) {
31743174
Cluster: cluster,
31753175
AzureCluster: tc.azureCluster,
31763176
AzureClients: AzureClients{
3177-
EnvironmentSettings: auth.EnvironmentSettings{
3177+
environmentSettings: environmentSettings{
31783178
Values: map[string]string{
3179-
auth.SubscriptionID: tc.azureCluster.Spec.SubscriptionID,
3179+
SubscriptionID: tc.azureCluster.Spec.SubscriptionID,
31803180
},
31813181
},
31823182
},
@@ -3496,9 +3496,9 @@ func TestVNetPeerings(t *testing.T) {
34963496
Cluster: cluster,
34973497
AzureCluster: azureCluster,
34983498
AzureClients: AzureClients{
3499-
EnvironmentSettings: auth.EnvironmentSettings{
3499+
environmentSettings: environmentSettings{
35003500
Values: map[string]string{
3501-
auth.SubscriptionID: tc.subscriptionID,
3501+
SubscriptionID: tc.subscriptionID,
35023502
},
35033503
},
35043504
},

azure/scope/identity.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type CredentialsProvider interface {
4545
GetClientID() string
4646
GetClientSecret(ctx context.Context) (string, error)
4747
GetTenantID() string
48-
GetTokenCredential(ctx context.Context, resourceManagerEndpoint, activeDirectoryEndpoint, tokenAudience string) (azcore.TokenCredential, error)
48+
GetTokenCredential(ctx context.Context, cloudConfig cloud.Configuration) (azcore.TokenCredential, error)
4949
Type() infrav1.IdentityType
5050
}
5151

@@ -82,7 +82,7 @@ func NewAzureCredentialsProvider(ctx context.Context, cache azure.CredentialCach
8282
}
8383

8484
// GetTokenCredential returns an Azure TokenCredential based on the provided azure identity.
85-
func (p *AzureCredentialsProvider) GetTokenCredential(ctx context.Context, resourceManagerEndpoint, activeDirectoryEndpoint, tokenAudience string) (azcore.TokenCredential, error) {
85+
func (p *AzureCredentialsProvider) GetTokenCredential(ctx context.Context, cloudConfig cloud.Configuration) (azcore.TokenCredential, error) {
8686
ctx, log, done := tele.StartSpanWithLogger(ctx, "azure.scope.AzureCredentialsProvider.GetTokenCredential")
8787
defer done()
8888

@@ -117,15 +117,7 @@ func (p *AzureCredentialsProvider) GetTokenCredential(ctx context.Context, resou
117117
options := azidentity.ClientSecretCredentialOptions{
118118
ClientOptions: azcore.ClientOptions{
119119
TracingProvider: tracingProvider,
120-
Cloud: cloud.Configuration{
121-
ActiveDirectoryAuthorityHost: activeDirectoryEndpoint,
122-
Services: map[cloud.ServiceName]cloud.ServiceConfiguration{
123-
cloud.ResourceManager: {
124-
Audience: tokenAudience,
125-
Endpoint: resourceManagerEndpoint,
126-
},
127-
},
128-
},
120+
Cloud: cloudConfig,
129121
},
130122
}
131123
cred, authErr = p.cache.GetOrStoreClientSecret(p.GetTenantID(), p.Identity.Spec.ClientID, clientSecret, &options)

azure/scope/identity_test.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,7 @@ func TestGetTokenCredential(t *testing.T) {
277277
cacheExpect: func(cache *mock_azure.MockCredentialCache) {
278278
cache.EXPECT().GetOrStoreClientSecret(fakeTenantID, fakeClientID, "fooSecret", gomock.Cond(func(opts *azidentity.ClientSecretCredentialOptions) bool {
279279
// ignore tracing provider
280-
return reflect.DeepEqual(opts.ClientOptions.Cloud, cloud.Configuration{
281-
ActiveDirectoryAuthorityHost: "https://login.microsoftonline.com",
282-
Services: map[cloud.ServiceName]cloud.ServiceConfiguration{
283-
cloud.ResourceManager: {
284-
Audience: "",
285-
Endpoint: "",
286-
},
287-
},
288-
})
280+
return reflect.DeepEqual(opts.ClientOptions.Cloud, cloud.AzurePublic)
289281
}))
290282
},
291283
},
@@ -322,15 +314,7 @@ func TestGetTokenCredential(t *testing.T) {
322314
cacheExpect: func(cache *mock_azure.MockCredentialCache) {
323315
cache.EXPECT().GetOrStoreClientSecret(fakeTenantID, fakeClientID, "fooSecret", gomock.Cond(func(opts *azidentity.ClientSecretCredentialOptions) bool {
324316
// ignore tracing provider
325-
return reflect.DeepEqual(opts.ClientOptions.Cloud, cloud.Configuration{
326-
ActiveDirectoryAuthorityHost: "https://login.microsoftonline.com",
327-
Services: map[cloud.ServiceName]cloud.ServiceConfiguration{
328-
cloud.ResourceManager: {
329-
Audience: "",
330-
Endpoint: "",
331-
},
332-
},
333-
})
317+
return reflect.DeepEqual(opts.ClientOptions.Cloud, cloud.AzurePublic)
334318
}))
335319
},
336320
},
@@ -443,7 +427,7 @@ func TestGetTokenCredential(t *testing.T) {
443427

444428
provider, err := NewAzureCredentialsProvider(context.Background(), cache, fakeClient, tt.cluster.Spec.IdentityRef, "")
445429
g.Expect(err).NotTo(HaveOccurred())
446-
_, err = provider.GetTokenCredential(context.Background(), "", tt.ActiveDirectoryAuthorityHost, "")
430+
_, err = provider.GetTokenCredential(context.Background(), cloud.AzurePublic)
447431
g.Expect(err).NotTo(HaveOccurred())
448432
})
449433
}

0 commit comments

Comments
 (0)