blob: 22797cd838048b1d3c5446e8e06bbf5ca1f790e3 [file] [log] [blame]
Jeremy Ronquillo6be909e2020-08-24 09:36:13 -07001#!/bin/bash -x
2#
3# Copyright 2019-present Open Networking Foundation
4#
Jeremy Ronquillo5263c732020-10-13 09:42:19 -07005# SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
Jeremy Ronquillo6be909e2020-08-24 09:36:13 -07006
7set -o errexit
8set -o pipefail
9set -o nounset
10
11exec 3>&1
12exec &>>/var/log/simple-ovs-cni.log
13
14PATH="$CNI_PATH:$(dirname "${BASH_SOURCE[0]}"):$PATH"
15CNI_CONF=$(cat /dev/stdin)
16
17get_bridge() {
18 echo "br-$(echo $CNI_CONF | jq -r '.name')"
19}
20
21ipam() {
22 local plugin=$(echo $CNI_CONF | jq -r '.ipam.type')
23 local res=$(echo $"$CNI_CONF" | "$plugin" | jq -c '.')
24 echo $res
25}
26
27add_br_and_port() {
28 mkdir -p /var/run/netns/
29 ln -sfT $CNI_NETNS /var/run/netns/$CNI_CONTAINERID
30
31 local bridge=$(get_bridge)
32 local ip=$1
33
34 ovs-vsctl --may-exist add-br $bridge
35 ovs-vsctl add-port $bridge $CNI_IFNAME -- set Interface $CNI_IFNAME type=internal
36
37 ip link set $CNI_IFNAME netns $CNI_CONTAINERID
38 ip netns exec $CNI_CONTAINERID ip addr add $ip dev $CNI_IFNAME
39 ip netns exec $CNI_CONTAINERID ip link set $CNI_IFNAME up
40}
41
42delete_br_and_port() {
43 local bridge="br-$(echo $CNI_CONF | jq -r '.name')"
44 ovs-vsctl del-port $CNI_IFNAME
45 if [ -z "$(ovs-vsctl list-ports $bridge)" ]; then
46 ovs-vsctl del-br $bridge
47 fi
48}
49
50case $CNI_COMMAND in
51ADD)
52 res=$(ipam)
Hyunsun84f0f172020-09-23 15:40:08 -050053 ip=$(echo $res | jq -r '.ips[0].address')
Jeremy Ronquillo6be909e2020-08-24 09:36:13 -070054 add_br_and_port $ip
Hyunsun84f0f172020-09-23 15:40:08 -050055 echo '{"cniVersion":"0.3.1"}' | jq -c --arg ip $ip '.ips[0].address = $ip' >&3
Jeremy Ronquillo6be909e2020-08-24 09:36:13 -070056 ;;
57DEL)
58 set +o errexit
59 ipam
60 delete_br_and_port
61 set -o errexit
62 ;;
63*)
64 echo "CNI_COMMAND=[ADD|DEL] only supported"
65 exit 1
66 ;;
67esac