Skip to content

Instantly share code, notes, and snippets.

@profnandaa
Last active March 20, 2023 10:03
Show Gist options
  • Save profnandaa/e9ab7bd7e069ce2728840b041ce4c300 to your computer and use it in GitHub Desktop.
Save profnandaa/e9ab7bd7e069ce2728840b041ce4c300 to your computer and use it in GitHub Desktop.
CNI on Windows
# Copyright The containerd Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
$DESTDIR = "$env:ProgramFiles\containerd"
$WINCNI_BIN_DIR = "$DESTDIR\cni\bin"
# releases: https://github.com/containernetworking/plugins/releases
$Version = "1.2.0"
# currently, only one architecture (amd64) is officially supported
$Uri = "https://github.com/containernetworking/plugins/releases/download/v$Version/cni-plugins-windows-amd64-v$Version.tgz"
# download the binaries
New-Item -Type Directory $WINCNI_BIN_DIR -Force
Invoke-WebRequest -Uri $Uri -OutFile cni-plugins-windows-amd64-v$Version.tgz
tar xvf .\cni-plugins-windows-amd64-v1.2.0.tgz -C $WINCNI_BIN_DIR
$CNI_CONFIG_DIR = "$DESTDIR\cni\conf"
New-Item -Type Directory $WINCNI_BIN_DIR
# TODO, split_ip and calculate_subnet
# ref: https://github.com/containerd/containerd/blob/main/script/setup/install-cni-windows
# hardcoding for now
$subnet = 24
# InterfaceAlias may differ
$GATEWAY = (Get-NetIPAddress -InterfaceAlias 'Ethernet' -AddressFamily IPv4).IPAddress
$PREFIX_LEN = (Get-NetIPAddress -InterfaceAlias 'Ethernet' -AddressFamily IPv4).PrefixLength
echo @"
{
"cniVersion": "1.0.0",
"name": "containerd-net",
"plugins": [
{
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"ipMasq": true,
"promiscMode": true,
"ipam": {
"type": "host-local",
"ranges": [
[{
"subnet": "$GATEWAY/$subnet"
}]
],
"routes": [
{ "dst": "0.0.0.0/0" },
{ "dst": "::/0" }
]
}
},
{
"type": "portmap",
"capabilities": {"portMappings": true}
}
]
}
"@ | Write-Output | Out-File "$CNI_CONFIG_DIR\0-containerd.conf" -Encoding "utf8"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment