Skip to content

Commit

Permalink
Az.ConnectedKubernetes fixes for after Nov 2024 post-CCOA release (#2…
Browse files Browse the repository at this point in the history
…6613)

* read value of env:KUBECONFIG

* read value of env:HELMCHART

* other misuages on Env:

* revert path read on string values

* read string value of en vars

* Use Get-AzEnvironment not Get-AzureEnvironment (which doesn't work on Linux).

* Trace the name of the current cloud if in Debug mode.

* Trace the parameters passed to the internal function.

* fix test

* fix test

* remove unrelated test files

* Moving DEVELOP.md to custom so that autorest does not delete it.

* Prefix Env with dollar.

* Pass through debug and verbose flags.

* Revert "Prefix Env with dollar."

This reverts commit e4651f4.

* Ensure correct path to helm.

* Adding dependencies?

* invoke-ScriptAnalyzer improvements.

* Add dependencies for required modules.

* PSD file is autogenerated.

* Small improvements.

* Small improvements.

* ScriptAnalyzer bugs

Rework code for Powershell 5.1 compatibility.

* Disable dependencies until can solve build-module issues.

* Compatability script in utils.

* Compatibility script in here gets run!

* Delete as autorest does this each run anyway :-(

* remove hardcoded --debug in helm commands

* add WIF disable flag and read value from inputObject

* add psd1 back

* update description and add examples

* update examples

* read properties from InputObject

* remove comments in README

* sset value of wif later

* eid psbounderparameters

* fix tests

* fix typos and comments

* remote disable gateway

* update examples

* remove incorrect header comments

* update examples

* one line change for path making

* parameter set for gw

* parameter value first

* fix syntax and add more examples

* move set back to input block and update inputobject comments

* correct typo

* Move Helm get values into Helm helper.

* Tidy up helm paths.

* Typo in new helm function.

* Remove duplicate else and add missing HelmCientLocation.

* helm get values needs to return data and file.

* Remove extraneous array.

* Merge InputObject function.

* Merge InputObject function.

* Bug and debugging.

* Typo removing multiple paths.

* Bad array creation?

* Remove unused code and correct parameter name.

* checks if the value is null instead of field

* Move helper function to end of file.

---------

Co-authored-by: xw-zhang24 <xiwenzhang@microsoft.com>
Co-authored-by: Paul D.Smith <pauldsmith@microsoft.com>
  • Loading branch information
3 people authored Nov 7, 2024
1 parent ff3b653 commit d0a47a9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,46 +421,20 @@ function Set-AzConnectedKubernetes {
$DisableAutoUpgrade = ($InputObject.ArcAgentProfileAgentAutoUpgrade -eq 'Disabled')
}

if ((-not $PSBoundParameters.ContainsKey('WorkloadIdentityEnabled')) -and $InputObject.PSObject.Properties['WorkloadIdentityEnabled']) {
$WorkloadIdentityEnabled = $InputObject.WorkloadIdentityEnabled
$PSBoundParameters.Add('WorkloadIdentityEnabled', $WorkloadIdentityEnabled)
}

if ((-not $PSBoundParameters.ContainsKey('OidcIssuerProfileEnabled')) -and $InputObject.OidcIssuerProfileEnabled) {
$OidcIssuerProfileEnabled = $true
$PSBoundParameters.Add('OidcIssuerProfileEnabled', $OidcIssuerProfileEnabled)
}

if ((-not $PSBoundParameters.ContainsKey('OidcIssuerProfileSelfHostedIssuerUrl')) -and $InputObject.OidcIssuerProfileSelfHostedIssuerUrl) {
$OidcIssuerProfileEnabled = $true
$PSBoundParameters.Add('OidcIssuerProfileSelfHostedIssuerUrl', $InputObject.OidcIssuerProfileSelfHostedIssuerUrl)
}

if ((-not $PSBoundParameters.ContainsKey('Distribution')) -and $InputObject.PSObject.Properties['Distribution']) {
$PSBoundParameters.Add('Distribution', $InputObject.Distribution)
}

if ((-not $PSBoundParameters.ContainsKey('DistributionVersion')) -and $InputObject.PSObject.Properties['DistributionVersion']) {
$PSBoundParameters.Add('DistributionVersion', $InputObject.DistributionVersion)
}

if ((-not $PSBoundParameters.ContainsKey('Infrastructure')) -and $InputObject.PSObject.Properties['Infrastructure']) {
$PSBoundParameters.Add('Infrastructure', $InputObject.Infrastructure)
}

if ((-not $PSBoundParameters.ContainsKey('PrivateLinkState')) -and $InputObject.PSObject.Properties['PrivateLinkState']) {
$PSBoundParameters.Add('PrivateLinkState', $InputObject.PrivateLinkState)
}
# Merge the fields that use a common merging process.
Merge-MaybeNullInput -InputObject $InputObject -LclPSBoundParameters $PSBoundParameters
}

if ($PSBoundParameters.ContainsKey('GatewayResourceId')) {
Write-Debug "Gateway enabled"
$PSBoundParameters.Add('GatewayEnabled', $true)
} elseif ($PSBoundParameters.ContainsKey('DisableGateway')) {
}
elseif ($PSBoundParameters.ContainsKey('DisableGateway')) {
Write-Debug "Gateway disabled"
$Null = $PSBoundParameters.Remove('DisableGateway')
$PSBoundParameters.Add('GatewayEnabled', $false)
} else {
}
else {
$PSBoundParameters.Add('GatewayEnabled', -not $DisableGateway)
if (-not [String]::IsNullOrEmpty($GatewayResourceId)) {
$PSBoundParameters.Add('GatewayResourceId', $GatewayResourceId)
Expand Down Expand Up @@ -791,18 +765,13 @@ function Set-AzConnectedKubernetes {

# Get current helm values
if ($PSCmdlet.ShouldProcess($ClusterName, "Get current helm values")) {

try {
$userValuesLocation = Join-Path $env:USERPROFILE ".azure\userValues.txt"

helm get values azure-arc `
--namespace $ReleaseInstallNamespace `
--kubeconfig $KubeConfig `
--kube-context $KubeContext > $userValuesLocation
}
catch {
throw "Unable to get helm values"
}
$userValuesLocation = Get-HelmValue `
-HelmClientLocation $HelmClientLocation `
-Namespace $ReleaseInstallNamespace `
-KubeConfig $KubeConfig `
-KubeContext $KubeContext `
-Verbose:($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent -eq $true) `
-Debug:($PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent -eq $true)
}

Write-Output "Executing helm upgrade, this can take a few minutes ...."
Expand Down Expand Up @@ -850,3 +819,22 @@ function Set-AzConnectedKubernetes {
}
}
}

function Merge-MaybeNullInput {
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.DoNotExportAttribute()]
param(
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.Models.Api20240715Preview.IConnectedCluster]
$InputObject,
[System.Collections.Generic.Dictionary[system.String, System.Object]]
$LclPSBoundParameters
)

$mergeFields = 'WorkloadIdentityEnabled', 'OidcIssuerProfileEnabled', 'OidcIssuerProfileSelfHostedIssuerUrl', 'Distribution', 'DistributionVersion', 'Infrastructure', 'PrivateLinkState'

foreach ($mergeField in $mergeFields) {
if ((-not $LclPSBoundParameters.ContainsKey($mergeField)) -and $InputObject.PSObject.Properties[$mergeField] -and $null -ne $InputObject.PSObject.Properties[$mergeField].Value) {
$parameterValue = $InputObject.PSObject.Properties[$mergeField].Value
$LclPSBoundParameters.Add($mergeField, $parameterValue)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,62 @@ function IsAmd64 {
return $isSupport
}
}
function Get-AzureHelmPath {
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.DoNotExportAttribute()]
param(
[string]$ChildPath
)

# The top-level directory is always determined from HOME or USERPROFILE
# and ".azure"
if (Test-Path Env:USERPROFILE) {
$Path = $Env:USERPROFILE
}
elseif (Test-Path Env:HOME) {
$Path = $Env:HOME
}
else {
throw "No environment to use as Azure/helm path root."
}

# In Powershell v5.1 (Desktop), Join-Path lacks the AdditionalChildPaths
# argument so we have to avoid using it.
$Path = Join-Path -Path $Path -ChildPath '.azure'
$Path = Join-Path -Path $Path -ChildPath $ChildPath

return $Path
}
function Get-HelmValue {
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.DoNotExportAttribute()]
param (
[Parameter(Mandatory)]
[string]$HelmClientLocation,
[Parameter(Mandatory)]
[string]$Namespace,
[string]$KubeConfig,
[string]$KubeContext,
[string]$ValuesFile = 'userValues.txt'
)

try {
$userValuesLocation = Get-AzureHelmPath -ChildPath $ValuesFile

$cmdHelmValuesPull = @($HelmClientLocation, "get", "values", "azure-arc", "--namespace", $ReleaseInstallNamespace)
if ($KubeConfig) {
$cmdHelmValuesPull += "--kubeconfig", $KubeConfig
}
if ($KubeContext) {
$cmdHelmValuesPull += "--kube-context", $KubeContext
}

Write-Debug "Pull helm values: $cmdHelmValuesPull[0] $cmdValuesPull[1..($cmdHelmValuesPull.Count - 1)]"
Invoke-ExternalCommand $cmdHelmValuesPull[0] $cmdHelmValuesPull[1..($cmdHelmValuesPull.Count - 1)] > $userValuesLocation
}
catch {
throw "Unable to get helm values: `n$_"
}
return $userValuesLocation
}

function Get-HelmChartPath {
[Microsoft.Azure.PowerShell.Cmdlets.ConnectedKubernetes.DoNotExportAttribute()]
Expand All @@ -143,19 +199,7 @@ function Get-HelmChartPath {

# Special path!
$PreOnboardingHelmChartsFolderName = 'PreOnboardingChecksCharts'

# Exporting Helm chart; note that we might be one Windows or Linux.
if (Test-Path Env:USERPROFILE) {
$root = $Env:USERPROFILE
}
elseif (Test-Path Env:HOME) {
$root = $Env:HOME
}
else {
throw "No environment to use as root."
}
Write-Verbose "Using 'helm' to add Azure Arc resources to Kubernetes cluster"
$ChartExportPath = $root | Join-Path -ChildPath '.azure' | Join-Path -ChildPath $ChartFolderName
$ChartExportPath = Get-AzureHelmPath -ChildPath $ChartFolderName
try {
if (Test-Path $ChartExportPath) {
Write-Debug "Cleaning up existing Helm chart folder at: $ChartExportPath"
Expand Down

0 comments on commit d0a47a9

Please sign in to comment.