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