-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.sh
executable file
·59 lines (52 loc) · 1.33 KB
/
init.sh
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
#!/bin/bash
# set -e
declare -A map
for i in {0..2}
do
nodeip=$(kubectl get nodes -o jsonpath="{.items[${i}].status.addresses[0].address}")
podcidr=$(kubectl get nodes -o jsonpath="{.items[${i}].spec.podCIDR}")
echo "nodeip: "$nodeip", podcidr: "$podcidr""
map["$nodeip"]="$podcidr"
done
writeConfig(){
podcidr=${map["${HOST_IP}"]}
template='{
"cniVersion": "0.3.0",
"name": "my-cni",
"type": "my-cni",
"podcidr": "%s"
}'
mkdir -p /etc/cni/net.d/
printf "${template}" $podcidr > /etc/cni/net.d/cni-config.conf
cat /etc/cni/net.d/cni-config.conf
mkdir -p /opt/cni/
cp my-cni /opt/cni/bin
# for debug
cp test.sh /opt/cni/bin
}
init(){
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -N masq > /dev/null 2>&1
for k in "${!map[@]}"
do
echo "init iptables rules, "${map[$k]}""
iptables -t nat -A masq -d ${map[$k]} -j RETURN
done
iptables -t nat -A masq -j MASQUERADE
iptables -t nat -A POSTROUTING -m addrtype ! --dst-type LOCAL -j masq
}
syncRoute(){
for k in "${!map[@]}"
do
if [ "$k" != "${HOST_IP}" ]; then
echo "init ip route, "${map[$k]}", "$k""
ip route add "${map[$k]}" via "$k" > /dev/null 2>&1
fi
done
}
writeConfig
init
syncRoute
rm /opt/cni/bin/cni.log > /dev/null 2>&1
touch /opt/cni/bin/cni.log
tail -f /opt/cni/bin/cni.log