From 2d15685b7d14a9595e5353e1986b8c4e8aa96269 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Thu, 15 Oct 2020 22:16:05 +0200 Subject: [PATCH] Some refactoring --- Dockerfile | 4 ++-- bridget.sh | 50 +++++++++++++++++++++++++------------------------- bridget.yaml | 2 +- 3 files changed, 28 insertions(+), 28 deletions(-) mode change 100644 => 100755 bridget.sh diff --git a/Dockerfile b/Dockerfile index c807832..24038b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine +FROM alpine:3.12 RUN apk add --no-cache curl iproute2 iputils jq ADD bridget.sh /bin/bridget.sh -CMD . /bin/bridget.sh +CMD /bin/bridget.sh diff --git a/bridget.sh b/bridget.sh old mode 100644 new mode 100755 index 9b8e2da..2531397 --- a/bridget.sh +++ b/bridget.sh @@ -3,7 +3,7 @@ CNI_CONFIG="${CNI_CONFIG:-/etc/cni/net.d/10-bridget.conf}" usage() { -cat </dev/null 2> /dev/null; then +if ! ip link show "$BRIDGE" 1>/dev/null 2>/dev/null; then log "Adding new bridge $BRIDGE" ip link add dev "$BRIDGE" type bridge @@ -92,8 +92,6 @@ fi log "Setting bridge $BRIDGE up" ip link set "$BRIDGE" up - - # ------------------------------------------------------------------------------------ # Configure vlan # ------------------------------------------------------------------------------------ @@ -105,7 +103,7 @@ if ([ ! -z "$VLAN" ] || [ ! -z "$IFACE" ]) && [ "${CHECK_SLAVES:-1}" = 1 ]; then if [ ! -z "$VLAN" ]; then # check if vlan interface exist - if ip link show "$IFACE.$VLAN" 1> /dev/null 2> /dev/null; then + if ip link show "$IFACE.$VLAN" 1>/dev/null 2>/dev/null; then log "VLAN interface $IFACE.$VLAN already exist" else log "Adding new VLAN interface $IFACE.$VLAN" @@ -130,18 +128,18 @@ if ([ ! -z "$VLAN" ] || [ ! -z "$IFACE" ]) && [ "${CHECK_SLAVES:-1}" = 1 ]; then SLAVEIF="$IFACE" fi - if ! ip link show "$SLAVEIF" 1> /dev/null 2> /dev/null; then + if ! ip link show "$SLAVEIF" 1>/dev/null 2>/dev/null; then error "$SLAVEIF does not exist" fi # check if slave interface contains right master - MASTERIF="$(ip -o link show "$SLAVEIF" | grep -o -m1 'master [^ ]\+' | cut -d' ' -f2 )" + MASTERIF="$(ip -o link show "$SLAVEIF" | grep -o -m1 'master [^ ]\+' | cut -d' ' -f2)" case "$MASTERIF" in - "$BRIDGE" ) log "$SLAVEIF already member of $BRIDGE" ;; - "" ) log "Adding $SLAVEIF as member to $BRIDGE" - ip link set "$SLAVEIF" master "$BRIDGE" ;; - * ) error "interface $SLAVEIF have another master" ;; + "$BRIDGE") log "$SLAVEIF already member of $BRIDGE" ;; + "" ) log "Adding $SLAVEIF as member to $BRIDGE" + ip link set "$SLAVEIF" master "$BRIDGE" ;; + * ) error "interface $SLAVEIF have another master" ;; esac fi @@ -154,20 +152,22 @@ log "Starting retriving parameters" POD_NETWORK="${POD_NETWORK:-10.244.0.0/16}" NODE_NETWORK="$(getnodecidr "${NODE_NAME}")" if [ -z "$NODE_NETWORK" ] || [ "$NODE_NETWORK" = "null" ]; then - error "Failed to get node cidr" + error "Failed to get node cidr" fi set -e -export "POD_$(ipcalc -p "$POD_NETWORK")" # POD_PREFIX -export "POD_$(ipcalc -n "$POD_NETWORK")" # POD_NETWORK -export "NODE_$(ipcalc -p "$NODE_NETWORK")" # NODE_PREFIX -export "NODE_$(ipcalc -b "$NODE_NETWORK")" # NODE_BROADCAST -export "NODE_$(ipcalc -n "$NODE_NETWORK")" # NODE_NETWORK +export "POD_$(ipcalc -b "$POD_NETWORK")" # POD_BROADCAST +export "POD_$(ipcalc -p "$POD_NETWORK")" # POD_PREFIX +export "POD_$(ipcalc -n "$POD_NETWORK")" # POD_NETWORK +export "NODE_$(ipcalc -p "$NODE_NETWORK")" # NODE_PREFIX +export "NODE_$(ipcalc -b "$NODE_NETWORK")" # NODE_BROADCAST +export "NODE_$(ipcalc -n "$NODE_NETWORK")" # NODE_NETWORK export "NODE_IP=$(next_ip "$NODE_NETWORK")" # NODE_IP set +e +debug "POD_BROADCAST=$POD_BROADCAST" debug "POD_PREFIX=$POD_PREFIX" debug "POD_NETWORK=$POD_NETWORK" debug "NODE_PREFIX=$NODE_PREFIX" @@ -182,7 +182,7 @@ debug "NODE_IP=$NODE_IP" log "Configuring $NODE_IP/$POD_PREFIX on $BRIDGE" ip -o addr show "$BRIDGE" | grep -o 'inet [^ ]\+' | while read _ IP; do # Remove bridge addresses from the same subnet, don't touch other addresses - if [ $(ipcalc -b "$IP") = $(ipcalc -b "$POD_NETWORK/$POD_PREFIX") -a "$IP" != "$NODE_IP/$POD_PREFIX" ]; then + if [ $(ipcalc -b "$IP") = "$POD_BROADCAST" ] && [ "$IP" != "$NODE_IP/$POD_PREFIX" ]; then ip addr del "$IP" dev "$BRIDGE" fi done @@ -210,7 +210,7 @@ debug "LAST_IP=$LAST_IP" log "Writing $CNI_CONFIG" -cat > $CNI_CONFIG <$CNI_CONFIG <