@@ -12,9 +12,6 @@ using module "..\Private\CommonToolUtilities.psm1"
1212$ModuleParentPath = Split-Path - Parent $PSScriptRoot
1313Import-Module - Name " $ModuleParentPath \Private\CommonToolUtilities.psm1" - Force
1414
15- $WINCNI_PLUGIN_REPO = " microsoft/windows-container-networking"
16- $CLOUDNATIVE_CNI_REPO = " containernetworking/plugins"
17-
1815function 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`t https://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
408388function Set-DefaultCNIConfig {
409389 [CmdletBinding (
0 commit comments