Skip to content

Commit

Permalink
Remove installation of HNS module. Let user do that manually (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: Christine Murimi <[email protected]>
  • Loading branch information
TinaMor authored Aug 5, 2024
1 parent 29d18b3 commit b1af67e
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 157 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
$requiredModules = @('Pester', 'ThreadJob', 'HNS')
$requiredModules = @('Pester', 'ThreadJob')
$missingModules = $requiredModules | Where-Object { -not (Get-Module -ListAvailable -Name $_) }
if ($missingModules) {
Expand Down Expand Up @@ -167,4 +167,4 @@ jobs:

- name: Post results
run: |
cat coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
cat coveragereport/SummaryGithub.md >> $env:GITHUB_STEP_SUMMARY
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
$requiredModules = @('ThreadJob', 'HNS')
$requiredModules = @('ThreadJob')
$missingModules = $requiredModules | Where-Object { -not (Get-Module -ListAvailable -Name $_) }
if ($missingModules) {
Expand Down Expand Up @@ -110,4 +110,4 @@ jobs:
shell: pwsh
run: |
$tag="${{ steps.update-version.outputs.NEW_VERSION }}"
echo ":rocket: Succefully released Containers.Toolkit v$tag" >> $GITHUB_STEP_SUMMARY
echo ":rocket: Successfully released Containers.Toolkit v$tag" >> $env:GITHUB_STEP_SUMMARY
87 changes: 36 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
## Table of contents

1. [Introduction](#introduction)
2. [Usage](#usage)
1. [Usage](#usage)
- [Installing and importing Containers-Toolkit module](#installing-and-importing-containers-toolkit-module)
- [](#download-source-files)
- [Command reference](#command-reference)
3. [Important Notes](#important-notes)
4. [Contribution](#contribution)
5. [Related Projects](#related-projects)
1. [Important Notes](#important-notes)
1. [FAQs](#faqs)
1. [Contribution](#contribution)
1. [Related Projects](#related-projects)

## Introduction

Expand All @@ -42,47 +42,51 @@ Containers-Toolkit is a Windows PowerShell module for downloading, installing, a

**Option 1:** Clone containers-toolkit into one of the folder locations in the `$env:PSModulePath` environment variable.

1. To get a possible module path:
1. **To get a possible module path:**

```PowerShell
$env:PSModulePath -split ";"
```

2. Clone the repo
1. **Clone the repo**

```PowerShell
cd <selected-module-path>
git clone https://github.com/microsoft/containers-toolkit.git
```

3. Import the module
1. **Import the module**

```PowerShell
Import-Module -Name containers-toolkit -Force
```

**Option 2:** Clone containers-toolkit to a folder location of choice and add the new module location to the Windows PowerShell module path

1. Clone the repo
1. **Clone the repo**

```PowerShell
git clone https://github.com/microsoft/containers-toolkit.git
```

1. Add the directory to Windows PowerShell module path
1. **Add the directory to Windows PowerShell module path**

```PowerShell
$env:PSModulePath += ";<path-to-module-directory>"
```

1. Install module dependencies
1. **Install module dependencies**
- Install `ThreadJob` module

```powershell
Install-Module -Name ThreadJob -Force
Install-Module -Name HNS -AllowClobber -Force
```

1. Import the module
- Install `HNS` module

To install the HNS module. follow the instructions here [instructions](./docs/FAQs.md#2-new-hnsnetwork-command-does-not-exist)

1. **Import the module**

```PowerShell
Import-Module -Name containers-toolkit -Force
Expand All @@ -95,7 +99,7 @@ Get-Help containers-toolkit
```

```PowerShell
Get-Module -Name containers-toolkit
Get-Module -Name containers-toolkit -ListAvailable
```

### Command reference
Expand Down Expand Up @@ -153,52 +157,33 @@ Get-Command -Module containers-toolkit

## FAQs

1. Error when running Import-Module
- [Error when running Import-Module](https://vnote42.net/2019/07/30/error-when-running-import-module/)
- [Unblock a script to run it without changing the execution policy](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.4#example-7-unblock-a-script-to-run-it-without-changing-the-execution-policy)
- [Unblock-File](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/unblock-file?view=powershell-7.4)

## TODO

- [ ] Set up GitWorkflow files:
- [GitHub Repository Structure Best Practices](https://medium.com/code-factory-berlin/github-repository-structure-best-practices-248e6effc405)
- Setup ARM64 [self-hosted runner](https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners)
- Dependabot to update version in main + Licence
- [ ] Pipeline configuration:
- Code Analysis with [DevSkim](https://aka.ms/DevSkim)
- [ ] Publish module to PSGallery
- [ ] Fix Code analysis warnings
- [ ] Dev install: (Hacks) Add functions in Containerd and BuildKit to build from source files. (Is this really necessary? May be an overkill)
- [ ] Publish to Microsoft Learn: [MicrosoftDocs
/
Virtualization-Documentation](https://github.com/MicrosoftDocs/Virtualization-Documentation/tree/live/virtualization/windowscontainers)
- [Contribute to the Microsoft Learn platform](https://learn.microsoft.com/en-us/contribute/content/?source=recommendations)
- [x] Rename this module to containerstoolkit: The current name for this module might cause confusion with repository named windows-containers-tools
- [x] Update README.md (Documentation)
- [x] Update containers-toolkit/containers-toolkit.Format.ps1xml (Documentation)
- [x] Update Containers-Toolkit/Containers-ToolkitlsForWindows.help.txt (Documentation)
- [x] Use Containers-Toolkit
- [x] Add Pester test
- [x] Replace GitHub username in URL: <https://github.com/...>
- [ ] ~~Rootless installation~~: Not needed for Windows
See [FAQs.md](./docs/FAQs.md)

## Contribution

See [CONTRIBUTING.md](./CONTRIBUTING.md)

## Related Projects

This project builds on work done by others to create a PowerShell module.
This project builds on the work of others to create a PowerShell module.

Credits (in alphabetic order):

- [Install-ContainerdRuntime](https://github.com/microsoft/Windows-Containers/blob/Main/helpful_tools/Install-ContainerdRuntime/install-containerd-runtime.ps1)
- [sig-windows-tools- Install-Containerd.ps1](https://github.com/kubernetes-sigs/sig-windows-tools/blob/master/hostprocess/Install-Containerd.ps1)
- [containerd-installer](https://github.com/lippertmarkus/containerd-installer)
- [Install MCR on Windows Servers](https://docs.mirantis.com/mcr/20.10/install/mcr-windows.html)
- [Stevedore](https://github.com/slonopotamus/stevedore)
- [setup_buildkitd_on_windows.ps1](https://gist.github.com/gabriel-samfira/6e56238ad11c24f490ac109bdd378471)
- [Windows Containers on Windows 10 without Docker (using Containerd)](https://www.jamessturtevant.com/posts/Windows-Containers-on-Windows-10-without-Docker-using-Containerd/)
| Repo/ Author | Link |
|------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Anthony Nandaa (@profnandaa) | [cni-setup-legacy.ps1](https://gist.github.com/profnandaa/33d65d85964181a42539bfd0b4f9561a) |
| Gabriel Samfira (@gabriel-samfira) | [setup_buildkitd_on_windows.ps1](https://gist.github.com/gabriel-samfira/6e56238ad11c24f490ac109bdd378471) |
| James Sturtevant (@jsturtevant) | [Windows Containers on Windows 10 without Docker (using Containerd)](https://www.jamessturtevant.com/posts/Windows-Containers-on-Windows-10-without-Docker-using-Containerd/) |
| kubernetes-sigs/sig-windows-tools | [Install-Containerd.ps1](https://github.com/kubernetes-sigs/sig-windows-tools/blob/master/hostprocess/Install-Containerd.ps1) |
| Marat Radchenko (@slonopotamus) | [Stevedore](https://github.com/slonopotamus/stevedore) |
| Markus Lippert (@lippertmarkus) | [containerd-installer](https://github.com/lippertmarkus/containerd-installer) |
| microsoft/Windows-Containers | [install-containerd-runtime.ps1](https://github.com/microsoft/Windows-Containers/blob/Main/helpful_tools/Install-ContainerdRuntime/install-containerd-runtime.ps1) |
| Mirantis | [Install MCR on Windows Servers](https://docs.mirantis.com/mcr/20.10/install/mcr-windows.html) |

## Other relevant repositories
## Container tools installed with this module

- [Containerd](https://github.com/containerd/containerd)
- [BuildKit](https://github.com/moby/buildkit)
- [nerdctl](https://github.com/containerd/nerdctl)
- [Container networking plugins for Windows containers](https://github.com/microsoft/windows-container-networking)
- [Container Network Interface - networking for Linux containers](https://github.com/containernetworking/cni)
12 changes: 6 additions & 6 deletions Tests/AllToolsUtilities.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ Describe "AllToolsUtilities.psm1" {
}

AfterAll {
Remove-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\AllToolsUtilities.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\ContainerdTools.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\BuildkitTools.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\NerdctlTools.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\ContainerNetworkTools.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "CommonToolUtilities" -Force -ErrorAction Ignore
Remove-Module -Name "AllToolsUtilities" -Force -ErrorAction Ignore
Remove-Module -Name "ContainerdTools" -Force -ErrorAction Ignore
Remove-Module -Name "BuildkitTools" -Force -ErrorAction Ignore
Remove-Module -Name "NerdctlTools" -Force -ErrorAction Ignore
Remove-Module -Name "ContainerNetworkTools" -Force -ErrorAction Ignore
}

Context "Show-ContainerTools" -Tag "Show-ContainerTools" {
Expand Down
4 changes: 2 additions & 2 deletions Tests/BuildkitTools.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Describe "BuildkitTools.psm1" {
}

AfterAll {
Remove-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\BuildkitTools.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "CommonToolUtilities" -Force -ErrorAction Ignore
Remove-Module -Name "BuildkitTools" -Force -ErrorAction Ignore
}

Context "Install-Buildkit" -Tag "Install-Buildkit" {
Expand Down
3 changes: 2 additions & 1 deletion Tests/CommonToolUtilities.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Describe "CommonToolUtilities.psm1" {
AfterAll {
Get-ChildItem "TestDrive:\" | Remove-Item -Recurse -Force

Remove-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "CommonToolUtilities" -Force -ErrorAction Ignore
Remove-Module -Name "UpdateEnvironmentPath" -Force -ErrorAction Ignore
}

Context "Get-LatestToolVersion" -Tag "Get-LatestToolVersion" {
Expand Down
56 changes: 28 additions & 28 deletions Tests/ContainerNetworkTools.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ Describe "ContainerNetworkTools.psm1" {
BeforeAll {
$RootPath = Split-Path -Parent $PSScriptRoot
$ModuleParentPath = Join-Path -Path $RootPath -ChildPath 'Containers-Toolkit'

Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
Import-Module -Name "$ModuleParentPath\Public\ContainerNetworkTools.psm1" -Force
Import-Module -Name "$RootPath\Tests\TestData\MockClasses.psm1" -Force
}

AfterEach {
Expand All @@ -22,8 +24,9 @@ Describe "ContainerNetworkTools.psm1" {
AfterAll {
Get-ChildItem -Path 'TestDrive:\' | Remove-Item -Recurse -Force

Remove-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\ContainerNetworkTools.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "CommonToolUtilities" -Force -ErrorAction Ignore
Remove-Module -Name "ContainerNetworkTools" -Force -ErrorAction Ignore
Remove-Module -Name "MockClasses" -Force -ErrorAction Ignore
}

Context "Install-WinCNIPlugin" -Tag "Install-WinCNIPlugin" {
Expand Down Expand Up @@ -137,7 +140,6 @@ Describe "ContainerNetworkTools.psm1" {
It "Should use defaults" {
Initialize-NatNetwork -Force

Should -Invoke Import-Module -ModuleName 'ContainerNetworkTools'
Should -Invoke Get-NetRoute -ModuleName 'ContainerNetworkTools'
Should -Invoke New-HNSNetwork -ModuleName 'ContainerNetworkTools' -ParameterFilter {
$Name -eq 'NAT'
Expand Down Expand Up @@ -190,47 +192,45 @@ Describe "ContainerNetworkTools.psm1" {
{ Initialize-NatNetwork } | Should -Throw "Windows CNI plugins have not been installed*"
}

It "Should install HNS module if it does not exist" {
It "Should throw error if HostNetworkingService and HNS module are not installed" {
Mock Get-Module -ModuleName 'ContainerNetworkTools'
Mock Install-Module -ModuleName 'ContainerNetworkTools'

{ Initialize-NatNetwork -Force } | Should -Throw "Could not import HNS module.*"
Should -Invoke Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' -or $Name -eq 'HNS' }
}

It "Should first check HostNetworkingService module by default" {
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' } -MockWith { return @{} }
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }

Initialize-NatNetwork -Force
Should -Invoke Install-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }
Should -Invoke Import-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }

Should -Invoke Import-Module -Times 0 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }
Should -Invoke Get-Module -Times 0 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }
}

It "Should download HNS module file if it does not exist and failed to install module" {
Mock Get-Module -ModuleName 'ContainerNetworkTools'
Mock Install-Module -ModuleName 'ContainerNetworkTools' -MockWith { Throw 'Could not download HNS module' }
Mock Test-Path -ModuleName 'ContainerNetworkTools' -MockWith { $false } `
-ParameterFilter { $path -eq "$Env:ProgramFiles\containerd\cni\hns.psm1" }
Mock Get-InstallationFile -ModuleName 'ContainerNetworkTools'
It "Should use HNS module if HostNetworkingService is not installed" {
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' }
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { return @{} }

Initialize-NatNetwork -Force
Should -Invoke Install-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }
Should -Invoke Get-InstallationFile -ModuleName 'ContainerNetworkTools' -ParameterFilter {
$Files -like @(
@{
Feature = "HNS.psm1"
Uri = 'https://raw.githubusercontent.com/microsoft/SDN/master/Kubernetes/windows/hns.psm1'
DownloadPath = "$Env:ProgramFiles\containerd\cni"
}
)
}
Should -Invoke Import-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq "$Env:ProgramFiles\containerd\cni\hns.psm1" }

Should -Invoke Import-Module -Times 0 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' }
Should -Invoke Import-Module -Times 1 -Scope It -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' }
}

It "Should throw an error when importing HNS module fails" {
Mock Test-Path -ModuleName 'ContainerNetworkTools' -MockWith { $true } `
-ParameterFilter { $path -eq "$Env:ProgramFiles\containerd\cni\hns.psm1" }
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HostNetworkingService' }
Mock Get-Module -ModuleName 'ContainerNetworkTools' -ParameterFilter { $Name -eq 'HNS' } -MockWith { return @{} }
Mock Import-Module -ModuleName 'ContainerNetworkTools' -MockWith { Throw 'Error message.' }

{ Initialize-NatNetwork -Force } | Should -Throw "Could not import HNS module. Error message."
}

It "Should throw an error if network exists" {
Mock Get-HnsNetwork -ModuleName 'ContainerNetworkTools' -MockWith { return @{Name = 'TestN/W' } }
{ Initialize-NatNetwork -NetworkName 'TestN/W' -Force } | Should -Throw "TestN/W already exists.*"
Mock Get-HnsNetwork -ModuleName 'ContainerNetworkTools' -MockWith { return @{ Name = 'TestN/W' } }
{ Initialize-NatNetwork -NetworkName 'TestN/W' -Force } | Should -Not -Throw
Should -Invoke New-HNSNetwork -Times 0 -Scope It -ModuleName 'ContainerNetworkTools'
}

It "Should throw an error if creating a new network fails" {
Expand Down
4 changes: 2 additions & 2 deletions Tests/ContainerdTools.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ Describe "ContainerdTools.psm1" {
}

AfterAll {
Remove-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\ContainerdTools.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "CommonToolUtilities" -Force -ErrorAction Ignore
Remove-Module -Name "ContainerdTools" -Force -ErrorAction Ignore
}

Context "Install-Containerd" -Tag "Install-Containerd" {
Expand Down
7 changes: 5 additions & 2 deletions Tests/NerdctlTools.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ Describe "NerdctlTools.psm1" {
}

AfterAll {
Remove-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "$ModuleParentPath\Public\NerdctlTools.psm1" -Force -ErrorAction Ignore
Remove-Module -Name "CommonToolUtilities" -Force -ErrorAction Ignore
Remove-Module -Name "BuildkitTools" -Force -ErrorAction Ignore
Remove-Module -Name "ContainerdTools" -Force -ErrorAction Ignore
Remove-Module -Name "ContainerNetworkTools" -Force -ErrorAction Ignore
Remove-Module -Name "NerdctlTools" -Force -ErrorAction Ignore
}

Context "Install-Nerdctl" -Tag "Install-Nerdctl" {
Expand Down
Loading

0 comments on commit b1af67e

Please sign in to comment.