Skip to content

Commit 9db4c3b

Browse files
authored
[Az.ConnectedKubernetes] support proxy environments and fix issues (#20944)
* 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 ChangeLog * Update parameter names and docs
1 parent 9e02753 commit 9db4c3b

12 files changed

+1798
-41
lines changed

src/ConnectedKubernetes/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Added optional configs (-HttpProxy, -HttpsProxy, -NoProxy, -ProxyCert) for connection behind outbound proxy server.
22+
* Added optional configs (-ContainerLogPath, -DisableAutoUpgrade, -NoWait, -OnboardingTimeout).
23+
* Fixed invalid URI issue with display name of location.
24+
* Fixed response can't be parsed issue with UseBasicParsing.
2125

2226
## Version 0.7.1
2327
* Made `New-AzConnectedKubernetes` support PowerShell 5.

src/ConnectedKubernetes/custom/New-AzConnectedKubernetes.ps1

Lines changed: 140 additions & 10 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
}
@@ -302,6 +346,15 @@ function New-AzConnectedKubernetes {
302346
} else {
303347
$ReleaseTrain = 'stable'
304348
}
349+
350+
$AzLocation = Get-AzLocation | Where-Object { ($_.DisplayName -ieq $Location) -or ($_.Location -ieq $Location)}
351+
$Region = $AzLocation.Location
352+
if ($null -eq $Region) {
353+
Write-Error "Invalid location: $Location"
354+
return
355+
} else {
356+
$Location = $Region
357+
}
305358
$ChartLocationUrl = "https://${Location}.dp.kubernetesconfiguration.azure.com/azure-arc-k8sagents/GetLatestHelmPackagePath?api-version=2019-11-01-preview&releaseTrain=${ReleaseTrain}"
306359

