From d43edcdcfaefe26b2efba56585765e7056fce2d2 Mon Sep 17 00:00:00 2001 From: Nate Malubay Date: Thu, 26 Dec 2024 08:58:33 -0800 Subject: [PATCH] Improved AIO quickstart script --- .../AksEdgeQuickStartForAio.ps1 | 98 +++++++++++-------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/tools/scripts/AksEdgeQuickStart/AksEdgeQuickStartForAio.ps1 b/tools/scripts/AksEdgeQuickStart/AksEdgeQuickStartForAio.ps1 index 5fa2176..0b25e5b 100644 --- a/tools/scripts/AksEdgeQuickStart/AksEdgeQuickStartForAio.ps1 +++ b/tools/scripts/AksEdgeQuickStart/AksEdgeQuickStartForAio.ps1 @@ -14,6 +14,8 @@ param( [String] $ClusterName, [String] $CustomLocationOid, [Switch] $UseK8s=$false, + [string] $AksEdgeInstallPath, + [string] $AksEdgeVhdxPath, [string] $Tag ) #Requires -RunAsAdministrator @@ -42,7 +44,7 @@ function Wait-ApiServerReady if ($retries -eq 0) { - exit -1 + exit -1 } } @@ -61,7 +63,7 @@ param( Invoke-AksEdgeNodeCommand -command "sudo cat /etc/kubernetes/manifests/kube-apiserver.yaml | tee /home/aksedge-user/kube-apiserver.yaml | tee /home/aksedge-user/kube-apiserver.yaml.working > /dev/null" Invoke-AksEdgeNodeCommand -command "sudo sed -i 's|service-account-issuer.*|service-account-issuer=$serviceAccountIssuer|' /home/aksedge-user/kube-apiserver.yaml" Invoke-AksEdgeNodeCommand -command "sudo cp /home/aksedge-user/kube-apiserver.yaml /etc/kubernetes/manifests/kube-apiserver.yaml" - & kubectl delete pod -n kube-system -l component=kube-apiserver + & kubectl delete pod -n kube-system -l component=kube-apiserver } else { @@ -105,18 +107,18 @@ param( if ((-Not $enableWorkloadIdentity) -Or ($connectedCluster.arcAgentProfile.agentState -eq "Succeeded")) { Write-Host "Cluster reached connected status" - break + break } } Write-Host "Arc connection status is $($connectedCluster.ConnectivityStatus). Waiting for status to be connected..." Start-Sleep -Seconds 10 - } + } if ($retries -eq 0) { - exit -1 - } + exit -1 + } } function New-ConnectedCluster @@ -155,15 +157,15 @@ param( { if (-Not [string]::IsNullOrEmpty($proxyArgs.Http)) { - $k8sConnectArgs += @("--proxy-http", $proxyArgs.Http) + $k8sConnectArgs += @("--proxy-http", $proxyArgs.Http) } if (-Not [string]::IsNullOrEmpty($proxyArgs.Https)) { - $k8sConnectArgs += @("--proxy-https", $proxyArgs.Https) + $k8sConnectArgs += @("--proxy-https", $proxyArgs.Https) } if (-Not [string]::IsNullOrEmpty($proxyArgs.No)) { - $k8sConnectArgs += @("--proxy-skip-range", $proxyArgs.No) + $k8sConnectArgs += @("--proxy-skip-range", $proxyArgs.No) } } @@ -174,13 +176,13 @@ param( if (-Not [string]::IsNullOrEmpty($arcArgs.GatewayResourceId)) { - $k8sConnectArgs += @("--gateway-resource-id", $arcArgs.GatewayResourceId) + $k8sConnectArgs += @("--gateway-resource-id", $arcArgs.GatewayResourceId) } Write-Host "Connect cmd args - $k8sConnectArgs" $errOut = $($retVal = & {az connectedk8s connect $k8sConnectArgs}) 2>&1 - if ($LASTEXITCODE -ne 0) - { + if ($LASTEXITCODE -ne 0) + { throw "Arc Connection failed with error : $errOut" } @@ -212,6 +214,7 @@ if (! [Environment]::Is64BitProcess) { Write-Host "Error: Run this in 64bit Powershell session" -ForegroundColor Red exit -1 } + #Validate inputs if ($arcLocations -inotcontains $Location) { Write-Host "Error: Location $Location is not supported for Azure Arc" -ForegroundColor Red @@ -220,13 +223,19 @@ if ($arcLocations -inotcontains $Location) { } # Validate az cli version. -$azVersion = (az version)[1].Split(":")[1].Split('"')[1] -$azMinRequiredVersion = "2.64.0" -if ($azVersion -lt $azMinRequiredVersion){ - Write-Host "Installed Azure CLI version $azVersion is older than $azMinRequiredVersion. Please upgrade Azure CLI and retry." -ForegroundColor Red - exit -1 +try { + $azVersion = (az version)[1].Split(":")[1].Split('"')[1] + $azMinRequiredVersion = "2.64.0" + if ($azVersion -lt $azMinRequiredVersion){ + Write-Host "Installed Azure CLI version $azVersion is older than $azMinRequiredVersion. Please upgrade Azure CLI and retry." -ForegroundColor Red + exit -1 + } +} catch { + Write-Host "Error: Azure CLI is not installed. Please install Azure CLI and retry." -ForegroundColor Red + exit -1 } + # Ensure logged into Azure $azureLogin = az account show if ( $null -eq $azureLogin){ @@ -234,10 +243,17 @@ if ( $null -eq $azureLogin){ exit -1 } +# Ensure Powershell Language Mode is "FullLanguage" +if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage") { + Write-Host "Error: Powershell ExecutionContext.SessionState.LanguageMode is set to '$($ExecutionContext.SessionState.LanguageMode)'." -ForegroundColor Red + Write-Host "Powershell ExecutionContext.SessionState.LanguageMode must be set to 'FullLanguage'." -ForegroundColor Red + exit -1 +} + # Ensure `connectedk8s` az cli extension is installed and up to date. $errOut = $($retVal = & {az extension add --upgrade --name connectedk8s -y}) 2>&1 -if ($LASTEXITCODE -ne 0) -{ +if ($LASTEXITCODE -ne 0) +{ throw "Error upgrading extension connecktedk8s : $errOut" } @@ -275,7 +291,11 @@ $aideuserConfig = @" "Password":"" } }, - "AksEdgeConfigFile": "aksedge-config.json" + "AksEdgeConfigFile": "aksedge-config.json", + "InstallOptions": { + "InstallPath": "$AksEdgeInstallPath", + "VhdxPath": "$AksEdgeVhdxPath" + } } "@ $aksedgeConfig = @" @@ -289,10 +309,10 @@ $aksedgeConfig = @" "Network": { "NetworkPlugin": "$networkplugin", "InternetDisabled": false, - "Proxy": { - "Http": null, - "Https": null, - "No": null + "Proxy": { + "Http": null, + "Https": null, + "No": null } }, "User": { @@ -380,8 +400,8 @@ if ($retval) { Write-Host "Step 3: Connect the cluster to Azure" -ForegroundColor Cyan # Set the azure subscription $errOut = $($retVal = & {az account set -s $SubscriptionId}) 2>&1 -if ($LASTEXITCODE -ne 0) -{ +if ($LASTEXITCODE -ne 0) +{ throw "Error setting Subscription ($SubscriptionId): $errOut" } @@ -390,8 +410,8 @@ $errOut = $($rgExists = & {az group show --resource-group $ResourceGroupName}) 2 if ($null -eq $rgExists) { Write-Host "Creating resource group: $ResourceGroupName" -ForegroundColor Cyan $errOut = $($retVal = & {az group create --location $Location --resource-group $ResourceGroupName --subscription $SubscriptionId}) 2>&1 - if ($LASTEXITCODE -ne 0) - { + if ($LASTEXITCODE -ne 0) + { throw "Error creating ResourceGroup ($ResourceGroupName): $errOut" } } @@ -407,21 +427,21 @@ $resourceProviders = foreach($rp in $resourceProviders) { $errOut = $($obj = & {az provider show -n $rp | ConvertFrom-Json}) 2>&1 - if ($LASTEXITCODE -ne 0) - { + if ($LASTEXITCODE -ne 0) + { throw "Error querying provider $rp : $errOut" } if ($obj.registrationState -eq "Registered") { - continue + continue } - - $errOut = $($retVal = & {az provider register -n $rp}) 2>&1 - if ($LASTEXITCODE -ne 0) - { + + $errOut = $($retVal = & {az provider register -n $rp}) 2>&1 + if ($LASTEXITCODE -ne 0) + { throw "Error registering provider $rp : $errOut" - } + } } # Arc-enable the Kubernetes cluster @@ -438,12 +458,12 @@ if ([string]::IsNullOrEmpty($objectId)) $errOut = $($objectId = & {az ad sp show --id $customLocationsAppId --query id -o tsv}) 2>&1 if ($null -eq $objectId) { - throw "Error querying ObjectId for CustomLocationsAppId : $errOut" + throw "Error querying ObjectId for CustomLocationsAppId : $errOut" } } $errOut = $($retVal = & {az connectedk8s enable-features -n $ClusterName -g $ResourceGroupName --custom-locations-oid $objectId --features cluster-connect custom-locations}) 2>&1 -if ($LASTEXITCODE -ne 0) -{ +if ($LASTEXITCODE -ne 0) +{ throw "Error enabling feature CustomLocations : $errOut" }