Skip to content

Commit

Permalink
chore: fix var substitution in components.json for windows and add CP…
Browse files Browse the repository at this point in the history
…U_ARCH substutition (#5862)
  • Loading branch information
timmy-wright authored Feb 17, 2025
1 parent c44de42 commit 972d85b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 24 deletions.
24 changes: 12 additions & 12 deletions parts/common/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@
"previousLatestVersion": "0.0.50"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v${version}.zip"
}
}
}
Expand All @@ -837,7 +837,7 @@
"latestVersion": "1.1.5"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/ccgakvplugin/v1.1.5/binaries/windows-gmsa-ccgakvplugin-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/ccgakvplugin/v1.1.5/binaries/windows-gmsa-ccgakvplugin-v${version}.zip"
}
}
}
Expand All @@ -854,7 +854,7 @@
"latestVersion": "1.1.2-hotfix.20230807"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/csi-proxy/v[version]/binaries/csi-proxy-v[version].tar.gz"
"downloadURL": "https://acs-mirror.azureedge.net/csi-proxy/v${version}/binaries/csi-proxy-v${version}.tar.gz"
}
}
}
Expand All @@ -872,7 +872,7 @@
"previousLatestVersion": "1.30.0"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/cloud-provider-azure/v[version]/binaries/azure-acr-credential-provider-windows-amd64-v[version].tar.gz"
"downloadURL": "https://acs-mirror.azureedge.net/cloud-provider-azure/v${version}/binaries/azure-acr-credential-provider-windows-amd64-v${version}.tar.gz"
}
}
}
Expand All @@ -889,7 +889,7 @@
"latestVersion": "3.24.0"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/calico-node/v[version]/binaries/calico-windows-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/calico-node/v${version}/binaries/calico-windows-v${version}.zip"
}
}
}
Expand All @@ -907,7 +907,7 @@
"previousLatestVersion": "1.5.38"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/azure-cni/v[version]/binaries/azure-vnet-cni-windows-amd64-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/azure-cni/v${version}/binaries/azure-vnet-cni-windows-amd64-v${version}.zip"
}
}
}
Expand All @@ -925,7 +925,7 @@
"previousLatestVersion": "1.4.58"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/azure-cni/v[version]/binaries/azure-vnet-cni-swift-windows-amd64-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/azure-cni/v${version}/binaries/azure-vnet-cni-swift-windows-amd64-v${version}.zip"
}
}
}
Expand All @@ -943,7 +943,7 @@
"previousLatestVersion": "1.4.58"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/azure-cni/v[version]/binaries/azure-vnet-cni-overlay-windows-amd64-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/azure-cni/v${version}/binaries/azure-vnet-cni-overlay-windows-amd64-v${version}.zip"
}
}
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@
"previousLatestVersion": "v1.7.17-azure.1"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/containerd/windows/[version]/binaries/containerd-[version]-windows-amd64.tar.gz"
"downloadURL": "https://acs-mirror.azureedge.net/containerd/windows/${version}/binaries/containerd-${version}-windows-amd64.tar.gz"
},
"ws2022": {
"versionsV2": [
Expand All @@ -1106,7 +1106,7 @@
"previousLatestVersion": "v1.7.17-azure.1"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/containerd/windows/[version]/binaries/containerd-[version]-windows-amd64.tar.gz"
"downloadURL": "https://acs-mirror.azureedge.net/containerd/windows/${version}/binaries/containerd-${version}-windows-amd64.tar.gz"
},
"default": {
"versionsV2": [
Expand All @@ -1116,7 +1116,7 @@
"previousLatestVersion": "v1.7.17-azure.1"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/containerd/windows/[version]/binaries/containerd-[version]-windows-amd64.tar.gz"
"downloadURL": "https://acs-mirror.azureedge.net/containerd/windows/${version}/binaries/containerd-${version}-windows-amd64.tar.gz"
}
},
"default": {
Expand Down Expand Up @@ -1247,7 +1247,7 @@
}
],
"downloadURL": "mcr.microsoft.com/oss/binaries/kubernetes/kubernetes-node:${version}-linux-${CPU_ARCH}",
"windowsDownloadURL": "https://acs-mirror.azureedge.net/kubernetes/[version]/windowszip/[version]-1int.zip"
"windowsDownloadURL": "https://acs-mirror.azureedge.net/kubernetes/${version}/windowszip/${version}-1int.zip"
}
}
}
Expand Down
30 changes: 26 additions & 4 deletions vhdbuilder/packer/windows/components_json_helpers.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@

