diff --git a/src/ConnectedKubernetes/ConnectedKubernetes.Autorest/custom/Set-AzConnectedKubernetes.ps1 b/src/ConnectedKubernetes/ConnectedKubernetes.Autorest/custom/Set-AzConnectedKubernetes.ps1 index 02a4f0f8de6a..94c0997a55fc 100644 --- a/src/ConnectedKubernetes/ConnectedKubernetes.Autorest/custom/Set-AzConnectedKubernetes.ps1 +++ b/src/ConnectedKubernetes/ConnectedKubernetes.Autorest/custom/Set-AzConnectedKubernetes.ps1 @@ -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) @@ -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 ...." @@ -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) + } + } +} diff --git a/src/ConnectedKubernetes/ConnectedKubernetes.Autorest/custom/helpers/HelmHelper.ps1 b/src/ConnectedKubernetes/ConnectedKubernetes.Autorest/custom/helpers/HelmHelper.ps1 index 3f698044af6c..227b6f6eaaba 100644 --- a/src/ConnectedKubernetes/ConnectedKubernetes.Autorest/custom/helpers/HelmHelper.ps1 +++ b/src/ConnectedKubernetes/ConnectedKubernetes.Autorest/custom/helpers/HelmHelper.ps1 @@ -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()] @@ -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"