-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmy-cni
executable file
·101 lines (94 loc) · 2.7 KB
/
my-cni
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
config=`cat /dev/stdin`
log="/opt/cni/bin/cni.log"
usage() {
cat << EOF
implement a simple kubernetes cni by shell.
Available CNI_COMMAND env:
ADD add a container network
DEL delete a container network
VERSION show cni version
EOF
}
allocateIP(){
if [ -f /opt/cni/bin/last_allocated_ip ]; then
ip=$((`cat /opt/cni/bin/last_allocated_ip`+1))
else
ip=2
fi
echo ${ip} > /opt/cni/bin/last_allocated_ip
echo ${ip}
}
add() {
echo config: "$config" >> "$log"
podcidr=$(echo $config | jq -r ".podcidr")
podcidr_gw=$(echo $podcidr | sed "s:0/24:1:g")
net_number=$(echo $podcidr | sed "s:.0/24:$n:g")
echo podcidr: "$podcidr" >> "$log"
echo podcidr_gw: "$podcidr_gw" >> "$log"
echo net_number: "$net_number" >> "$log"
# ensure bridge cni0 exist
ip link add cni0 type bridge > /dev/null 2>&1
ip link set cni0 up
ip addr add "$podcidr_gw"/24 dev cni0
veth_id=$(date +%s%N | md5sum | head -c 6)
ip link add dev veth_"${veth_id}" type veth peer name veth1_"${veth_id}"
ip link set dev veth_"${veth_id}" up
ip link set veth_"${veth_id}" master cni0
ln -sfT ${CNI_NETNS} /var/run/netns/${CNI_CONTAINERID}
ip link set veth1_"${veth_id}" netns ${CNI_CONTAINERID}
ip netns exec ${CNI_CONTAINERID} ip link set veth1_"${veth_id}" name ${CNI_IFNAME}
ip netns exec ${CNI_CONTAINERID} ip link set dev lo up
host_number=`allocateIP`
ip netns exec ${CNI_CONTAINERID} ip addr add "$net_number"."$host_number"/24 dev ${CNI_IFNAME}
echo "allocate ip "$net_number"."$host_number"" >> "$log"
ip netns exec ${CNI_CONTAINERID} ip link set dev ${CNI_IFNAME} up
# set default gw in container
ip netns exec ${CNI_CONTAINERID} ip route add default via ${podcidr_gw}
echo "completed exec cni add: ${CNI_CONTAINERID}" >> "$log"
ip netns exec $CNI_CONTAINERID ip link show >> "$log"
mac=$(ip netns exec $CNI_CONTAINERID ip link show ${CNI_IFNAME} | awk '/ether/ {print $2}')
address="$net_number"."$host_number"/24
output_template='{
"cniVersion": "0.3.0",
"interfaces": [
{
"name": "%s",
"mac": "%s",
"sandbox": "%s"
}
],
"ips": [
{
"version": "4",
"address": "%s",
"gateway": "%s",
"interface": 0
}
]
}'
output=$(printf "${output_template}" $CNI_IFNAME $mac $CNI_NETNS $address $podcidr_gw)
echo $output >> "$log"
echo "$output"
}
del() {
ip netns exec ${CNI_CONTAINERID} ip link del dev ${CNI_IFNAME}
}
version() {
echo "not implement"
}
case "${CNI_COMMAND}" in
ADD)
add
;;
DEL)
del
;;
VERSION)
version
;;
*)
usage
exit 1
;;
esac