@@ -31,6 +31,10 @@ func main() {
31
31
opts .environment = "AZUREPUBLICCLOUD"
32
32
}
33
33
34
+ if err := createASHEnvironmentFile (opts ); err != nil {
35
+ panic (err )
36
+ }
37
+
34
38
cloudConfig , err := getCloudConfig (opts .environment )
35
39
if err != nil {
36
40
panic (err )
@@ -218,6 +222,34 @@ type configOpts struct {
218
222
federatedTokenFile string
219
223
accountKey string
220
224
environment string
225
+ // environmentFilePath and environmentFileContents are specific
226
+ // for Azure Stack Hub
227
+ environmentFilePath string
228
+ environmentFileContents string
229
+ }
230
+
231
+ func createASHEnvironmentFile (opts * configOpts ) error {
232
+ if len (opts .environmentFilePath ) == 0 || len (opts .environmentFileContents ) == 0 {
233
+ klog .Info ("Azure Stack Hub environment variables not present in current environment, skipping setup..." )
234
+ return nil
235
+ }
236
+ f , err := os .Create (opts .environmentFilePath )
237
+ if err != nil {
238
+ return err
239
+ }
240
+
241
+ _ , err = f .WriteString (opts .environmentFileContents )
242
+ if err != nil {
243
+ f .Close ()
244
+ os .Remove (f .Name ())
245
+ return err
246
+ }
247
+
248
+ err = f .Close ()
249
+ if err != nil {
250
+ return err
251
+ }
252
+ return nil
221
253
}
222
254
223
255
func getCloudConfig (environment string ) (cloud.Configuration , error ) {
@@ -238,14 +270,16 @@ func getCloudConfig(environment string) (cloud.Configuration, error) {
238
270
239
271
func getConfigOpts () * configOpts {
240
272
return & configOpts {
241
- storageAccountName : strings .TrimSpace (os .Getenv ("AZURE_STORAGE_ACCOUNT_NAME" )),
242
- containerName : strings .TrimSpace (os .Getenv ("AZURE_CONTAINER_NAME" )),
243
- clientID : strings .TrimSpace (os .Getenv ("AZURE_CLIENT_ID" )),
244
- tenantID : strings .TrimSpace (os .Getenv ("AZURE_TENANT_ID" )),
245
- clientSecret : strings .TrimSpace (os .Getenv ("AZURE_CLIENT_SECRET" )),
246
- federatedTokenFile : strings .TrimSpace (os .Getenv ("AZURE_FEDERATED_TOKEN_FILE" )),
247
- accountKey : strings .TrimSpace (os .Getenv ("AZURE_ACCOUNTKEY" )),
248
- environment : strings .TrimSpace (os .Getenv ("AZURE_ENVIRONMENT" )),
273
+ storageAccountName : strings .TrimSpace (os .Getenv ("AZURE_STORAGE_ACCOUNT_NAME" )),
274
+ containerName : strings .TrimSpace (os .Getenv ("AZURE_CONTAINER_NAME" )),
275
+ clientID : strings .TrimSpace (os .Getenv ("AZURE_CLIENT_ID" )),
276
+ tenantID : strings .TrimSpace (os .Getenv ("AZURE_TENANT_ID" )),
277
+ clientSecret : strings .TrimSpace (os .Getenv ("AZURE_CLIENT_SECRET" )),
278
+ federatedTokenFile : strings .TrimSpace (os .Getenv ("AZURE_FEDERATED_TOKEN_FILE" )),
279
+ accountKey : strings .TrimSpace (os .Getenv ("AZURE_ACCOUNTKEY" )),
280
+ environment : strings .TrimSpace (os .Getenv ("AZURE_ENVIRONMENT" )),
281
+ environmentFilePath : strings .TrimSpace (os .Getenv ("AZURE_ENVIRONMENT_FILEPATH" )),
282
+ environmentFileContents : strings .TrimSpace (os .Getenv ("AZURE_ENVIRONMENT_FILECONTENTS" )),
249
283
}
250
284
}
251
285
@@ -254,18 +288,27 @@ func getConfigOpts() *configOpts {
254
288
// this function is basically copy of what the operator itself does,
255
289
// as a way to ensure that it will work in the same way as the operator.
256
290
func getClient (cloudConfig cloud.Configuration , opts * configOpts ) (* container.Client , error ) {
291
+ env , err := azure .EnvironmentFromName (opts .environment )
292
+ if err != nil {
293
+ return nil , err
294
+ }
257
295
containerURL := fmt .Sprintf (
258
- "https://%s.blob.core.windows.net /%s" ,
296
+ "https://%s.blob.%s /%s" ,
259
297
opts .storageAccountName ,
298
+ env .StorageEndpointSuffix ,
260
299
opts .containerName ,
261
300
)
262
301
var client * container.Client
302
+ clientOpts := azcore.ClientOptions {
303
+ Cloud : cloudConfig ,
304
+ }
305
+
263
306
if len (opts .accountKey ) > 0 {
264
307
cred , err := container .NewSharedKeyCredential (opts .storageAccountName , opts .accountKey )
265
308
if err != nil {
266
309
return nil , err
267
310
}
268
- client , err = container .NewClientWithSharedKeyCredential (containerURL , cred , nil )
311
+ client , err = container .NewClientWithSharedKeyCredential (containerURL , cred , & container. ClientOptions { ClientOptions : clientOpts } )
269
312
if err != nil {
270
313
return nil , err
271
314
}
@@ -279,7 +322,7 @@ func getClient(cloudConfig cloud.Configuration, opts *configOpts) (*container.Cl
279
322
if err != nil {
280
323
return nil , err
281
324
}
282
- client , err = container .NewClient (containerURL , cred , nil )
325
+ client , err = container .NewClient (containerURL , cred , & container. ClientOptions { ClientOptions : clientOpts } )
283
326
if err != nil {
284
327
return nil , err
285
328
}
@@ -296,16 +339,21 @@ func getClient(cloudConfig cloud.Configuration, opts *configOpts) (*container.Cl
296
339
if err != nil {
297
340
return nil , err
298
341
}
299
- client , err = container .NewClient (containerURL , cred , nil )
342
+ client , err = container .NewClient (containerURL , cred , & container. ClientOptions { ClientOptions : clientOpts } )
300
343
if err != nil {
301
344
return nil , err
302
345
}
303
346
} else {
304
- cred , err := azidentity .NewDefaultAzureCredential (nil )
347
+ options := azidentity.DefaultAzureCredentialOptions {
348
+ ClientOptions : azcore.ClientOptions {
349
+ Cloud : cloudConfig ,
350
+ },
351
+ }
352
+ cred , err := azidentity .NewDefaultAzureCredential (& options )
305
353
if err != nil {
306
354
return nil , err
307
355
}
308
- client , err = container .NewClient (containerURL , cred , nil )
356
+ client , err = container .NewClient (containerURL , cred , & container. ClientOptions { ClientOptions : clientOpts } )
309
357
if err != nil {
310
358
return nil , err
311
359
}
0 commit comments