| 1 | #!/bin/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 | set -eu -o pipefail |
| 18 | |
| 19 | DESTDIR="${DESTDIR:-"C:\\Program Files\\containerd"}" |
| 20 | WINCNI_BIN_DIR="${DESTDIR}/cni/bin" |
| 21 | WINCNI_PKG=github.com/Microsoft/windows-container-networking |
| 22 | WINCNI_VERSION=aa10a0b31e9f72937063436454def1760b858ee2 |
| 23 | |
| 24 | git clone "https://${WINCNI_PKG}.git" "${GOPATH}/src/${WINCNI_PKG}" |
| 25 | cd "${GOPATH}/src/${WINCNI_PKG}" |
| 26 | git checkout "${WINCNI_VERSION}" |
| 27 | make all |
| 28 | install -D -m 755 "out/nat.exe" "${WINCNI_BIN_DIR}/nat.exe" |
| 29 | install -D -m 755 "out/sdnbridge.exe" "${WINCNI_BIN_DIR}/sdnbridge.exe" |
| 30 | install -D -m 755 "out/sdnoverlay.exe" "${WINCNI_BIN_DIR}/sdnoverlay.exe" |
| 31 | |
| 32 | CNI_CONFIG_DIR="${DESTDIR}/cni/conf" |
| 33 | mkdir -p "${CNI_CONFIG_DIR}" |
| 34 | |
| 35 | # split_ip splits ip into a 4-element array. |
| 36 | split_ip() { |
| 37 | local -r varname="$1" |
| 38 | local -r ip="$2" |
| 39 | for i in {0..3}; do |
| 40 | eval "$varname"[$i]="$( echo "$ip" | cut -d '.' -f $((i + 1)) )" |
| 41 | done |
| 42 | } |
| 43 | |
| 44 | # subnet gets subnet for a gateway, e.g. 192.168.100.0/24. |
| 45 | calculate_subnet() { |
| 46 | local -r gateway="$1" |
| 47 | local -r prefix_len="$2" |
| 48 | split_ip gateway_array "$gateway" |
| 49 | local len=$prefix_len |
| 50 | for i in {0..3}; do |
| 51 | if (( len >= 8 )); then |
| 52 | mask=255 |
| 53 | elif (( len > 0 )); then |
| 54 | mask=$(( 256 - 2 ** ( 8 - len ) )) |
| 55 | else |
| 56 | mask=0 |
| 57 | fi |
| 58 | (( len -= 8 )) |
| 59 | result_array[i]=$(( gateway_array[i] & mask )) |
| 60 | done |
| 61 | result="$(printf ".%s" "${result_array[@]}")" |
| 62 | result="${result:1}" |
| 63 | echo "$result/$((32 - prefix_len))" |
| 64 | } |
| 65 | |
| 66 | # nat already exists on the Windows VM, the subnet and gateway |
| 67 | # we specify should match that. |
| 68 | : ${GATEWAY:="$(powershell -c "(Get-NetIPAddress -InterfaceAlias 'vEthernet (nat)' -AddressFamily IPv4).IPAddress")"} |
| 69 | : ${PREFIX_LEN:="$(powershell -c "(Get-NetIPAddress -InterfaceAlias 'vEthernet (nat)' -AddressFamily IPv4).PrefixLength")"} |
| 70 | |
| 71 | subnet="$(calculate_subnet "$GATEWAY" "$PREFIX_LEN")" |
| 72 | |
| 73 | # The "name" field in the config is used as the underlying |
| 74 | # network type right now (see |
| 75 | # https://github.com/microsoft/windows-container-networking/pull/45), |
| 76 | # so it must match a network type in: |
| 77 | # https://docs.microsoft.com/en-us/windows-server/networking/technologies/hcn/hcn-json-document-schemas |
| 78 | bash -c 'cat >"'"${CNI_CONFIG_DIR}"'"/0-containerd-nat.conf <<EOF |
| 79 | { |
| 80 | "cniVersion": "0.2.0", |
| 81 | "name": "nat", |
| 82 | "type": "nat", |
| 83 | "master": "Ethernet", |
| 84 | "ipam": { |
| 85 | "subnet": "'$subnet'", |
| 86 | "routes": [ |
| 87 | { |
| 88 | "GW": "'$GATEWAY'" |
| 89 | } |
| 90 | ] |
| 91 | }, |
| 92 | "capabilities": { |
| 93 | "portMappings": true, |
| 94 | "dns": true |
| 95 | } |
| 96 | } |
| 97 | EOF' |