307360
$Uri = [System.Uri]::New($ChartLocationUrl)
@@ -315,7 +368,7 @@ function New-AzConnectedKubernetes {
315368
$HeaderParameter = @{
316369
"Authorization" = "Bearer $AccessToken"
317370
}
318-
$Response = Invoke-WebRequest -Uri $Uri -Headers $HeaderParameter -Method Post
371+
$Response = Invoke-WebRequest -Uri $Uri -Headers $HeaderParameter -Method Post -UseBasicParsing
319372
if ($Response.StatusCode -eq 200) {
320373
$RegisteryPath = ($Response.Content | ConvertFrom-Json).repositoryPath
321374
} else {
@@ -354,27 +407,104 @@ function New-AzConnectedKubernetes {
354407
. "$PSScriptRoot/../utils/RSAHelper.ps1"
355408
$AgentPublicKey = ExportRSAPublicKeyBase64($RSA)
356409
$AgentPrivateKey = ExportRSAPrivateKeyBase64($RSA)
410+
$AgentPrivateKey = "-----BEGIN RSA PRIVATE KEY-----`n" + $AgentPrivateKey + "`n-----END RSA PRIVATE KEY-----"
357411
} catch {
358412
Write-Error "Unable to generate RSA keys"
359413
throw
360414
}
361415
} else {
362416
$AgentPublicKey = [System.Convert]::ToBase64String($RSA.ExportRSAPublicKey())
363-
$AgentPrivateKey = "-----BEGIN RSA PRIVATE KEY-----`n" + [System.Convert]::ToBase64String($RSA.ExportRSAPrivateKey()) + "`n-----END RSA PRIVATE KEY-----"
417+
$AgentPrivateKey = "-----BEGIN RSA PRIVATE KEY-----`n" + [System.Convert]::ToBase64String($RSA.ExportRSAPrivateKey()) + "`n-----END RSA PRIVATE KEY-----"
364418
}
365-
419+
366420
$HelmChartPath = Join-Path -Path $ChartExportPath -ChildPath 'azure-arc-k8sagents'
367421
if (Test-Path Env:HELMCHART) {
368422
$ChartPath = Get-ChildItem -Path Env:HELMCHART
369423
} else {
370424
$ChartPath = $HelmChartPath
371425
}
372426

427+
#Region helm options
428+
$options = ""
429+
$proxyEnableState = $false
430+
if (-not ([string]::IsNullOrEmpty($HttpProxy))) {
431+
$HttpProxyStr = $HttpProxy.ToString()
432+
$HttpProxyStr = $HttpProxyStr -replace ',','\,'
433+
$HttpProxyStr = $HttpProxyStr -replace '/','\/'
434+
$options += " --set global.httpProxy=$HttpProxyStr"
435+
$proxyEnableState = $true
436+
$Null = $PSBoundParameters.Remove('HttpProxy')
437+
}
438+
if (-not ([string]::IsNullOrEmpty($HttpsProxy))) {
439+
$HttpsProxyStr = $HttpsProxy.ToString()
440+
$HttpsProxyStr = $HttpsProxyStr -replace ',','\,'
441+
$HttpsProxyStr = $HttpsProxyStr -replace '/','\/'
442+
$options += " --set global.httpsProxy=$HttpsProxyStr"
443+
$proxyEnableState = $true
444+
$Null = $PSBoundParameters.Remove('HttpsProxy')
445+
}
446+
if (-not ([string]::IsNullOrEmpty($NoProxy))) {
447+
$NoProxy = $NoProxy -replace ',','\,'
448+
$NoProxy = $NoProxy -replace '/','\/'
449+
$options += " --set global.noProxy=$NoProxy"
450+
$proxyEnableState = $true
451+
$Null = $PSBoundParameters.Remove('NoProxy')
452+
}
453+
if ($proxyEnableState) {
454+
$options += " --set global.isProxyEnabled=true"
455+
}
456+
try {
457+
if ((-not ([string]::IsNullOrEmpty($ProxyCert))) -and (Test-Path $ProxyCert)) {
458+
$options += " --set-file global.proxyCert=$ProxyCert"
459+
$options += " --set global.isCustomCert=true"
460+
}
461+
} catch {
462+
Write-Error "Unable to find ProxyCert from file path"
463+
throw
464+
}
465+
if ($DisableAutoUpgrade) {
466+
$options += " --set systemDefaultValues.azureArcAgents.autoUpdate=false"
467+
$Null = $PSBoundParameters.Remove('DisableAutoUpgrade')
468+
}
469+
if (-not ([string]::IsNullOrEmpty($ContainerLogPath))) {
470+
$options += " --set systemDefaultValues.fluent-bit.containerLogPath=$ContainerLogPath"
471+
$Null = $PSBoundParameters.Remove('ContainerLogPath')
472+
}
473+
if (-not ([string]::IsNullOrEmpty($KubeConfig))) {
474+
$options += " --kubeconfig $KubeConfig"
475+
}
476+
if (-not ([string]::IsNullOrEmpty($KubeContext))) {
477+
$options += " --kube-context $KubeContext"
478+
}
479+
if (!$NoWait) {
480+
$options += " --wait --timeout $OnboardingTimeout"
481+
$options += "s"
482+
}
483+
#Endregion
484+
if ($PSBoundParameters.ContainsKey('OnboardingTimeout')) {
485+
$PSBoundParameters.Remove('OnboardingTimeout')
486+
}
487+
if ((-not ([string]::IsNullOrEmpty($Proxy))) -and (-not $PSBoundParameters.ContainsKey('ProxyCredential'))) {
488+
if (-not ([string]::IsNullOrEmpty($Proxy.UserInfo))) {
489+
try{
490+
$userInfo = $Proxy.UserInfo -Split ':'
491+
$pass = ConvertTo-SecureString $userInfo[1] -AsPlainText -Force
492+
$ProxyCredential = New-Object System.Management.Automation.PSCredential ($userInfo[0] , $pass)
493+
$PSBoundParameters.Add('ProxyCredential', $ProxyCredential)
494+
} catch {
495+
Write-Warning "Please set ProxyCredential or provide username and password in the Proxy parameter"
496+
throw
497+
}
498+
} else {
499+
Write-Warning "If the proxy is a private proxy, pass ProxyCredential parameter or provide username and password in the Proxy parameter"
500+
}
501+
}
502+
373503
$PSBoundParameters.Add('AgentPublicKeyCertificate', $AgentPublicKey)
374504
$Response = Az.ConnectedKubernetes.internal\New-AzConnectedKubernetes @PSBoundParameters
375505

376-
$TenantId = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext.Tenant.Id
377-
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
506+
$TenantId = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext.Tenant.Id
507+
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)
378508
Return $Response
379509
}
380510
}

