Skip to content

Commit 90003ba

Browse files
authored
Refactor Initialize-NatNetwork mising CNI plugin installation (#54)
1 parent d58f547 commit 90003ba

File tree

2 files changed

+35
-66
lines changed

2 files changed

+35
-66
lines changed

Tests/ContainerNetworkTools.Tests.ps1

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Describe "ContainerNetworkTools.psm1" {
156156
Mock Get-HnsNetwork -ModuleName 'ContainerNetworkTools'
157157
Mock New-HNSNetwork -ModuleName 'ContainerNetworkTools'
158158
Mock Restart-Service -ModuleName 'ContainerNetworkTools'
159+
Mock Install-WinCNIPlugin -ModuleName 'ContainerNetworkTools'
159160
}
160161

161162
It "Should use defaults" {
@@ -168,7 +169,7 @@ Describe "ContainerNetworkTools.psm1" {
168169
$Gateway -eq '99.2.0.8'
169170
$AddressPrefix -eq '99.2.0.0/16'
170171
}
171-
$MockConfFilePath = "TestDrive:\Program Files\Containerd\cni\conf\0-containerd-nat.conf"
172+
$MockConfFilePath = "C:\Program Files\Containerd\cni\conf\0-containerd-nat.conf"
172173
$MockConfFilePath | Should -Exist
173174
$MockConfFilePath | Should -FileContentMatch "`"cniVersion`": `"1.0.0`""
174175
}
@@ -191,26 +192,14 @@ Describe "ContainerNetworkTools.psm1" {
191192
$MockConfFilePath | Should -FileContentMatch "`"cniVersion`": `"2.5.7`""
192193
}
193194

194-
It "Should install missing WinCNI plugins if user consents" {
195+
It "Should install missing WinCNI plugins if plugins are missing" {
195196
Mock Test-EmptyDirectory -ModuleName 'ContainerNetworkTools' -MockWith { return $true }
196197
Mock New-Item -ModuleName 'ContainerNetworkTools'
197-
Mock Install-WinCNIPlugin -ModuleName 'ContainerNetworkTools'
198-
199-
$mockedConsent = [ActionConsent]::Yes.value__
200-
Mock Get-Host -ModuleName "ContainerNetworkTools" -MockWith { return [UITest]::new($mockedConsent) }
201198

202-
Initialize-NatNetwork
203-
Should -Invoke Install-WinCNIPlugin -ModuleName 'ContainerNetworkTools'
204-
}
205-
206-
It "Should throw an error if WinCNI plugins do not exist and user does not consent to install them" {
207-
Mock Test-EmptyDirectory -ModuleName 'ContainerNetworkTools' -MockWith { return $true }
208-
209-
$mockedConsent = [ActionConsent]::No.value__
210-
Mock Get-Host -ModuleName "ContainerNetworkTools" -MockWith { return [UITest]::new($mockedConsent) }
211-
212-
$ENV:PESTER = $true
213-
{ Initialize-NatNetwork } | Should -Throw "Windows CNI plugins have not been installed*"
199+
Initialize-NatNetwork -Force
200+
Should -Invoke Install-WinCNIPlugin -ModuleName 'ContainerNetworkTools' -ParameterFilter {
201+
$WinCNIPath -eq "$Env:ProgramFiles\Containerd\cni"
202+
}
214203
}
215204

216205
It "Should throw error if HostNetworkingService and HNS module are not installed" {

containers-toolkit/Public/ContainerNetworkTools.psm1

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ using module "..\Private\CommonToolUtilities.psm1"
1212
$ModuleParentPath = Split-Path -Parent $PSScriptRoot
1313
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1414

15-
$WINCNI_PLUGIN_REPO = "microsoft/windows-container-networking"
16-
$CLOUDNATIVE_CNI_REPO = "containernetworking/plugins"
17-
1815
function Get-WinCNILatestVersion {
1916
param (
2017
[String]$repo = "microsoft/windows-container-networking"
@@ -37,8 +34,8 @@ function Install-WinCNIPlugin {
3734
[parameter(HelpMessage = "Windows CNI plugin version to use. Defaults to 'latest'")]
3835
[string]$WinCNIVersion = "latest",
3936

40-
[parameter(HelpMessage = "Path to cni folder ~\cni . Not ~\cni\bin")]
41-
[String]$WinCNIPath,
37+
[parameter(HelpMessage = "Path to cni folder ~\cni (not ~\cni\bin). Defaults to `$env:ProgramFiles\containerd\cni)")]
38+
[String]$WinCNIPath = "$env:ProgramFiles\containerd\cni",
4239

4340
[parameter(HelpMessage = "Source of the Windows CNI plugins. Defaults to 'microsoft/windows-container-networking'")]
4441
[ValidateSet("microsoft/windows-container-networking", "containernetworking/plugins")]
@@ -112,13 +109,13 @@ function Install-WinCNIPlugin {
112109

113110
# Download file from repo
114111
$downloadParams = @{
115-
ToolName = "$ToolName"
116-
Repository = $SourceRepo
117-
Version = $WinCNIVersion
118-
OSArchitecture = $OSArchitecture
119-
DownloadPath = "$HOME\Downloads\"
112+
ToolName = "$ToolName"
113+
Repository = $SourceRepo
114+
Version = $WinCNIVersion
115+
OSArchitecture = $OSArchitecture
116+
DownloadPath = "$HOME\Downloads\"
120117
ChecksumSchemaFile = $null
121-
FileFilterRegEx = $fileFilterRegEx
118+
FileFilterRegEx = $fileFilterRegEx
122119
}
123120
$downloadParamsProperties = [FileDownloadParameters]::new(
124121
$downloadParams.ToolName,
@@ -133,10 +130,10 @@ function Install-WinCNIPlugin {
133130

134131
# Untar and install tool
135132
$params = @{
136-
Feature = "$ToolName"
137-
InstallPath = "$WinCNIPath\bin"
138-
SourceFile = $sourceFile
139-
cleanup = $true
133+
Feature = "$ToolName"
134+
InstallPath = "$WinCNIPath\bin"
135+
SourceFile = $sourceFile
136+
cleanup = $true
140137
UpdateEnvPath = $false
141138
}
142139
Install-RequiredFeature @params
@@ -169,8 +166,8 @@ function Initialize-NatNetwork {
169166
[parameter(HelpMessage = "Windows CNI plugins version to use. Defaults to latest version.")]
170167
[String]$WinCNIVersion,
171168

172-
[parameter(HelpMessage = "Absolute path to cni folder ~\cni. Not ~\cni\bin")]
173-
[String]$WinCNIPath,
169+
[parameter(HelpMessage = "Absolute path to cni folder ~\cni (not ~\cni\bin). Defaults to `$env:ProgramFiles\containerd\cni)")]
170+
[String]$WinCNIPath = "$env:ProgramFiles\containerd\cni",
174171

175172
[parameter(HelpMessage = "Bypass confirmation to install any missing dependencies (Windows CNI plugins and HNS module)")]
176173
[Switch] $Force
@@ -212,20 +209,27 @@ function Initialize-NatNetwork {
212209
Throw "Could not import HNS module. $_"
213210
}
214211

212+
Write-Information -MessageData "Creating NAT network" -InformationAction Continue
213+
214+
# Install missing WinCNI plugins
215+
if (!$isInstalled) {
216+
if ($force) {
217+
Write-Warning "Windows CNI plugins have not been installed. Installing Windows CNI plugins at '$WinCNIPath'"
218+
Install-WinCNIPlugin -WinCNIPath $WinCNIPath -WinCNIVersion $WinCNIVersion -Force:$force
219+
}
220+
else {
221+
Write-Warning "Couldn't initialize NAT network. CNI plugins have not been installed. To install, run the command `"Install-WinCNIPlugin`"."
222+
return
223+
}
224+
}
225+
215226
# Check of NAT exists
216227
$natInfo = Get-HnsNetwork -ErrorAction Ignore | Where-Object { $_.Name -eq $networkName }
217228
if ($null -ne $natInfo) {
218229
Write-Warning "$networkName already exists. To view existing networks, use `"Get-HnsNetwork`". To remove the existing network use the `"Remove-HNSNetwork`" command."
219230
return
220231
}
221232

222-
Write-Information -MessageData "Creating NAT network" -InformationAction Continue
223-
224-
# Install missing WinCNI plugins
225-
if (!$isInstalled) {
226-
Install-MissingPlugin -WinCNIVersion $WinCNIVersion -Force:$Force
227-
}
228-
229233
New-Item -ItemType 'Directory' -Path $cniConfDir -Force | Out-Null
230234

231235
# Check if `New-HNSNetwork` command exists
@@ -380,30 +384,6 @@ function Import-HNSModule {
380384
Throw "`"HostNetworkingService`" or `"HNS`" module is not installed. To resolve this issue, see`n`thttps://github.com/microsoft/containers-toolkit/blob/main/docs/FAQs.md#2-new-hnsnetwork-command-does-not-exist"
381385
}
382386

383-
function Install-MissingPlugin {
384-
param(
385-
[parameter(HelpMessage = "Windows CNI plugin version to use. Defaults to latest version")]
386-
[string]$WinCNIVersion,
387-
388-
[Switch]$Force
389-
)
390-
# Get user consent to install missing dependencies
391-
$consent = $Force
392-
if (!$Force) {
393-
$title = "Windows CNI plugins have not been installed."
394-
$question = "Do you want to install the Windows CNI plugins?"
395-
$choices = '&Yes', '&No'
396-
$consent = ([ActionConsent](Get-Host).UI.PromptForChoice($title, $question, $choices, 1)) -eq [ActionConsent]::Yes
397-
398-
if (-not $consent) {
399-
Throw "Windows CNI plugins have not been installed. To install, run the command `"Install-WinCNIPlugin`"."
400-
}
401-
}
402-
403-
Install-WinCNIPlugin -WinCNIVersion $WinCNIVersion -Force:$consent
404-
}
405-
406-
407387
# FIXME: nerdctl- Warning when user tries to run container with this network config
408388
function Set-DefaultCNIConfig {
409389
[CmdletBinding(

0 commit comments

Comments
 (0)