-
Notifications
You must be signed in to change notification settings - Fork 250
feat: windows cse ensure oras and reserve some exit code for network isolated cluster #7995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # functions for network isolated cluster | ||
|
|
||
| # Initialize-Oras will install oras and login the registry if anonymous access is disabled. This is required for network isolated cluster to pull windowszip from private container registry. | ||
| function Initialize-Oras { | ||
| Install-Oras | ||
| # reserve for Invoke-OrasLogin to avoid frequent code changes in parts/windows/ | ||
| } | ||
|
|
||
| # unpackage and install oras from cache | ||
| # Oras is used for pulling windows binaries, e.g. windowszip, from private container registry when it is network isolated cluster. | ||
| function Install-Oras { | ||
| # Check if OrasPath variable exists to avoid latest cached cse in vhd with possible old ab svc | ||
| $orasPathVarExists = Test-Path variable:global:OrasPath | ||
| if (-not $orasPathVarExists) { | ||
| Write-Log "OrasPath variable does not exist. Setting OrasPath to default value C:\aks-tools\oras\oras.exe" | ||
| $global:OrasPath = "C:\aks-tools\oras\oras.exe" | ||
| } | ||
|
|
||
| if (Test-Path -Path $global:OrasPath) { | ||
| # oras already installed, skip | ||
fseldow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Write-Log "Oras already installed at $($global:OrasPath)" | ||
| return | ||
| } | ||
| # Ensure cache directory exists before checking for archives or downloading | ||
| if (-Not (Test-Path $global:OrasCacheDir)) { | ||
| New-Item -ItemType Directory -Path $global:OrasCacheDir -Force | Out-Null | ||
| } | ||
fseldow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (-Not (Test-Path $global:OrasCacheDir)) { | ||
| Set-ExitCode -ExitCode $global:WINDOWS_CSE_ERROR_ORAS_NOT_FOUND -ErrorMessage "Oras cache directory not found at $($global:OrasCacheDir)" | ||
| } | ||
fseldow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Look for a cached oras archive (.tar.gz or .zip) in the oras cache directory | ||
| $orasArchive = Get-ChildItem -Path $global:OrasCacheDir -File | | ||
| Where-Object { $_.Name -like "*.tar.gz" -or $_.Name -like "*.zip" } | | ||
| Sort-Object LastWriteTime -Descending | | ||
| Select-Object -First 1 | ||
| if (-Not $orasArchive) { | ||
| Set-ExitCode -ExitCode $global:WINDOWS_CSE_ERROR_ORAS_NOT_FOUND -ErrorMessage "No oras archive (.tar.gz or .zip) found in $($global:OrasCacheDir)" | ||
| } | ||
|
|
||
| # Extract the archive to the oras install directory | ||
| $orasInstallDir = [IO.Path]::GetDirectoryName($global:OrasPath) | ||
| if (-Not (Test-Path $orasInstallDir)) { | ||
| New-Item -ItemType Directory -Path $orasInstallDir -Force | Out-Null | ||
| } | ||
|
|
||
| Write-Log "Extracting oras from $($orasArchive.FullName) to $orasInstallDir" | ||
| if ($orasArchive.Name -like "*.zip") { | ||
| AKS-Expand-Archive -Path $orasArchive.FullName -DestinationPath $orasInstallDir | ||
| } elseif ($orasArchive.Name -like "*.tar.gz") { | ||
| try { | ||
| tar -xzf $orasArchive.FullName -C $orasInstallDir | ||
| if ($LASTEXITCODE -ne 0) { | ||
| Set-ExitCode -ExitCode $global:WINDOWS_CSE_ERROR_ORAS_NOT_FOUND -ErrorMessage "Failed to extract oras archive $($orasArchive.FullName) (tar exit code $LASTEXITCODE)" | ||
| } | ||
| } catch { | ||
| Set-ExitCode -ExitCode $global:WINDOWS_CSE_ERROR_ORAS_NOT_FOUND -ErrorMessage "Exception while extracting oras archive $($orasArchive.FullName): $($_.Exception.Message)" | ||
| } | ||
| } | ||
|
|
||
| if (-Not (Test-Path $global:OrasPath)) { | ||
| Set-ExitCode -ExitCode $global:WINDOWS_CSE_ERROR_ORAS_NOT_FOUND -ErrorMessage "Oras executable not found at $($global:OrasPath) after extraction" | ||
| } | ||
|
|
||
| Write-Log "Oras installed successfully at $($global:OrasPath)" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
|
|
||
| BeforeAll { | ||
| . $PSScriptRoot\..\..\..\parts\windows\windowscsehelper.ps1 | ||
| . $PSCommandPath.Replace('.tests.ps1', '.ps1') | ||
|
|
||
| } | ||
|
|
||
| Describe "Install-Oras" { | ||
| BeforeEach { | ||
| $global:OrasPath = "C:\aks-tools\oras\oras.exe" | ||
| $global:OrasCacheDir = "C:\akse-cache\oras" | ||
fseldow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $script:archiveExtractCalls = 0 | ||
fseldow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Mock New-Item -MockWith {} | ||
| Mock Expand-Archive -MockWith { $script:archiveExtractCalls++ } | ||
| Mock AKS-Expand-Archive -MockWith { | ||
| param($Path, $DestinationPath, $Force) | ||
| $script:archiveExtractCalls++ | ||
| } | ||
| Mock tar -MockWith {} | ||
| Mock Set-ExitCode -MockWith { | ||
| Param( | ||
| [Parameter(Mandatory = $true)][int]$ExitCode, | ||
| [Parameter(Mandatory = $true)][string]$ErrorMessage | ||
| ) | ||
| throw "Set-ExitCode:${ExitCode}:${ErrorMessage}" | ||
| } | ||
| } | ||
|
|
||
| It "should return early when oras executable already exists" { | ||
| Mock Test-Path -MockWith { | ||
| Param($Path) | ||
| return $Path -eq $global:OrasPath | ||
| } | ||
fseldow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| { Install-Oras } | Should -Not -Throw | ||
| Assert-MockCalled -CommandName 'New-Item' -Times 0 | ||
| Assert-MockCalled -CommandName 'Expand-Archive' -Times 0 | ||
| Assert-MockCalled -CommandName 'AKS-Expand-Archive' -Times 0 | ||
| } | ||
|
|
||
| It "should extract cached zip archive and install oras" { | ||
| $script:orasInstalled = $false | ||
|
|
||
| Mock Test-Path -MockWith { | ||
| Param($Path) | ||
| switch ($Path) { | ||
| { $_ -eq $global:OrasPath } { return $script:orasInstalled } | ||
| { $_ -eq $global:OrasCacheDir } { return $true } | ||
| { $_ -eq "C:\aks-tools\oras" } { return $false } | ||
| default { return $true } | ||
| } | ||
| } | ||
|
|
||
| Mock Get-ChildItem -MockWith { | ||
| return [pscustomobject]@{ Name = "oras_1.3.0_windows_amd64.zip"; FullName = "C:\akse-cache\oras\oras_1.3.0_windows_amd64.zip" } | ||
| } | ||
|
|
||
| Mock Expand-Archive -MockWith { | ||
| $script:archiveExtractCalls++ | ||
| $script:orasInstalled = $true | ||
| } | ||
| Mock AKS-Expand-Archive -MockWith { | ||
| param($Path, $DestinationPath, $Force) | ||
| $script:archiveExtractCalls++ | ||
| $script:orasInstalled = $true | ||
| } | ||
|
|
||
| { Install-Oras } | Should -Not -Throw | ||
| $script:archiveExtractCalls | Should -Be 1 | ||
| } | ||
|
|
||
| It "should fail when no cached oras archive exists" { | ||
| Mock Test-Path -MockWith { | ||
| Param($Path) | ||
| return $Path -ne $global:OrasPath | ||
| } | ||
|
|
||
| Mock Get-ChildItem -MockWith { @() } | ||
|
|
||
| { | ||
| Install-Oras | ||
| } | Should -Throw "*Set-ExitCode:$($global:WINDOWS_CSE_ERROR_ORAS_NOT_FOUND):No oras archive*" | ||
| } | ||
|
|
||
| It "should fail when tar extraction returns non-zero exit code" { | ||
| Mock Test-Path -MockWith { | ||
| Param($Path) | ||
| switch ($Path) { | ||
| { $_ -eq $global:OrasPath } { return $false } | ||
| { $_ -eq $global:OrasCacheDir } { return $true } | ||
| { $_ -eq "C:\aks-tools\oras" } { return $true } | ||
| default { return $true } | ||
| } | ||
| } | ||
|
|
||
| Mock Get-ChildItem -MockWith { | ||
| return [pscustomobject]@{ Name = "oras_1.3.0_windows_amd64.tar.gz"; FullName = "C:\akse-cache\oras\oras_1.3.0_windows_amd64.tar.gz" } | ||
| } | ||
|
|
||
| Mock tar -MockWith { $global:LASTEXITCODE = 1 } | ||
|
|
||
| { | ||
| Install-Oras | ||
| } | Should -Throw "*Set-ExitCode:$($global:WINDOWS_CSE_ERROR_ORAS_NOT_FOUND):Failed to extract oras archive*" | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.