@@ -54,12 +54,55 @@ function New-AzConnectedKubernetes {
54
54
# The ID of the target subscription.
55
55
${SubscriptionId} ,
56
56
57
+ [Parameter ()]
58
+ [Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Path' )]
59
+ [System.Uri ]
60
+ # The http URI of the proxy server for the kubernetes cluster to use
61
+ ${HttpProxy} ,
62
+
63
+ [Parameter ()]
64
+ [Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Path' )]
65
+ [System.Uri ]
66
+ # The https URI of the proxy server for the kubernetes cluster to use
67
+ ${HttpsProxy} ,
68
+
69
+ [Parameter ()]
70
+ [Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Path' )]
71
+ [System.String ]
72
+ # The comma-separated list of hostnames that should be excluded from the proxy server for the kubernetes cluster to use
73
+ ${NoProxy} ,
74
+
75
+ [Parameter ()]
76
+ [Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Path' )]
77
+ [System.String ]
78
+ # The path to the certificate file for proxy or custom Certificate Authority.
79
+ ${ProxyCert} ,
80
+
81
+ [Parameter ()]
82
+ [Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Path' )]
83
+ [ValidateRange (0 , 3600 )]
84
+ [Int ]
85
+ # The time required (in seconds) for the arc-agent pods to be installed on the kubernetes cluster.
86
+ ${OnboardingTimeout} = 600 ,
87
+
88
+ [Parameter ()]
89
+ [Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Path' )]
90
+ [System.Management.Automation.SwitchParameter ]
91
+ # Flag to disable auto upgrade of arc agents.
92
+ ${DisableAutoUpgrade} ,
93
+
94
+ [Parameter ()]
95
+ [Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Path' )]
96
+ [System.String ]
97
+ # Override the default container log path to enable fluent-bit logging.
98
+ ${ContainerLogPath} ,
99
+
57
100
[Parameter (HelpMessage = " Path to the kube config file" )]
58
101
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Body' )]
59
102
[System.String ]
60
103
# Path to the kube config file
61
104
${KubeConfig} ,
62
-
105
+
63
106
[Parameter (HelpMessage = " Kubconfig context from current machine" )]
64
107
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Body' )]
65
108
[System.String ]
@@ -172,14 +215,14 @@ function New-AzConnectedKubernetes {
172
215
[Parameter (DontShow)]
173
216
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Runtime' )]
174
217
[System.Uri ]
175
- # The URI for the proxy server to use
218
+ # The URI of the proxy server for host os to use
176
219
${Proxy} ,
177
220
178
221
[Parameter (DontShow)]
179
222
[ValidateNotNull ()]
180
223
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category (' Runtime' )]
181
224
[System.Management.Automation.PSCredential ]
182
- # Credentials for a proxy server to use for the remote call
225
+ # The credential of the proxy server for host os to use
183
226
${ProxyCredential} ,
184
227
185
228
[Parameter (DontShow)]
@@ -224,7 +267,7 @@ function New-AzConnectedKubernetes {
224
267
if ($PSBoundParameters.ContainsKey (' KubeContext' )) {
225
268
$Null = $PSBoundParameters.Remove (' KubeContext' )
226
269
}
227
- if (($KubeContext -eq $null ) -or ($KubeContext -eq ' ' )) {
270
+ if (($null -eq $KubeContext ) -or ($KubeContext -eq ' ' )) {
228
271
$KubeContext = kubectl config current- context
229
272
}
230
273
@@ -260,9 +303,10 @@ function New-AzConnectedKubernetes {
260
303
# EndRegion
261
304
262
305
# Region get release namespace
306
+ Set-Variable ReleaseInstallNamespace - option Constant - value " azure-arc-release"
263
307
$ReleaseNamespace = $null
264
308
try {
265
- $ReleaseNamespace = (helm status azure- arc - o json -- kubeconfig $KubeConfig -- kube- context $KubeContext | ConvertFrom-Json ).namespace
309
+ $ReleaseNamespace = (helm status azure- arc - o json -- kubeconfig $KubeConfig -- kube- context $KubeContext - n $ReleaseInstallNamespace | ConvertFrom-Json ).namespace
266
310
} catch {
267
311
Write-Error " Fail to find the namespace for azure-arc."
268
312
}
@@ -292,7 +336,12 @@ function New-AzConnectedKubernetes {
292
336
$HelmRepoUrl = Get-ChildItem - Path Env:HELMREPOURL
293
337
helm repo add $HelmRepoName $HelmRepoUrl -- kubeconfig $KubeConfig -- kube- context $KubeContext
294
338
}
295
-
339
+
340
+ $resources = Get-Module Az.Resources - ListAvailable
341
+ if ($null -eq $resources ) {
342
+ Write-Error " Missing required module(s): Az.Resources. Please run 'Install-Module Az.Resources -Repository PSGallery' to install Az.Resources."
343
+ return
344
+ }
296
345
if (Test-Path Env:HELMREGISTRY) {
297
346
$RegisteryPath = Get-ChildItem - Path Env:HELMREGISTRY
298
347
} else {
@@ -301,7 +350,15 @@ function New-AzConnectedKubernetes {
301
350
$ReleaseTrain = Get-ChildItem - Path Env:RELEASETRAIN
302
351
} else {
303
352
$ReleaseTrain = ' stable'
304
- }
353
+ }
354
+ $AzLocation = Get-AzLocation | Where-Object { ($_.DisplayName -ieq $Location ) -or ($_.Location -ieq $Location )}
355
+ $Region = $AzLocation.Location
356
+ if ($null -eq $Region ) {
357
+ Write-Error " Invalid location: $Location "
358
+ return
359
+ } else {
360
+ $Location = $Region
361
+ }
305
362
$ChartLocationUrl = " https://${Location} .dp.kubernetesconfiguration.azure.com/azure-arc-k8sagents/GetLatestHelmPackagePath?api-version=2019-11-01-preview&releaseTrain=${ReleaseTrain} "
306
363
307
364
$Uri = [System.Uri ]::New($ChartLocationUrl )
@@ -315,21 +372,20 @@ function New-AzConnectedKubernetes {
315
372
$HeaderParameter = @ {
316
373
" Authorization" = " Bearer $AccessToken "
317
374
}
318
- $Response = Invoke-WebRequest - Uri $Uri - Headers $HeaderParameter - Method Post
375
+ $Response = Invoke-WebRequest - Uri $Uri - Headers $HeaderParameter - Method Post - UseBasicParsing
319
376
if ($Response.StatusCode -eq 200 ) {
320
377
$RegisteryPath = ($Response.Content | ConvertFrom-Json ).repositoryPath
321
378
} else {
322
- Write-Error " Error while fetching helm chart registry path: ${$Response.RawContent} "
323
- throw
379
+ throw " Error while fetching helm chart registry path: ${$Response.RawContent} "
380
+
324
381
}
325
382
}
326
383
Set-Item - Path Env:HELM_EXPERIMENTAL_OCI - Value 1
327
384
# Region pull helm chart
328
385
try {
329
386
helm chart pull $RegisteryPath -- kubeconfig $KubeConfig -- kube- context $KubeContext
330
387
} catch {
331
- Write-Error " Unable to pull helm chart from the registery $RegisteryPath "
332
- throw
388
+ throw " Unable to pull helm chart from the registery $RegisteryPath "
333
389
}
334
390
# Endregion
335
391
@@ -349,21 +405,107 @@ function New-AzConnectedKubernetes {
349
405
# Endregion
350
406
351
407
$RSA = [System.Security.Cryptography.RSA ]::Create(4096 )
352
- $AgentPublicKey = [System.Convert ]::ToBase64String($RSA.ExportRSAPublicKey ())
353
- $AgentPrivateKey = " -----BEGIN RSA PRIVATE KEY-----`n " + [System.Convert ]::ToBase64String($RSA.ExportRSAPrivateKey ()) + " `n -----END RSA PRIVATE KEY-----"
354
-
408
+ if ($PSVersionTable.PSVersion.Major -eq 5 ) {
409
+ try {
410
+ . " $PSScriptRoot /RSAHelper.ps1"
411
+ $AgentPublicKey = ExportRSAPublicKeyBase64($RSA )
412
+ $AgentPrivateKey = ExportRSAPrivateKeyBase64($RSA )
413
+ $AgentPrivateKey = " -----BEGIN RSA PRIVATE KEY-----`n " + $AgentPrivateKey + " `n -----END RSA PRIVATE KEY-----"
414
+ } catch {
415
+ throw " Unable to generate RSA keys"
416
+ }
417
+ } else {
418
+ $AgentPublicKey = [System.Convert ]::ToBase64String($RSA.ExportRSAPublicKey ())
419
+ $AgentPrivateKey = " -----BEGIN RSA PRIVATE KEY-----`n " + [System.Convert ]::ToBase64String($RSA.ExportRSAPrivateKey ()) + " `n -----END RSA PRIVATE KEY-----"
420
+ }
421
+
355
422
$HelmChartPath = Join-Path - Path $ChartExportPath - ChildPath ' azure-arc-k8sagents'
356
423
if (Test-Path Env:HELMCHART) {
357
424
$ChartPath = Get-ChildItem - Path Env:HELMCHART
358
425
} else {
359
426
$ChartPath = $HelmChartPath
360
427
}
361
428
429
+ # Region helm options
430
+ $options = " "
431
+ $proxyEnableState = $false
432
+ if (-not ([string ]::IsNullOrEmpty($HttpProxy ))) {
433
+ $HttpProxyStr = $HttpProxy.ToString ()
434
+ $HttpProxyStr = $HttpProxyStr -replace ' ,' , ' \,'
435
+ $HttpProxyStr = $HttpProxyStr -replace ' /' , ' \/'
436
+ $options += " --set global.httpProxy=$HttpProxyStr "
437
+ $proxyEnableState = $true
438
+ $Null = $PSBoundParameters.Remove (' HttpProxy' )
439
+ }
440
+ if (-not ([string ]::IsNullOrEmpty($HttpsProxy ))) {
441
+ $HttpsProxyStr = $HttpsProxy.ToString ()
442
+ $HttpsProxyStr = $HttpsProxyStr -replace ' ,' , ' \,'
443
+ $HttpsProxyStr = $HttpsProxyStr -replace ' /' , ' \/'
444
+ $options += " --set global.httpsProxy=$HttpsProxyStr "
445
+ $proxyEnableState = $true
446
+ $Null = $PSBoundParameters.Remove (' HttpsProxy' )
447
+ }
448
+ if (-not ([string ]::IsNullOrEmpty($NoProxy ))) {
449
+ $NoProxy = $NoProxy -replace ' ,' , ' \,'
450
+ $NoProxy = $NoProxy -replace ' /' , ' \/'
451
+ $options += " --set global.noProxy=$NoProxy "
452
+ $proxyEnableState = $true
453
+ $Null = $PSBoundParameters.Remove (' NoProxy' )
454
+ }
455
+ if ($proxyEnableState ) {
456
+ $options += " --set global.isProxyEnabled=true"
457
+ }
458
+ try {
459
+ if ((-not ([string ]::IsNullOrEmpty($ProxyCert ))) -and (Test-Path $ProxyCert )) {
460
+ $options += " --set-file global.proxyCert=$ProxyCert "
461
+ $options += " --set global.isCustomCert=true"
462
+ }
463
+ } catch {
464
+ throw " Unable to find ProxyCert from file path"
465
+ }
466
+ if ($DisableAutoUpgrade ) {
467
+ $options += " --set systemDefaultValues.azureArcAgents.autoUpdate=false"
468
+ $Null = $PSBoundParameters.Remove (' DisableAutoUpgrade' )
469
+ }
470
+ if (-not ([string ]::IsNullOrEmpty($ContainerLogPath ))) {
471
+ $options += " --set systemDefaultValues.fluent-bit.containerLogPath=$ContainerLogPath "
472
+ $Null = $PSBoundParameters.Remove (' ContainerLogPath' )
473
+ }
474
+ if (-not ([string ]::IsNullOrEmpty($KubeConfig ))) {
475
+ $options += " --kubeconfig $KubeConfig "
476
+ }
477
+ if (-not ([string ]::IsNullOrEmpty($KubeContext ))) {
478
+ $options += " --kube-context $KubeContext "
479
+ }
480
+ if (! $NoWait ) {
481
+ $options += " --wait --timeout $OnboardingTimeout "
482
+ $options += " s"
483
+ }
484
+ # Endregion
485
+ if ($PSBoundParameters.ContainsKey (' OnboardingTimeout' )) {
486
+ $PSBoundParameters.Remove (' OnboardingTimeout' )
487
+ }
488
+ if ((-not ([string ]::IsNullOrEmpty($Proxy ))) -and (-not $PSBoundParameters.ContainsKey (' ProxyCredential' ))) {
489
+ if (-not ([string ]::IsNullOrEmpty($Proxy.UserInfo ))) {
490
+ try {
491
+ $userInfo = $Proxy.UserInfo -Split ' :'
492
+ $pass = ConvertTo-SecureString $userInfo [1 ] - AsPlainText - Force
493
+ $ProxyCredential = New-Object System.Management.Automation.PSCredential ($userInfo [0 ] , $pass )
494
+ $PSBoundParameters.Add (' ProxyCredential' , $ProxyCredential )
495
+ } catch {
496
+ Write-Warning " Please set ProxyCredential or provide username and password in the Proxy parameter"
497
+ throw
498
+ }
499
+ } else {
500
+ Write-Warning " If the proxy is a private proxy, pass ProxyCredential parameter or provide username and password in the Proxy parameter"
501
+ }
502
+ }
503
+
362
504
$PSBoundParameters.Add (' AgentPublicKeyCertificate' , $AgentPublicKey )
363
505
$Response = Az.ConnectedKubernetes.internal\New-AzConnectedKubernetes @PSBoundParameters
364
506
365
- $TenantId = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider ]::Instance.Profile.DefaultContext.Tenant.Id
366
- helm upgrade -- install azure- arc $ChartPath -- set global.subscriptionId= $SubscriptionId -- set global.resourceGroupName= $ResourceGroupName -- set global.resourceName= $ClusterName -- set global.tenantId= $TenantId -- set global.location= $Location -- set global.onboardingPrivateKey= $AgentPrivateKey -- set systemDefaultValues.spnOnboarding= false -- set global.azureEnvironment= AZUREPUBLICCLOUD -- set systemDefaultValues.clusterconnect- agent.enabled= true -- set global.kubernetesDistro= $Distribution -- set global.kubernetesInfra= $Infrastructure -- kubeconfig $KubeConfig -- kube - context $KubeContext -- wait -- timeout 600s
507
+ $TenantId = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider ]::Instance.Profile.DefaultContext.Tenant.Id
508
+ helm upgrade -- install azure- arc $ChartPath -- namespace $ReleaseInstallNamespace -- create - namespace -- set global.subscriptionId= $SubscriptionId -- set global.resourceGroupName= $ResourceGroupName -- set global.resourceName= $ClusterName -- set global.tenantId= $TenantId -- set global.location= $Location -- set global.onboardingPrivateKey= $AgentPrivateKey -- set systemDefaultValues.spnOnboarding= false -- set global.azureEnvironment= AZUREPUBLICCLOUD -- set systemDefaultValues.clusterconnect- agent.enabled= true -- set global.kubernetesDistro= $Distribution -- set global.kubernetesInfra= $Infrastructure ( -split $options )
367
509
Return $Response
368
510
}
369
511
}
0 commit comments