Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Implement multi-node networking support in the Flannel script
Browse files Browse the repository at this point in the history
Signed-off-by: Dennis Marttinen <dennis@weave.works>
  • Loading branch information
twelho committed Jul 16, 2020
1 parent 5dcffd4 commit 95daefd
Showing 1 changed file with 72 additions and 16 deletions.
88 changes: 72 additions & 16 deletions tools/ignite-flannel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,45 @@ FLANNEL_CONFLIST='{
]
}'

FLANNEL_NETWORK_CONFIG='{
"Network": "10.50.0.0/16",
"SubnetLen": 24,
"SubnetMin": "10.50.10.0",
"SubnetMax": "10.50.99.0",
"Backend": {
"Type": "udp",
"Port": 8285
}
}'

ARGS=("$@") # Save the args before shifting them away
ACTIONS="init|cleanup"
ACTION_ARGS=() # Arguments passed to the action
ACTIONS="init|join|cleanup"

usage() {
cat <<- EOF
Usage:
$0 [-h|--help] ($ACTIONS)
$0 [-h|--help] ($ACTIONS) [parameter]...
Available Actions:
init Start etcd and Flannel on this machine. For standalone and multi-node host use cases.
join <host> Start Flannel on this machine and join the multi-node overlay initialized by <host>.
cleanup Remove all persistent data created by this script. Does not remove interfaces, iptables etc.
Available Options:
-h, --help Show this help text
-h, --help Show this help text.
EOF

exit "$1"
}

join_usage() {
cat <<- EOF
Usage:
$0 join <host>
where <host> is the IP address or FQDN of a host
machine initialized with '$0 init'.
EOF

exit "$1"
Expand All @@ -62,12 +91,11 @@ while test "$#" -gt 0; do
-h|--help)
usage 0
;;
*)
usage 1
;;
esac

shift

[ -n "$ACTION" ] && ACTION_ARGS+=("$1")
done

[ -z "$ACTION" ] && usage 1
Expand All @@ -85,7 +113,7 @@ log() {
# Stops the given containers
stop_container() {
mapfile -t ID <<< "$(docker ps -q --filter "name=$1")"
{ (( ${#ID[@]} )) && docker stop "${ID[@]}" 1> /dev/null; } || true
{ [ -n "${ID[0]}" ] && docker stop "${ID[@]}" 1> /dev/null; } || true
}

# Forwards the passed parameters to 'etcdctl set'
Expand All @@ -94,9 +122,6 @@ set_config() {
}

run_init() {
# Stop etcd and/or Flannel if they're already running
stop_container "$ETCD_NAME|$FLANNEL_NAME"

log "Starting $ETCD_NAME container... "
docker run -d --rm \
-p 2379:2379 \
Expand All @@ -115,7 +140,7 @@ run_init() {
--initial-cluster-state new

log "Setting Flannel config:"
set_config /coreos.com/network/config '{ "Network": "10.1.0.0/16" }'
set_config /coreos.com/network/config "$FLANNEL_NETWORK_CONFIG"

log "Starting $FLANNEL_NAME container... "
docker run -d --rm \
Expand All @@ -136,27 +161,58 @@ run_init() {
echo "$FLANNEL_CONFLIST" > /etc/cni/net.d/10-flannel.conflist
}

run_join() {
# Get the passed etcd endpoint
[ -z "${ACTION_ARGS[0]}" ] && join_usage 1
ETCD_ENDPOINT="http://${ACTION_ARGS[0]}:2379"

log "Starting $FLANNEL_NAME container... "
docker run -d --rm \
--net host \
--name "$FLANNEL_NAME" \
--cap-add NET_ADMIN \
--device /dev/net/tun \
-v /etc/cni:/etc/cni \
-v /run/flannel:/run/flannel \
quay.io/coreos/flannel:${FLANNEL_VERSION} \
-ip-masq \
-etcd-endpoints "$ETCD_ENDPOINT"

log "Setting CNI config..."
# Remove Ignite's conflist if it exists
rm -f /etc/cni/net.d/10-ignite.conflist

# Write the Flannel conflist
echo "$FLANNEL_CONFLIST" > /etc/cni/net.d/10-flannel.conflist
}

run_cleanup() {
# Remove Flannel's conflist
rm -f /etc/cni/net.d/10-flannel.conflist

# Stop etcd and/or Flannel if they're running
stop_container "$ETCD_NAME|$FLANNEL_NAME"
}

# Stop etcd and/or Flannel if they're already running
stop_container "$ETCD_NAME|$FLANNEL_NAME"

# Run the specified action
case "$ACTION" in
init)
run_init
log "Initialized, now start your Ignite VMs with the CNI network plugin."
;;
join)
run_join
log "Complete, now check if joining was successful using 'docker logs $FLANNEL_NAME'."
log "If so, go ahead and start your Ignite VMs with the CNI network plugin."
;;
cleanup)
run_cleanup
log "Cleanup complete."
log "Cleanup complete. To finish removal of non-persistent resources such as generated"
log "network interfaces and iptables rules, reboot your system (or remove them by hand)."
;;
*)
# Should never happen
log "Configuration error: invalid action $ACTION"
log "Internal error: invalid action $ACTION"
exit 1
;;
esac

0 comments on commit 95daefd

Please sign in to comment.