Don't overwrite CIDR_ARGS; write new variables ALL_CIDRS and IPAM_CIDRS

Handle manual CIDRs and IPAM CIDRs separately, to avoid inconsistencies and misleading messages
This commit is contained in:
Bryan Boreham committed 2015-06-10 17:24:35 +01:00
1 parent 2b4eec39cf
commit 3cdc3522be
1 file changed
+62 -58
+62 -58
View File
@@ -104,14 +104,14 @@ validate_cidr() {
collect_cidr_args() { collect_cidr_args() {
CIDR_ARGS="" CIDR_ARGS=""
CIDR_COUNT=0 CIDR_ARG_COUNT=0
while is_cidr "$1" || [ "$1" = "--subnet" ] ; do while is_cidr "$1" || [ "$1" = "--subnet" ] ; do
CIDR_ARGS="$CIDR_ARGS $1" CIDR_ARGS="$CIDR_ARGS $1"
CIDR_COUNT=$((CIDR_COUNT + 1)) CIDR_ARG_COUNT=$((CIDR_ARG_COUNT + 1))
# only accept 'default' after '--subnet', in case someone has a container called 'default' # only accept 'default' after '--subnet', in case someone has a container called 'default'
if [ "$1" = "--subnet" -a "$2" = "default" ] ; then if [ "$1" = "--subnet" -a "$2" = "default" ] ; then
CIDR_ARGS="$CIDR_ARGS $2" CIDR_ARGS="$CIDR_ARGS $2"
CIDR_COUNT=$((CIDR_ARG_COUNT + 1)) CIDR_ARG_COUNT=$((CIDR_ARG_COUNT + 1))
shift 1 shift 1
fi fi
shift 1 shift 1
@@ -155,7 +155,7 @@ if [ "$1" = "run" ] ; then
DNS_ARGS=$(exec_remote dns-args "$@") DNS_ARGS=$(exec_remote dns-args "$@")
fi fi
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_COUNT shift $CIDR_ARG_COUNT
CONTAINER=$(docker $DOCKER_CLIENT_ARGS run $DNS_ARGS -d "$@") CONTAINER=$(docker $DOCKER_CLIENT_ARGS run $DNS_ARGS -d "$@")
exec_remote attach $CIDR_ARGS $CONTAINER exec_remote attach $CIDR_ARGS $CONTAINER
echo $CONTAINER echo $CONTAINER
@@ -757,45 +757,46 @@ ipam_claim() {
done done
} }
# Pass all CIDRs to IPAM so that network addresses can be interpreted # Call IPAM as necessary to allocate addresses
# as subnets to allocate within. $1 is the full container id; following # $1 is the full container id and following args are previously parsed CIDR_ARGS.
# args are previously parsed CIDR_ARGS. # Returns ALL_CIDRS and IPAM_CIDRS
ipam_cidrs() { ipam_cidrs() {
CONTAINER_ID="$1" CONTAINER_ID="$1"
shift 1 shift 1
if [ $# -gt 0 ] ; then ALL_CIDRS=""
CIDR_ARGS="" IPAM_CIDRs=""
while [ $# -gt 0 ] ; do if [ $# -eq 0 ] ; then
if [ $# -gt 1 -a "$1" = "--subnet" ] ; then # If no addresses passed in, get one in the default subnet
shift 1 IPAM_CIDRS="$(http_call $CONTAINER_NAME $HTTP_PORT POST /ip/$CONTAINER_ID)"
if [ "$1" = "default" ] ; then if [ "$IPAM_CIDRS" = "404 page not found" ] ; then
CIDR=$(http_call $CONTAINER_NAME $HTTP_PORT POST /ip/$CONTAINER_ID) echo "No IP address supplied (use the -iprange option on 'weave launch' to enable IP address allocation)" >&2
else return 1
CIDR=$(http_call $CONTAINER_NAME $HTTP_PORT POST /ip/$CONTAINER_ID/$1) fi
fi ALL_CIDRS="$IPAM_CIDRS"
if [ "$CIDR" = "404 page not found" ] ; then fi
echo "IP address allocation must be enabled to use --subnet (use the -iprange option on 'weave launch')" >&2 while [ $# -gt 0 ] ; do
return 1 if [ $# -gt 1 -a "$1" = "--subnet" ] ; then
fi
if ! is_cidr "$CIDR" ; then
echo "$CIDR" >&2
return 1
fi
CIDR_COUNT=$((CIDR_COUNT - 1))
else
CIDR=$1
fi
CIDR_ARGS="$CIDR_ARGS $CIDR"
shift 1 shift 1
done if [ "$1" = "default" ] ; then
return 0 CIDR=$(http_call $CONTAINER_NAME $HTTP_PORT POST /ip/$CONTAINER_ID)
fi else
# If no addresses passed in, get one in the default subnet CIDR=$(http_call $CONTAINER_NAME $HTTP_PORT POST /ip/$CONTAINER_ID/$1)
CIDR_ARGS="$(http_call $CONTAINER_NAME $HTTP_PORT POST /ip/$CONTAINER_ID)" fi
if [ "$CIDR_ARGS" = "404 page not found" ] ; then if [ "$CIDR" = "404 page not found" ] ; then
echo "No IP address supplied (use the -iprange option on 'weave launch' to enable IP address allocation)" >&2 echo "IP address allocation must be enabled to use --subnet (use the -iprange option on 'weave launch')" >&2
return 1 return 1
fi fi
if ! is_cidr "$CIDR" ; then
echo "$CIDR" >&2
return 1
fi
IPAM_CIDRS="$IPAM_CIDRS $CIDR"
ALL_CIDRS="$ALL_CIDRS $CIDR"
else
ALL_CIDRS="$ALL_CIDRS $1"
fi
shift 1
done
} }
###################################################################### ######################################################################
@@ -1062,12 +1063,12 @@ case "$COMMAND" in
dns_args "$@" dns_args "$@"
fi fi
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_COUNT shift $CIDR_ARG_COUNT
CONTAINER=$(docker run $DNS_ARGS -d "$@") CONTAINER=$(docker run $DNS_ARGS -d "$@")
create_bridge create_bridge
ipam_cidrs $CONTAINER $CIDR_ARGS ipam_cidrs $CONTAINER $CIDR_ARGS
with_container_netns_or_die $CONTAINER attach $CIDR_ARGS with_container_netns_or_die $CONTAINER attach $ALL_CIDRS
when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $CIDR_ARGS when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS
echo $CONTAINER echo $CONTAINER
;; ;;
dns-args) dns-args)
@@ -1080,34 +1081,37 @@ case "$COMMAND" in
;; ;;
start) start)
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_COUNT shift $CIDR_ARG_COUNT
[ $# -eq 1 ] || usage [ $# -eq 1 ] || usage
RES=$(docker start $1) RES=$(docker start $1)
CONTAINER=$(container_id $1) CONTAINER=$(container_id $1)
create_bridge create_bridge
ipam_cidrs $CONTAINER $CIDR_ARGS ipam_cidrs $CONTAINER $CIDR_ARGS
with_container_netns_or_die $CONTAINER attach $CIDR_ARGS with_container_netns_or_die $CONTAINER attach $ALL_CIDRS
when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $CIDR_ARGS when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS
echo $RES echo $RES
;; ;;
attach) attach)
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_COUNT shift $CIDR_ARG_COUNT
[ $# -eq 1 ] || usage [ $# -eq 1 ] || usage
CONTAINER=$(container_id $1) CONTAINER=$(container_id $1)
create_bridge create_bridge
ipam_cidrs $CONTAINER $CIDR_ARGS ipam_cidrs $CONTAINER $CIDR_ARGS
with_container_netns $CONTAINER attach $CIDR_ARGS >/dev/null with_container_netns $CONTAINER attach $ALL_CIDRS >/dev/null
when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $CIDR_ARGS when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS
;; ;;
detach) detach)
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_COUNT shift $CIDR_ARG_COUNT
[ $# -eq 1 ] || usage [ $# -eq 1 ] || usage
CONTAINER=$(container_id $1) CONTAINER=$(container_id $1)
ipam_cidrs $CONTAINER $CIDR_ARGS ipam_cidrs $CONTAINER $CIDR_ARGS
with_container_netns $CONTAINER detach $CIDR_ARGS >/dev/null with_container_netns $CONTAINER detach $ALL_CIDRS >/dev/null
when_dns_running with_container_fqdn $CONTAINER delete_dns_fqdn $CIDR_ARGS when_dns_running with_container_fqdn $CONTAINER delete_dns_fqdn $ALL_CIDRS
for CIDR in $IPAM_CIDRS ; do
http_call $CONTAINER_NAME $HTTP_PORT DELETE /ip/$CONTAINER/${CIDR%/*}
done
;; ;;
dns-add) dns-add)
collect_ip_args "$@" collect_ip_args "$@"
@@ -1135,7 +1139,7 @@ case "$COMMAND" in
;; ;;
expose) expose)
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_COUNT shift $CIDR_ARG_COUNT
ipam_cidrs weave:expose $CIDR_ARGS ipam_cidrs weave:expose $CIDR_ARGS
if [ $# -eq 0 ] ; then if [ $# -eq 0 ] ; then
FQDN="" FQDN=""
@@ -1144,7 +1148,7 @@ case "$COMMAND" in
FQDN="$2" FQDN="$2"
fi fi
create_bridge --without-ethtool create_bridge --without-ethtool
for CIDR in $CIDR_ARGS ; do for CIDR in $ALL_CIDRS ; do
if ! ip addr show dev $BRIDGE | grep -qF $CIDR ; then if ! ip addr show dev $BRIDGE | grep -qF $CIDR ; then
ip addr add dev $BRIDGE $CIDR ip addr add dev $BRIDGE $CIDR
arp_update $BRIDGE $CIDR arp_update $BRIDGE $CIDR
@@ -1158,19 +1162,19 @@ case "$COMMAND" in
;; ;;
hide) hide)
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_COUNT shift $CIDR_ARG_COUNT
ipam_cidrs weave:expose $CIDR_ARGS ipam_cidrs weave:expose $CIDR_ARGS
create_bridge --without-ethtool create_bridge --without-ethtool
for CIDR in $CIDR_ARGS ; do for CIDR in $ALL_CIDRS ; do
if ip addr show dev $BRIDGE | grep -qF $CIDR ; then if ip addr show dev $BRIDGE | grep -qF $CIDR ; then
ip addr del dev $BRIDGE $CIDR ip addr del dev $BRIDGE $CIDR
delete_iptables_rule nat WEAVE -d $CIDR ! -s $CIDR -j MASQUERADE delete_iptables_rule nat WEAVE -d $CIDR ! -s $CIDR -j MASQUERADE
delete_iptables_rule nat WEAVE -s $CIDR ! -d $CIDR -j MASQUERADE delete_iptables_rule nat WEAVE -s $CIDR ! -d $CIDR -j MASQUERADE
when_dns_running delete_dns 'weave:expose' $CIDR when_dns_running delete_dns 'weave:expose' $CIDR
fi fi
# Tell IPAM we don't need this address, but ignore errors because done
# the address might not have come from IPAM for CIDR in $IPAM_CIDRS ; do
http_call $CONTAINER_NAME $HTTP_PORT DELETE /ip/weave:expose/${CIDR%/*} >/dev/null 2>&1 || true http_call $CONTAINER_NAME $HTTP_PORT DELETE /ip/weave:expose/${CIDR%/*}
done done
;; ;;
stop) stop)