1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#!/usr/bin/env bash
if [[ ${DEBUG} -gt 0 ]]; then set -x; fi
NETCONFPATH="${NETCONFPATH-/etc/cni/net.d}"
function exec_list() {
plist="$1"
name="$2"
cniVersion="$3"
echo "$plist" | jq -c '.[]' | while read -r conf; do
plugin_bin="$(echo "$conf" | jq -r '.type')"
conf="$(echo "$conf" | jq -r ".name = \"$name\" | .cniVersion = \"$cniVersion\"")"
if [ -n "$res" ]; then
conf="$(echo "$conf" | jq -r ".prevResult=$res")"
fi
if ! res=$(echo "$conf" | $plugin_bin); then
error "$name" "$res"
elif [[ ${DEBUG} -gt 0 ]]; then
echo "${res}" | jq -r .
fi
done
}
function error () {
name="$1"
res="$2"
err_msg=$(echo "$res" | jq -r '.msg')
if [ -z "$errmsg" ]; then
err_msg=$res
fi
echo "${name} : error executing $CNI_COMMAND: $err_msg"
exit 1
}
function exec_plugins() {
i=0
contid=$2
netns=$3
export CNI_COMMAND=$(echo $1 | tr '[:lower:]' '[:upper:]')
export PATH=$CNI_PATH:$PATH
export CNI_CONTAINERID=$contid
export CNI_NETNS=$netns
for netconf in $(echo "$NETCONFPATH"/*.conf | sort); do
export CNI_IFNAME=$(printf eth%d $i)
name=$(jq -r '.name' <"$netconf")
cniVersion=$(jq -r '.cniVersion' <"$netconf")
plist=$(jq '.plugins | select(.!=null)' <"$netconf")
if [ -n "$plist" ]; then
exec_list "$plist" "$name" "$cniVersion"
else
plugin=$(jq -r '.type' <"$netconf")
if ! res=$($plugin <"$netconf"); then
error "$name" "$res"
elif [[ ${DEBUG} -gt 0 ]]; then
echo "${res}" | jq -r .
fi
fi
(( i++ )) || true
done
}
if [ $# -ne 3 ]; then
echo "Usage: $0 add|del CONTAINER-ID NETNS-PATH"
echo " Adds or deletes the container specified by NETNS-PATH to the networks"
echo " specified in \$NETCONFPATH directory"
exit 1
fi
exec_plugins $1 $2 $3
|