function SafeReplaceString {
Param(
[Parameter(Mandatory = $true)][string]
$stringToReplace
)

$stringToReplace = &{
Clear-Variable -Name * -Exclude version,CPU_ARCH,stringToReplace -ErrorAction SilentlyContinue
$executionContext.InvokeCommand.ExpandString($stringToReplace)
}

return $stringToReplace
}


function GetComponentsFromComponentsJson
{
Param(
Expand Down Expand Up @@ -26,12 +42,16 @@ function GetComponentsFromComponentsJson
$skuMatch = $windowsVersion.windowsSkuMatch
if ($skuMatch -eq $null -or $windowsSku -eq $null -or $windowsSku -Like $skuMatch)
{
$url = $downloadUrl.replace("*", $windowsVersion.latestVersion)
$version = $windowsVersion.latestVersion
$url = SafeReplaceString($downloadUrl)
$url = $url.replace("*", $windowsVersion.latestVersion)
$output += $url

if (-not [string]::IsNullOrEmpty($windowsVersion.previousLatestVersion))
{
$url = $downloadUrl.replace("*", $windowsVersion.previousLatestVersion)
$version = $windowsVersion.previousLatestVersion
$url = SafeReplaceString($downloadUrl)
$url = $url.replace("*", $windowsVersion.previousLatestVersion)
$output += $url
}
}
Expand Down Expand Up @@ -102,12 +122,14 @@ function GetPackagesFromComponentsJson

foreach ($windowsVersion in $items)
{
$url = $downloadUrl.replace("[version]", $windowsVersion.latestVersion)
$version = $windowsVersion.latestVersion
$url = SafeReplaceString($downloadUrl)
$thisList += $url

if (-not [string]::IsNullOrEmpty($windowsVersion.previousLatestVersion))
{
$url = $downloadUrl.replace("[version]", $windowsVersion.previousLatestVersion)
$version = $windowsVersion.previousLatestVersion
$url = SafeReplaceString($downloadUrl)
$thisList += $url
}
}
Expand Down
76 changes: 68 additions & 8 deletions vhdbuilder/packer/windows/components_json_helpers.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@ BeforeAll {
. $PSCommandPath.Replace('.tests.ps1', '.ps1')
}

Describe 'SafeReplaceString' {
It 'given no vars are present, it returns the string' {
SafeReplaceString "this is a string" | Should -Be "this is a string"
}

It 'given version var is present, it replaces version' {
# set the str before the $version env var so that we know it's being replaced in the function.
$str = "this is a `${version}` string"
$version = "versioned"
SafeReplaceString $str | Should -Be "this is a versioned string"
}

It 'given CPU_ARCH var is present, it replaces version' {
# set the str before the $version env var so that we know it's being replaced in the function.
$str = "this is an `${CPU_ARCH}` string"
$CPU_ARCH = "architecture"
SafeReplaceString $str | Should -Be "this is an architecture string"
}

It 'given BOBBY var is present, it replaces with empty string' {
# set the str before the $version env var so that we know it's being replaced in the function.
$str = "this is an `${BOBBY}` string"
$BOBBY = "architecture"
SafeReplaceString $str | Should -Be "this is an string"
}

}



Describe 'GetWindowsDefenderInfo' {
BeforeEach {
$testString = '{
Expand Down Expand Up @@ -344,7 +374,7 @@ Describe 'Gets the Binaries' {
"previousLatestVersion": "0.0.51"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v${version}.zip"
}
}
}
Expand Down Expand Up @@ -385,7 +415,7 @@ Describe 'Gets the Binaries' {
}
)
$componentsJson.Packages[0].windowsDownloadLocation = "location"
$componentsJson.Packages[0].downloadUris.windows.default.downloadURL = "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
$componentsJson.Packages[0].downloadUris.windows.default.downloadURL = "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v`${version}.zip"

$packages = GetPackagesFromComponentsJson $componentsJson

Expand All @@ -403,7 +433,7 @@ Describe 'Gets the Binaries' {
}
)
$componentsJson.Packages[0].windowsDownloadLocation = "location"
$componentsJson.Packages[0].downloadUris.windows.default.downloadURL = "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
$componentsJson.Packages[0].downloadUris.windows.default.downloadURL = "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v`${version}.zip"

