Skip to content

Refactor 'Install-WinCNIPlugin' to use 'Install-RequiredFeature' #44

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

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions Tests/ContainerNetworkTools.Tests.ps1
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ Describe "ContainerNetworkTools.psm1" {

Context "Install-WinCNIPlugin" -Tag "Install-WinCNIPlugin" {
BeforeAll {
$Script:ToolName = 'WinCNIPlugin'
$Script:WinCNIRepo = 'https://github.com/microsoft/windows-container-networking/releases/download'
$Script:MockZipFileName = "windows-container-networking-cni-amd64-v1.0.0.zip"
$Script:TestDownloadPath = "$HOME\Downloads\$Script:MockZipFileName"
@@ -47,8 +48,7 @@ Describe "ContainerNetworkTools.psm1" {
Mock Test-EmptyDirectory -ModuleName 'ContainerNetworkTools' -MockWith { return $true }
Mock Install-WinCNIPlugin -ModuleName 'ContainerNetworkTools'
Mock Test-Path -ModuleName 'ContainerNetworkTools' -MockWith { return $true }
Mock Invoke-ExecutableCommand -ModuleName 'ContainerNetworkTools' -MockWith {
return New-MockObject -Type 'System.Diagnostics.Process' -Properties @{ ExitCode = 0 } }
Mock Install-RequiredFeature -ModuleName 'ContainerNetworkTools'
}

It 'Should not process on implicit request for validation (WhatIfPreference)' {
@@ -69,16 +69,20 @@ Describe "ContainerNetworkTools.psm1" {

Should -Invoke Uninstall-WinCNIPlugin -ModuleName 'ContainerNetworkTools' -Times 0 -Exactly -Scope It
Should -Invoke Get-InstallationFile -ModuleName 'ContainerNetworkTools' -ParameterFilter {
$fileParameters[0].Feature -eq "WinCNIPlugin" -and
$fileParameters[0].Feature -eq "$Script:ToolName" -and
$fileParameters[0].Repo -eq "microsoft/windows-container-networking" -and
$fileParameters[0].Version -eq 'latest' -and
$fileParameters[0].DownloadPath -eq "$HOME\Downloads"
[string]::IsNullOrWhiteSpace($fileParameters.ChecksumSchemaFile) -and
$fileParameters[0].FileFilterRegEx -eq $null
}
Should -Invoke Invoke-ExecutableCommand -ModuleName 'ContainerNetworkTools' -ParameterFilter {
$executable -eq "tar.exe" -and
$arguments -eq "-xf `"$Script:TestDownloadPath`" -C `"C:\Program Files\Containerd\cni\bin`""
Should -Invoke Install-RequiredFeature -ModuleName 'ContainerNetworkTools' -ParameterFilter {
$Feature -eq "$Script:ToolName" -and
$InstallPath -eq "$Env:ProgramFiles\Containerd\cni\bin" -and
$SourceFile -eq "$Script:TestDownloadPath" -and
$EnvPath -eq $null -and
$cleanup -eq $true -and
$UpdateEnvPath -eq $false
}
}

@@ -99,9 +103,12 @@ Describe "ContainerNetworkTools.psm1" {
$fileParameters[0].OSArchitecture -eq '386' -and
$fileParameters[0].FileFilterRegEx -eq ".*tgz(.SHA512)?$"
}
Should -Invoke Invoke-ExecutableCommand -ModuleName 'ContainerNetworkTools' -ParameterFilter {
$executable -eq "tar.exe" -and
$arguments -eq "-xf `"$HOME\Downloads\$MockZipFileName`" -C `"TestDrive:\WinCNI\bin`""
Should -Invoke Install-RequiredFeature -ModuleName 'ContainerNetworkTools' -ParameterFilter {
$Feature -eq "$Script:ToolName" -and
$InstallPath -eq 'TestDrive:\WinCNI\bin' -and
$SourceFile -eq $MockDownloadFilePath -and
$cleanup -eq $true -and
$UpdateEnvPath -eq $false
}
}

17 changes: 11 additions & 6 deletions containers-toolkit/Private/CommonToolUtilities.psm1
Original file line number Diff line number Diff line change
@@ -164,10 +164,10 @@ function Get-ReleaseAssets {
# Filter assets by OS (windows) and architecture
# In the "zip|tar.gz" regex, we do not add the "$" at the end to allow for checksum files to be included
# The checksum files end with eg: ".tar.gz.sha256sum"
($_.name -match "(windows(.+)$OSArch)") -or
($_.name -match "(windows(.+)$OSArch)") -or

# nerdctl checksum files are named "SHA256SUMS".
(& ([ScriptBlock]::Create($NERDCTL_FILTER_SCRIPTBLOCK_STR -f $_.name)))
(& ([ScriptBlock]::Create($NERDCTL_FILTER_SCRIPTBLOCK_STR -f $_.name)))
)
} |
ForEach-Object {
@@ -320,7 +320,7 @@ function Get-InstallationFile {

# Buildkit checksum files are named ending with ".provenance.json" or ".sbom.json"
# We only need the ".sbom.json" file
($_.asset_name -match ".*sbom.json$") -or
($_.asset_name -match ".sbom.json$") -or

# nerdctl checksum files are named "SHA256SUMS". Check file names that have such a format.
(& ([ScriptBlock]::Create($NERDCTL_FILTER_SCRIPTBLOCK_STR -f $_.asset_name)))
@@ -702,7 +702,10 @@ function Install-RequiredFeature {
[string] $InstallPath,
[string[]] $SourceFile,
[string] $EnvPath,
[boolean] $cleanup
[boolean] $cleanup,

# Use by WinCNI plugin to avoid updating the environment path
[boolean] $UpdateEnvPath = $true
)
# Create the directory to untar to
Write-Information -InformationAction Continue -MessageData "Extracting $Feature to $InstallPath"
@@ -730,12 +733,14 @@ function Install-RequiredFeature {
}

# Add to env path
Add-FeatureToPath -Feature $Feature -Path $EnvPath
if ($UpdateEnvPath -and -not [string]::IsNullOrWhiteSpace($envPath)) {
Add-FeatureToPath -Feature $feature -Path $envPath
}

# Clean up
if ($CleanUp) {
Write-Output "Cleanup to remove downloaded files"
if (Test-Path -Path $SourceFile) {
if (Test-Path -Path $SourceFile -ErrorAction SilentlyContinue) {
Remove-Item -Path $SourceFile -Force -ErrorAction Ignore
}
}
4 changes: 2 additions & 2 deletions containers-toolkit/Public/AllToolsUtilities.psm1
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ function Show-ContainerTools {
$installedTools += (Get-InstalledVersion -Feature $tool -Latest:$Latest)
}

$registerCommands = (Get-Command -Name "*-*Service" | Where-Object { $_.Source -eq 'Containers-Toolkit' }).Name -join ', '
$registerCommands = (Get-Command -Name "*-*Service" -Module 'Containers-Toolkit').Name -join ', '
$message = "For unregistered services/daemons, check the tool's help or register the service using `n`t$registerCommands"
Write-Information -MessageData $message -Tags "Instructions" -InformationAction Continue
return $installedTools
@@ -131,7 +131,7 @@ function Install-ContainerTools {
$completedInstalls += $task.Name
}
catch {
Write-Error "$($task.Name) Installation failed. $_"
Write-Error "$($task.Name) installation failed. $_"
$failedInstalls += $task.Name
}
}
35 changes: 11 additions & 24 deletions containers-toolkit/Public/ContainerNetworkTools.psm1
Original file line number Diff line number Diff line change
@@ -43,13 +43,14 @@ function Install-WinCNIPlugin {
)

begin {
$ToolName = 'WinCNIPlugin'
if (!$WinCNIPath) {
$containerdPath = Get-DefaultInstallPath -Tool "containerd"
$WinCNIPath = "$containerdPath\cni"
}
$WinCNIPath = $WinCNIPath -replace '(\\bin)$', ''

# Check if Containerd is alread installed
# Check if WinCNI plugins are installed
$isInstalled = -not (Test-EmptyDirectory -Path $WinCNIPath)

$plugin = "Windows CNI plugins"
@@ -86,10 +87,6 @@ function Install-WinCNIPlugin {

New-Item -Path $WinCNIPath -ItemType Directory -Force -ErrorAction Ignore | Out-Null

# NOTE: We download plugins from https://github.com/microsoft/windows-container-networking
# instead of https://github.com/containernetworking/plugins/releases.
# The latter causes an error in nerdctl: "networking setup error has occurred. incompatible CNI versions"

Write-Debug ("Downloading Windows CNI plugins from {0}" -f $SourceRepo)

# File filter for Windows CNI plugins
@@ -104,7 +101,7 @@ function Install-WinCNIPlugin {

# Download file from repo
$downloadParams = @{
ToolName = "WinCNIPlugin"
ToolName = "$ToolName"
Repository = $SourceRepo
Version = $WinCNIVersion
OSArchitecture = $OSArchitecture
@@ -123,25 +120,15 @@ function Install-WinCNIPlugin {
)
$sourceFile = Get-InstallationFile -FileParameters $downloadParamsProperties

if (-not (Test-Path -Path $sourceFile)) {
Throw "Couldn't find the downloaded file $sourceFile"
}

$WinCNIBin = "$WinCNIPath\bin"
if (-not (Test-Path -Path $WinCNIBin)) {
New-Item -Path $WinCNIBin -ItemType Directory -Force -ErrorAction Ignore | Out-Null
}

# Expand archive file
$cmdOutput = Invoke-ExecutableCommand -executable "tar.exe" -arguments "-xf `"$sourceFile`" -C `"$WinCNIBin`"" -timeout (60 * 2)
if ($cmdOutput.ExitCode -ne 0) {
Throw "Failed to expand archive `"$sourceFile`" at `"$WinCNIBin`". Exit code: $($cmdOutput.ExitCode). $($cmdOutput.StandardError.ReadToEnd())"
}

if (Test-Path -Path $sourceFile) {
# Remove the downloaded file
Remove-Item -Path "$sourceFile" -Force -ErrorAction SilentlyContinue
# Untar and install tool
$params = @{
Feature = "$ToolName"
InstallPath = "$WinCNIPath\bin"
SourceFile = $sourceFile
cleanup = $true
UpdateEnvPath = $false
}
Install-RequiredFeature @params

Write-Output "CNI plugin version $WinCNIVersion ($sourceRepo) successfully installed at $WinCNIPath"
}
Loading
Oops, something went wrong.