Skip to content

Commit 0881ccb

Browse files
authored
Merge pull request #4055 from apostasie/fix-win-cni
[CI] Update wincni
2 parents 1837b2f + 46af33e commit 0881ccb

File tree

3 files changed

+106
-27
lines changed

3 files changed

+106
-27
lines changed

.github/workflows/test-canary.yml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,8 @@ jobs:
7676
check-latest: true
7777
- run: go install ./cmd/nerdctl
7878
- run: make install-dev-tools
79-
# This here is solely to get the cni install script, which has not been modified in 3+ years.
80-
# There is little to no reason to update this to latest containerd
81-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
82-
with:
83-
repository: containerd/containerd
84-
ref: "v1.7.27"
85-
path: containerd
86-
fetch-depth: 1
8779
- name: "Set up CNI"
88-
working-directory: containerd
89-
run: GOPATH=$(go env GOPATH) script/setup/install-cni-windows
80+
run: GOPATH=$(go env GOPATH) ./hack/provisioning/windows/cni.sh
9081
# Windows setup script can only use released versions
9182
- name: "Set up containerd"
9283
env:

.github/workflows/test.yml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,9 @@ jobs:
7878
with:
7979
go-version: ${{ env.GO_VERSION }}
8080
check-latest: true
81-
- if: ${{ matrix.goos=='windows' }}
82-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
83-
with:
84-
repository: containerd/containerd
85-
ref: v1.7.27
86-
path: containerd
87-
fetch-depth: 1
8881
- if: ${{ matrix.goos=='windows' }}
8982
name: "Set up CNI"
90-
working-directory: containerd
91-
run: GOPATH=$(go env GOPATH) script/setup/install-cni-windows
83+
run: GOPATH=$(go env GOPATH) ./hack/provisioning/windows/cni.sh
9284
- name: "Run unit tests"
9385
run: make test-unit
9486

@@ -370,15 +362,8 @@ jobs:
370362
- run: |
371363
go install ./cmd/nerdctl
372364
make install-dev-tools
373-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
374-
with:
375-
repository: containerd/containerd
376-
ref: v1.7.27
377-
path: containerd
378-
fetch-depth: 1
379365
- name: "Set up CNI"
380-
working-directory: containerd
381-
run: GOPATH=$(go env GOPATH) script/setup/install-cni-windows
366+
run: GOPATH=$(go env GOPATH) ./hack/provisioning/windows/cni.sh
382367
- name: "Set up containerd"
383368
env:
384369
ctrdVersion: 1.7.27

hack/provisioning/windows/cni.sh

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright The containerd Authors.
4+
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# adapted from: https://raw.githubusercontent.com/containerd/containerd/refs/tags/v2.0.3/script/setup/install-cni-windows
18+
19+
#shellcheck disable=SC2154
20+
set -o errexit -o errtrace -o functrace -o nounset -o pipefail
21+
22+
# FIXME: make this configurable
23+
WINCNI_VERSION=v0.3.1
24+
25+
git config --global advice.detachedHead false
26+
27+
DESTDIR="${DESTDIR:-"C:\\Program Files\\containerd\\cni"}"
28+
WINCNI_BIN_DIR="${DESTDIR}/bin"
29+
WINCNI_PKG=github.com/Microsoft/windows-container-networking
30+
31+
git clone --depth 1 --branch "${WINCNI_VERSION}" "https://${WINCNI_PKG}.git" "${GOPATH}/src/${WINCNI_PKG}"
32+
cd "${GOPATH}/src/${WINCNI_PKG}"
33+
make all
34+
install -D -m 755 "out/nat.exe" "${WINCNI_BIN_DIR}/nat.exe"
35+
install -D -m 755 "out/sdnbridge.exe" "${WINCNI_BIN_DIR}/sdnbridge.exe"
36+
install -D -m 755 "out/sdnoverlay.exe" "${WINCNI_BIN_DIR}/sdnoverlay.exe"
37+
38+
CNI_CONFIG_DIR="${DESTDIR}/conf"
39+
mkdir -p "${CNI_CONFIG_DIR}"
40+
41+
# split_ip splits ip into a 4-element array.
42+
split_ip() {
43+
local -r varname="$1"
44+
local -r ip="$2"
45+
for i in {0..3}; do
46+
eval "$varname"["$i"]="$( echo "$ip" | cut -d '.' -f $((i + 1)) )"
47+
done
48+
}
49+
50+
# subnet gets subnet for a gateway, e.g. 192.168.100.0/24.
51+
calculate_subnet() {
52+
local -r gateway="$1"
53+
local -r prefix_len="$2"
54+
split_ip gateway_array "$gateway"
55+
local len=$prefix_len
56+
for i in {0..3}; do
57+
if (( len >= 8 )); then
58+
mask=255
59+
elif (( len > 0 )); then
60+
mask=$(( 256 - 2 ** ( 8 - len ) ))
61+
else
62+
mask=0
63+
fi
64+
(( len -= 8 ))
65+
result_array[i]=$(( gateway_array[i] & mask ))
66+
done
67+
result="$(printf ".%s" "${result_array[@]}")"
68+
result="${result:1}"
69+
echo "$result/$((32 - prefix_len))"
70+
}
71+
72+
# nat already exists on the Windows VM, the subnet and gateway
73+
# we specify should match that.
74+
: "${GATEWAY:=$(powershell -c "(Get-NetIPAddress -InterfaceAlias 'vEthernet (nat)' -AddressFamily IPv4).IPAddress")}"
75+
: "${PREFIX_LEN:=$(powershell -c "(Get-NetIPAddress -InterfaceAlias 'vEthernet (nat)' -AddressFamily IPv4).PrefixLength")}"
76+
77+
subnet="$(calculate_subnet "$GATEWAY" "$PREFIX_LEN")"
78+
79+
# The "name" field in the config is used as the underlying
80+
# network type right now (see
81+
# https://github.com/microsoft/windows-container-networking/pull/45),
82+
# so it must match a network type in:
83+
# https://docs.microsoft.com/en-us/windows-server/networking/technologies/hcn/hcn-json-document-schemas
84+
bash -c 'cat >"'"${CNI_CONFIG_DIR}"'"/0-containerd-nat.conf <<EOF
85+
{
86+
"cniVersion": "1.0.0",
87+
"name": "nat",
88+
"type": "nat",
89+
"master": "Ethernet",
90+
"ipam": {
91+
"subnet": "'"$subnet"'",
92+
"routes": [
93+
{
94+
"GW": "'"$GATEWAY"'"
95+
}
96+
]
97+
},
98+
"capabilities": {
99+
"portMappings": true,
100+
"dns": true
101+
}
102+
}
103+
EOF'

0 commit comments

Comments
 (0)