Skip to content

Commit e00d493

Browse files
yinghsugnNickcandyBethanyZhou
authored
[Az.ConnectedKubernetes] support proxy environments and fix issues (from generation) (#20955)
* OOB of Az.ConnectedKubernetes (#20904) * Fix/connectedk8s ps5 (#20603) * Create RSAHelper.ps1 * Update New-AzConnectedKubernetes.ps1 * Add try-catch and update function parameters * Update New-AzConnectedKubernetes.ps1 * [Az.ConnectedKubernetes] Update ChangeLog (#20649) * Update RSAHelper.ps1 (coding style) * Update ChangeLog.md * upgrade Az.ConnectedKubernetes from 0.7.0 to 0.7.1 --------- Co-authored-by: Ann Lu <112682982+yinghsugn@users.noreply.github.com> * Fix Invoke-WebRequest in Win10 issue * Add optional settings (+2 squashed commit) Squashed commit: [37a75ce5980] Add optional settings [f90cd1a98ee] Add optional settings * Install azure-arc helm release in azure-arc-release namespace (+1 squashed commits) Squashed commits: [49e9e01caf3] Install azure-arc helm release in azure-arc-release namespace (+1 squashed commits) Squashed commits: [9662ecff989] Install azure-arc helm release in azure-arc-release namespace * Fix RSAHelper format * Add Proxy settings (+1 squashed commits) Squashed commits: [28ff1d7f8cf] Add Proxy settings (+4 squashed commit) Squashed commit: [cdfe615ad63] Update warning messages [5d46ec26806] Check credential [db0351e75f5] Update parameter description [79bf2b15c29] Modify parameter type * Fix Invalid URI issue when passing diaplsy name of location (+1 squashed commits) Squashed commits: [c893de7a1b5] Fix Invalid URI issue when passing diaplsy name of location (+1 squashed commits) Squashed commits: [bc81f1915fb] Fix Invalid URI issue when passing diaplsy name of location * Update examples * Update parameter names and docs * Move RSAHelper to custom * Update docs * Update src/ConnectedKubernetes/custom/New-AzConnectedKubernetes.ps1 Co-authored-by: Beisi Zhou <zhoubeisi@gmail.com> * Recover autorest (autorest --reset) and version in tools folder * Recover change in tools folder * Remove require * check Az.Resources * Update src/ConnectedKubernetes/custom/New-AzConnectedKubernetes.ps1 --------- Co-authored-by: NanxiangLiu <33285578+Nickcandy@users.noreply.github.com> Co-authored-by: Beisi Zhou <zhoubeisi@gmail.com>
1 parent 4fad738 commit e00d493

File tree

9 files changed

+518
-26
lines changed

9 files changed

+518
-26
lines changed

src/ConnectedKubernetes/custom/New-AzConnectedKubernetes.ps1

Lines changed: 159 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,55 @@ function New-AzConnectedKubernetes {
5454
# The ID of the target subscription.
5555
${SubscriptionId},
5656

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+
57100
[Parameter(HelpMessage="Path to the kube config file")]
58101
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category('Body')]
59102
[System.String]
60103
# Path to the kube config file
61104
${KubeConfig},
62-
105+
63106
[Parameter(HelpMessage="Kubconfig context from current machine")]
64107
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category('Body')]
65108
[System.String]
@@ -172,14 +215,14 @@ function New-AzConnectedKubernetes {
172215
[Parameter(DontShow)]
173216
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category('Runtime')]
174217
[System.Uri]
175-
# The URI for the proxy server to use
218+
# The URI of the proxy server for host os to use
176219
${Proxy},
177220

178221
[Parameter(DontShow)]
179222
[ValidateNotNull()]
180223
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Category('Runtime')]
181224
[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
183226
${ProxyCredential},
184227

185228
[Parameter(DontShow)]
@@ -224,7 +267,7 @@ function New-AzConnectedKubernetes {
224267
if ($PSBoundParameters.ContainsKey('KubeContext')) {
225268
$Null = $PSBoundParameters.Remove('KubeContext')
226269
}
227-
if (($KubeContext -eq $null) -or ($KubeContext -eq '')) {
270+
if (($null -eq $KubeContext) -or ($KubeContext -eq '')) {
228271
$KubeContext = kubectl config current-context
229272
}
230273

@@ -260,9 +303,10 @@ function New-AzConnectedKubernetes {
260303
#EndRegion
261304

262305
#Region get release namespace
306+
Set-Variable ReleaseInstallNamespace -option Constant -value "azure-arc-release"
263307
$ReleaseNamespace = $null
264308
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
266310
} catch {
267311
Write-Error "Fail to find the namespace for azure-arc."
268312
}
@@ -292,7 +336,12 @@ function New-AzConnectedKubernetes {
292336
$HelmRepoUrl = Get-ChildItem -Path Env:HELMREPOURL
293337
helm repo add $HelmRepoName $HelmRepoUrl --kubeconfig $KubeConfig --kube-context $KubeContext
294338
}
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+
}
296345
if (Test-Path Env:HELMREGISTRY) {
297346
$RegisteryPath = Get-ChildItem -Path Env:HELMREGISTRY
298347
} else {
@@ -301,7 +350,15 @@ function New-AzConnectedKubernetes {
301350
$ReleaseTrain = Get-ChildItem -Path Env:RELEASETRAIN
302351
} else {
303352
$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+
}
305362
$ChartLocationUrl = "https://${Location}.dp.kubernetesconfiguration.azure.com/azure-arc-k8sagents/GetLatestHelmPackagePath?api-version=2019-11-01-preview&releaseTrain=${ReleaseTrain}"
306363

307364
$Uri = [System.Uri]::New($ChartLocationUrl)
@@ -315,21 +372,20 @@ function New-AzConnectedKubernetes {
315372
$HeaderParameter = @{
316373
"Authorization" = "Bearer $AccessToken"
317374
}
318-
$Response = Invoke-WebRequest -Uri $Uri -Headers $HeaderParameter -Method Post
375+
$Response = Invoke-WebRequest -Uri $Uri -Headers $HeaderParameter -Method Post -UseBasicParsing
319376
if ($Response.StatusCode -eq 200) {
320377
$RegisteryPath = ($Response.Content | ConvertFrom-Json).repositoryPath
321378
} 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+
324381
}
325382
}
326383
Set-Item -Path Env:HELM_EXPERIMENTAL_OCI -Value 1
327384
#Region pull helm chart
328385
try {
329386
helm chart pull $RegisteryPath --kubeconfig $KubeConfig --kube-context $KubeContext
330387
} 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"
333389
}
334390
#Endregion
335391

@@ -349,21 +405,107 @@ function New-AzConnectedKubernetes {
349405
#Endregion
350406

351407
$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+
355422
$HelmChartPath = Join-Path -Path $ChartExportPath -ChildPath 'azure-arc-k8sagents'
356423
if (Test-Path Env:HELMCHART) {
357424
$ChartPath = Get-ChildItem -Path Env:HELMCHART
358425
} else {
359426
$ChartPath = $HelmChartPath
360427
}
361428

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+
362504
$PSBoundParameters.Add('AgentPublicKeyCertificate', $AgentPublicKey)
363505
$Response = Az.ConnectedKubernetes.internal\New-AzConnectedKubernetes @PSBoundParameters
364506

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)
367509
Return $Response
368510
}
369511
}

src/ConnectedKubernetes/custom/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ These provide functionality to our HTTP pipeline and other useful features. In s
3232
### Attributes
3333
For processing the cmdlets, we've created some additional attributes:
3434
- `Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.DescriptionAttribute`
35-
- Used in C# cmdlets to provide a high-level description of the cmdlet. This is propagated to reference documentation via [help comments](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_comment_based_help) in the exported scripts.
35+
- Used in C# cmdlets to provide a high-level description of the cmdlet. This is propagated to reference documentation via [help comments](https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_comment_based_help) in the exported scripts.
3636
- `Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.DoNotExportAttribute`
3737
- Used in C# and script cmdlets to suppress creating an exported cmdlet at build-time. These cmdlets will *not be exposed* by `Az.ConnectedKubernetes`.
3838
- `Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.InternalExportAttribute`

0 commit comments

Comments
 (0)