Skip to content

Commit 1e821cb

Browse files
committed
Refactor Initialize-NatNetwork mising CNI plugin installation
1 parent e2ffb29 commit 1e821cb

File tree

2 files changed

+35
-63
lines changed

2 files changed

+35
-63
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 & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ function Install-WinCNIPlugin {
3434
[parameter(HelpMessage = "Windows CNI plugin version to use. Defaults to 'latest'")]
3535
[string]$WinCNIVersion = "latest",
3636

37-
[parameter(HelpMessage = "Path to cni folder ~\cni . Not ~\cni\bin")]
38-
[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",
3939

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

110110
# Download file from repo
111111
$downloadParams = @{
112-
ToolName = "$ToolName"
113-
Repository = $SourceRepo
114-
Version = $WinCNIVersion
115-
OSArchitecture = $OSArchitecture
116-
DownloadPath = "$HOME\Downloads\"
112+
ToolName = "$ToolName"
113+
Repository = $SourceRepo
114+
Version = $WinCNIVersion
115+
OSArchitecture = $OSArchitecture
116+
DownloadPath = "$HOME\Downloads\"
117117
ChecksumSchemaFile = $null
118-
FileFilterRegEx = $fileFilterRegEx
118+
FileFilterRegEx = $fileFilterRegEx
119119
}
120120
$downloadParamsProperties = [FileDownloadParameters]::new(
121121
$downloadParams.ToolName,
@@ -130,10 +130,10 @@ function Install-WinCNIPlugin {
130130

131131
# Untar and install tool
132132
$params = @{
133-
Feature = "$ToolName"
134-
InstallPath = "$WinCNIPath\bin"
135-
SourceFile = $sourceFile
136-
cleanup = $true
133+
Feature = "$ToolName"
134+
InstallPath = "$WinCNIPath\bin"
135+
SourceFile = $sourceFile
136+
cleanup = $true
137137
UpdateEnvPath = $false
138138
}
139139
Install-RequiredFeature @params
@@ -166,8 +166,8 @@ function Initialize-NatNetwork {
166166
[parameter(HelpMessage = "Windows CNI plugins version to use. Defaults to latest version.")]
167167
[String]$WinCNIVersion,
168168

169-
[parameter(HelpMessage = "Absolute path to cni folder ~\cni. Not ~\cni\bin")]
170-
[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",
171171

172172
[parameter(HelpMessage = "Bypass confirmation to install any missing dependencies (Windows CNI plugins and HNS module)")]
173173
[Switch] $Force
@@ -209,20 +209,27 @@ function Initialize-NatNetwork {
209209
Throw "Could not import HNS module. $_"
210210
}
211211

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+
212226
# Check of NAT exists
213227
$natInfo = Get-HnsNetwork -ErrorAction Ignore | Where-Object { $_.Name -eq $networkName }
214228
if ($null -ne $natInfo) {
215229
Write-Warning "$networkName already exists. To view existing networks, use `"Get-HnsNetwork`". To remove the existing network use the `"Remove-HNSNetwork`" command."
216230
return
217231
}
218232

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

228235
# Check if `New-HNSNetwork` command exists
@@ -377,30 +384,6 @@ function Import-HNSModule {
377384
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"
378385
}
379386

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

0 commit comments

Comments
 (0)