Initial treasuremap/template for site_type ovsdpdk
[yaml_builds.git] / site_type / ovsdpdk / airship-treasuremap / global / v4.0 / scripts / configure-ip-rules.yaml
1 ---
2 schema: pegleg/Script/v1
3 metadata:
4   schema: metadata/Document/v1
5   name: configure-ip-rules
6   storagePolicy: cleartext
7   layeringDefinition:
8     abstract: false
9     layer: global
10 data: |-
11   #!/bin/bash
12   set -ex
13
14   function usage() {
15       cat <<EOU
16   Options are:
17
18     -c POD_CIDR     The pod CIDR for the Kubernetes cluster, e.g. 10.97.0.0/16
19     -i INTERFACE    (optional) The interface for internal pod traffic, e.g.
20                     bond0.22.  Used to auto-detect the service gateway.
21                     Exclusive with -g.
22     -g SERVICE_GW   (optional) The service gateway/VRR IP for routing pod
23                     traffic.  Exclusive with -i.
24     -o OVERLAP_CIDR (optional) This CIDR will be routed via the VRRP IP on
25                     INTERFACE.  It is used to provide a work around when
26                     complete Calico routes cannot be received via BGP.
27                     e.g. 10.96.0.0/15.  NOTE: This must include the POD_CIDR.
28     -s SERVICE_CIDR (optional) A routable CIDR to configure for ingress, maas,
29                     e.g. 10.23.22.192/29
30   EOU
31   }
32
33   SERVICE_CIDR=
34   OVERLAP_CIDR=
35
36   while getopts ":c:g:hi:o:s:" o; do
37       case "${o}" in
38           c)
39               POD_CIDR=${OPTARG}
40               ;;
41           g)
42               SERVICE_GW=${OPTARG}
43               ;;
44           h)
45               usage
46               exit 0
47               ;;
48           i)
49               INTERFACE=${OPTARG}
50               ;;
51           o)
52               OVERLAP_CIDR=${OPTARG}
53               ;;
54           s)
55               SERVICE_CIDR=${OPTARG}
56               ;;
57           \?)
58               echo "Unknown option: -${OPTARG}" >&2
59               exit 1
60               ;;
61           :)
62               echo "Missing argument for option: -${OPTARG}" >&2
63               exit 1
64               ;;
65           *)
66               echo "Unimplemented option: -${OPTARG}" >&2
67               exit 1
68               ;;
69       esac
70   done
71   shift $((OPTIND-1))
72
73   if [ "x$POD_CIDR" == "x" ]; then
74       echo "Missing pod CIDR, e.g -c 10.97.0.0/16" >&2
75       usage
76       exit 1
77   fi
78
79   if [ "x$INTERFACE" != "x" ]; then
80       while ! ip route list dev "${INTERFACE}" > /dev/null; do
81           echo Waiting for device "${INTERFACE}" to be ready. >&2
82           sleep 5
83       done
84   fi
85
86   intra_vrrp_ip=
87   if [ "x${SERVICE_GW}" == "x" ]; then
88       intra_vrrp_ip=$(ip route list dev "${INTERFACE}" | awk '($2~/via/){print $3}' | head -n 1)
89   else
90       intra_vrrp_ip=${SERVICE_GW}
91   fi
92
93   TABLE="1500"
94
95   if [ "x${intra_vrrp_ip}" == "x" ]; then
96       echo "Either INTERFACE or SERVICE_GW is required: e.g. either -i bond0.22 or -g 10.23.22.1"
97       usage
98       exit 1
99   fi
100
101   # Setup a routing table for traffic from service IPs
102   ip route flush table "${TABLE}"
103   ip route add default via "${intra_vrrp_ip}" table "${TABLE}"
104
105   # Setup arp_announce adjustment on interface facing gateway
106   arp_intf=$(ip route get ${intra_vrrp_ip} | grep dev | awk '{print $3}')
107   echo 2 > /proc/sys/net/ipv4/conf/${arp_intf}/arp_announce
108
109
110   if [ "x$OVERLAP_CIDR" != "x" ]; then
111       # NOTE: This is a work-around for nodes not receiving complete
112       # routes via BGP.
113       ip route add "${OVERLAP_CIDR}" via "${intra_vrrp_ip}"
114   fi
115
116   if [ "x$SERVICE_CIDR" != "x" ]; then
117       # Traffic from the service IPs to pods should use the pod network.
118       ip rule add \
119           from "${SERVICE_CIDR}" \
120           to "${POD_CIDR}" \
121           lookup main \
122           pref 10000
123       # Other traffic from service IPs should only use the VRRP IP
124       ip rule add \
125           from "${SERVICE_CIDR}" \
126           lookup "${TABLE}" \
127           pref 10100
128   fi