src/ConnectedKubernetes/custom/Remove-AzConnectedKubernetes.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,10 @@ param(
186186
#Endregion
187187

188188
#Region get release namespace
189+
Set-Variable ReleaseInstallNamespace -option Constant -value "azure-arc-release"
189190
$ReleaseNamespace = $null
190191
try {
191-
$ReleaseNamespace = (helm status azure-arc -o json --kubeconfig $KubeConfig --kube-context $KubeContext | ConvertFrom-Json).namespace
192+
$ReleaseNamespace = (helm status azure-arc -o json --kubeconfig $KubeConfig --kube-context $KubeContext -n $ReleaseInstallNamespace | ConvertFrom-Json).namespace
192193
} catch {
193194
Write-Error "Fail to find the namespace for azure-arc."
194195
}
@@ -211,7 +212,7 @@ param(
211212
}
212213
if (($ResourceGroupName -eq $ConfigmapRgName) -and ($ClusterName -eq $ConfigmapClusterName)) {
213214
Az.ConnectedKubernetes.internal\Remove-AzConnectedKubernetes @PSBoundParameters
214-
helm delete azure-arc --namespace $ReleaseNamespace --kubeconfig $KubeConfig --kube-context $KubeContext
215+
helm delete azure-arc --namespace $ReleaseInstallNamespace --kubeconfig $KubeConfig --kube-context $KubeContext
215216
} else {
216217
Write-Error "The current context in the kubeconfig file does not correspond to the connected cluster resource specified. Agents installed on this cluster correspond to the resource group name '$ConfigmapRgName' and resource name '$ConfigmapClusterName'."
217218
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
Module Name: Az.ConnectedKubernetes
3+
Module Guid: 683047b8-5094-43e1-96c7-6792b93e81cb
4+
Download Help Link: https://learn.microsoft.com/powershell/module/az.connectedkubernetes
5+
Help Version: 1.0.0.0
6+
Locale: en-US
7+
---
8+
9+
# Az.ConnectedKubernetes Module
10+
## Description
11+
Microsoft Azure PowerShell: ConnectedKubernetes cmdlets
12+
13+
## Az.ConnectedKubernetes Cmdlets
14+
### [Get-AzConnectedKubernetes](Get-AzConnectedKubernetes.md)
15+
Returns the properties of the specified connected cluster, including name, identity, properties, and additional cluster details.
16+
17+
### [Get-AzConnectedKubernetesUserCredential](Get-AzConnectedKubernetesUserCredential.md)
18+
Gets cluster user credentials of the connected cluster with a specified resource group and name.
19+
20+
### [New-AzConnectedKubernetes](New-AzConnectedKubernetes.md)
21+
API to register a new Kubernetes cluster and create a tracked resource in Azure Resource Manager (ARM).
22+
23+
### [Remove-AzConnectedKubernetes](Remove-AzConnectedKubernetes.md)
24+
Delete a connected cluster, removing the tracked resource in Azure Resource Manager (ARM).
25+
26+
### [Update-AzConnectedKubernetes](Update-AzConnectedKubernetes.md)
27+
API to update certain properties of the connected cluster resource
28+

0 commit comments

Comments
 (0)