$packages = GetPackagesFromComponentsJson $componentsJson

Expand All @@ -418,7 +448,7 @@ Describe 'Gets the Binaries' {
}
)
$componentsJson.Packages[0].windowsDownloadLocation = "location"
$componentsJson.Packages[0].downloadUris.windows.default.downloadURL = "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
$componentsJson.Packages[0].downloadUris.windows.default.downloadURL = "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v`${version}.zip"

$packages = GetPackagesFromComponentsJson $componentsJson

Expand All @@ -439,7 +469,7 @@ Describe 'Gets the Binaries' {
"latestVersion": "0.0.48"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v${version}.zip"
}
}
}
Expand All @@ -455,7 +485,7 @@ Describe 'Gets the Binaries' {
"latestVersion": "0.0.49"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v${version}.zip"
}
}
}
Expand Down Expand Up @@ -484,7 +514,7 @@ Describe 'Gets the Binaries' {
"latestVersion": "0.0.49"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v${version}.zip"
},
"default": {
"versionsV2": [
Expand All @@ -493,7 +523,7 @@ Describe 'Gets the Binaries' {
"latestVersion": "0.0.48"
}
],
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v[version].zip"
"downloadURL": "https://acs-mirror.azureedge.net/aks/windows/cse/aks-windows-cse-scripts-v${version}.zip"
}
}
}
Expand Down Expand Up @@ -522,6 +552,36 @@ Describe 'Gets The Versions' {
$componentsJson = echo $testString | ConvertFrom-Json
}

It 'replaces CPU_ARCH in the string' {
$componentsJson.ContainerImages[0].windowsVersions = @(
[PSCustomObject]@{
latestVersion = "1.8.22"
}
)
$componentsJson.ContainerImages[0].downloadURL = "mcr.microsoft.com/oss/kubernetes/autoscaler/`${CPU_ARCH}/addon-resizer:*"

$CPU_ARCH="x86"
$components = GetComponentsFromComponentsJson $componentsJson

$components | Should -HaveCount 1
$components | Should -Contain "mcr.microsoft.com/oss/kubernetes/autoscaler/x86/addon-resizer:1.8.22"
}

It 'does not replace varvarvar in the string' {
$componentsJson.ContainerImages[0].windowsVersions = @(
[PSCustomObject]@{
latestVersion = "1.8.22"
}
)
$componentsJson.ContainerImages[0].downloadURL = "mcr.microsoft.com/oss/kubernetes/autoscaler/`${varvarvar}/addon-resizer:*"

$varvarvar="x86"
$components = GetComponentsFromComponentsJson $componentsJson

$components | Should -HaveCount 1
$components | Should -Contain "mcr.microsoft.com/oss/kubernetes/autoscaler//addon-resizer:1.8.22"
}

It 'given there are no container images, it returns an empty array' {
$componentsJson.ContainerImages = @()

Expand Down
21 changes: 21 additions & 0 deletions vhdbuilder/packer/windows/windows-vhd-configuration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ $global:aksToolsDir = "c:\aks-tools"
# We need to guarantee that the node provisioning will not fail because the vhd is full before resize-osdisk is called in AKS Windows CSE script.
$global:lowestFreeSpace = 1*1024*1024*1024 # 1GB

$cpu = Get-WmiObject -Class Win32_Processor
$CPU_ARCH = switch ($cpu.Architecture) {
0 { "amd64" } # x86
1 { "" } # MIPS
2 { "" } # Alpha
3 { "" } # PowerPC
5 { "arm64" } # ARM
6 { "amd64" } # Itanium
9 { "amd64" } # x64
default { "" }
}

Write-Output ($cpu | ConvertTo-Json)

if ([string]::IsNullOrEmpty($CPU_ARCH)) {
$cpuName = $cpu.Name
$cpuArch = $cpu.Architecture
Write-Output "Unknown architecture for CPU $cpuName with arch $cpuArch"
throw "Unsupported architecture for SKU $windowsSKU for CPU $cpuName with arch $cpuArch"
}

$HelpersFile = "c:/k/components_json_helpers.ps1"
$ComponentsJsonFile = "c:/k/components.json"
$WindowsSettingsFile = "c:/k/windows_settings.json"
Expand Down

0 comments on commit 972d85b

Please sign in to comment.