@@ -19,49 +19,69 @@ package azure
19
19
import (
20
20
"context"
21
21
"fmt"
22
- "log"
23
22
"os"
24
- "strings"
25
23
"time"
26
24
27
- "github.com/Azure/azure-sdk-for-go/sdk/azcore"
28
- "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud "
29
- "github.com/Azure/azure-sdk-for-go/sdk/azidentity "
30
- "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/ resources"
31
- "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage "
32
- "github.com/Azure/go-autorest/autorest/azure "
33
- "github.com/jongio/azidext/go/azidext "
25
+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm "
26
+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy "
27
+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/to "
28
+ resources "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/ resources/armresources "
29
+ "sigs.k8s.io/cloud-provider-azure/pkg/azclient "
30
+ "sigs.k8s.io/cloud-provider-azure/pkg/azclient/accountclient "
31
+ "sigs.k8s.io/cloud-provider-azure/pkg/azclient/resourcegroupclient "
34
32
)
35
33
36
34
type Client struct {
37
- environment azure.Environment
38
35
subscriptionID string
39
- groupsClient resources. GroupsClient
40
- accountsClient storage. AccountsClient
36
+ groupsClient resourcegroupclient. Interface
37
+ accountsClient accountclient. Interface
41
38
}
42
39
43
40
func GetClient (cloud , subscriptionID , clientID , tenantID , clientSecret string ) (* Client , error ) {
44
- env , err := azure .EnvironmentFromName (cloud )
41
+ armConfig := & azclient.ARMClientConfig {
42
+ Cloud : cloud ,
43
+ }
44
+ cloudConfig , err := azclient .GetAzureCloudConfig (armConfig )
45
45
if err != nil {
46
46
return nil , err
47
47
}
48
-
49
- options := azidentity.ClientSecretCredentialOptions {
50
- ClientOptions : azcore.ClientOptions {
51
- Cloud : getCloudConfig (env ),
48
+ credProvider , err := azclient .NewAuthProvider (azclient.AzureAuthConfig {
49
+ TenantID : tenantID ,
50
+ AADClientID : clientID ,
51
+ AADClientSecret : clientSecret ,
52
+ }, & arm.ClientOptions {
53
+ AuxiliaryTenants : []string {tenantID },
54
+ ClientOptions : policy.ClientOptions {
55
+ Cloud : * cloudConfig ,
52
56
},
57
+ })
58
+ if err != nil {
59
+ return nil , err
53
60
}
54
- cred , err := azidentity .NewClientSecretCredential (tenantID , clientID , clientSecret , & options )
61
+ cred , err := credProvider .GetAzIdentity ()
62
+ if err != nil {
63
+ return nil , err
64
+ }
65
+ factory , err := azclient .NewClientFactory (& azclient.ClientFactoryConfig {
66
+ SubscriptionID : subscriptionID ,
67
+ }, armConfig , cred )
55
68
if err != nil {
56
69
return nil , err
57
70
}
58
71
59
- return getClient (env , subscriptionID , cred , env .TokenAudience ), nil
72
+ return & Client {
73
+ subscriptionID : subscriptionID ,
74
+ groupsClient : factory .GetResourceGroupClient (),
75
+ accountsClient : factory .GetAccountClient (),
76
+ }, nil
60
77
}
61
78
62
- func (az * Client ) EnsureResourceGroup (ctx context.Context , name , location string , managedBy * string ) (resourceGroup * resources.Group , err error ) {
79
+ func (az * Client ) EnsureResourceGroup (ctx context.Context , name , location string , managedBy * string ) (resourceGroup * resources.ResourceGroup , err error ) {
63
80
var tags map [string ]* string
64
81
group , err := az .groupsClient .Get (ctx , name )
82
+ if err != nil {
83
+ group = & resources.ResourceGroup {}
84
+ }
65
85
if err == nil && group .Tags != nil {
66
86
tags = group .Tags
67
87
} else {
@@ -71,92 +91,38 @@ func (az *Client) EnsureResourceGroup(ctx context.Context, name, location string
71
91
managedBy = group .ManagedBy
72
92
}
73
93
// Tags for correlating resource groups with prow jobs on testgrid
74
- tags ["buildID" ] = stringPointer (os .Getenv ("BUILD_ID" ))
75
- tags ["jobName" ] = stringPointer (os .Getenv ("JOB_NAME" ))
76
- tags ["creationTimestamp" ] = stringPointer (time .Now ().UTC ().Format (time .RFC3339 ))
94
+ tags ["buildID" ] = to . Ptr (os .Getenv ("BUILD_ID" ))
95
+ tags ["jobName" ] = to . Ptr (os .Getenv ("JOB_NAME" ))
96
+ tags ["creationTimestamp" ] = to . Ptr (time .Now ().UTC ().Format (time .RFC3339 ))
77
97
78
- response , err := az .groupsClient .CreateOrUpdate (ctx , name , resources.Group {
98
+ response , err := az .groupsClient .CreateOrUpdate (ctx , name , resources.ResourceGroup {
79
99
Name : & name ,
80
100
Location : & location ,
81
101
ManagedBy : managedBy ,
82
102
Tags : tags ,
83
103
})
84
104
if err != nil {
85
- return & response , err
105
+ return response , err
86
106
}
87
107
88
- return & response , nil
108
+ return response , nil
89
109
}
90
110
91
111
func (az * Client ) DeleteResourceGroup (ctx context.Context , groupName string ) error {
92
112
_ , err := az .groupsClient .Get (ctx , groupName )
93
113
if err == nil {
94
- future , err := az .groupsClient .Delete (ctx , groupName )
114
+ err := az .groupsClient .Delete (ctx , groupName )
95
115
if err != nil {
96
116
return fmt .Errorf ("cannot delete resource group %v: %w" , groupName , err )
97
117
}
98
- err = future .WaitForCompletionRef (ctx , az .groupsClient .Client )
99
- if err != nil {
100
- // Skip the teardown errors because of https://github.com/Azure/go-autorest/issues/357
101
- // TODO(feiskyer): fix the issue by upgrading go-autorest version >= v11.3.2.
102
- log .Printf ("Warning: failed to delete resource group %q with error %v" , groupName , err )
103
- }
104
118
}
105
119
return nil
106
120
}
107
121
108
122
func (az * Client ) GetAccountNumByResourceGroup (ctx context.Context , groupName string ) (count int , err error ) {
109
- result , err := az .accountsClient .ListByResourceGroup (ctx , groupName )
123
+ result , err := az .accountsClient .List (ctx , groupName )
110
124
if err != nil {
111
125
return - 1 , err
112
126
}
113
- return len (result .Values ()), nil
114
- }
115
-
116
- func getCloudConfig (env azure.Environment ) cloud.Configuration {
117
- switch env .Name {
118
- case azure .USGovernmentCloud .Name :
119
- return cloud .AzureGovernment
120
- case azure .ChinaCloud .Name :
121
- return cloud .AzureChina
122
- case azure .PublicCloud .Name :
123
- return cloud .AzurePublic
124
- default :
125
- return cloud.Configuration {
126
- ActiveDirectoryAuthorityHost : env .ActiveDirectoryEndpoint ,
127
- Services : map [cloud.ServiceName ]cloud.ServiceConfiguration {
128
- cloud .ResourceManager : {
129
- Audience : env .TokenAudience ,
130
- Endpoint : env .ResourceManagerEndpoint ,
131
- },
132
- },
133
- }
134
- }
135
- }
136
-
137
- func getClient (env azure.Environment , subscriptionID string , cred * azidentity.ClientSecretCredential , scope string ) * Client {
138
- c := & Client {
139
- environment : env ,
140
- subscriptionID : subscriptionID ,
141
- groupsClient : resources .NewGroupsClientWithBaseURI (env .ResourceManagerEndpoint , subscriptionID ),
142
- accountsClient : storage .NewAccountsClient (subscriptionID ),
143
- }
144
-
145
- if ! strings .HasSuffix (scope , "/.default" ) {
146
- scope += "/.default"
147
- }
148
- // Use an adapter so azidentity in the Azure SDK can be used as Authorizer
149
- // when calling the Azure Management Packages, which we currently use. Once
150
- // the Azure SDK clients (found in /sdk) move to stable, we can update our
151
- // clients and they will be able to use the creds directly without the
152
- // authorizer.
153
- authorizer := azidext .NewTokenCredentialAdapter (cred , []string {scope })
154
- c .groupsClient .Authorizer = authorizer
155
- c .accountsClient .Authorizer = authorizer
156
-
157
- return c
158
- }
159
-
160
- func stringPointer (s string ) * string {
161
- return & s
127
+ return len (result ), nil
162
128
}
0 commit comments