From 32d39090235e68e8011d77ce13c94a8a69b75951 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 16 Jun 2015 14:47:47 +0100 Subject: [PATCH 1/3] Make ipam_cidrs() return failure when call_weave fails --- weave | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/weave b/weave index c5e9eb3b..e653a1bf 100755 --- a/weave +++ b/weave @@ -788,7 +788,9 @@ ipam_cidrs() { IPAM_CIDRs="" if [ $# -eq 0 ] ; then # If no addresses passed in, get one in the default subnet - IPAM_CIDRS=$(call_weave POST /ip/$CONTAINER_ID) + if ! IPAM_CIDRS=$(call_weave POST /ip/$CONTAINER_ID) ; then + return 1 + fi if [ "$IPAM_CIDRS" = "404 page not found" ] ; then echo "No IP address supplied (use the -iprange option on 'weave launch' to enable IP address allocation)" >&2 return 1 @@ -798,9 +800,12 @@ ipam_cidrs() { while [ $# -gt 0 ] ; do if [ "${1%:*}" = "net" ] ; then if [ "$1" = "net:default" ] ; then - CIDR=$(call_weave POST /ip/$CONTAINER_ID) + IPAM_URL=/ip/$CONTAINER_ID else - CIDR=$(call_weave POST /ip/$CONTAINER_ID/"${1#net:}") + IPAM_URL=/ip/$CONTAINER_ID/"${1#net:}" + fi + if ! CIDR=$(call_weave POST $IPAM_URL) ; then + return 1 fi if [ "$CIDR" = "404 page not found" ] ; then echo "IP address allocation must be enabled to use --subnet (use the -iprange option on 'weave launch')" >&2 From ee4daf02b53490fdadb704344ec18e6fb5f8111a Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 16 Jun 2015 15:01:53 +0100 Subject: [PATCH 2/3] Introduce ipam_cidrs_or_die, and call it where we don't want the container to survive an ipam failure. --- weave | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/weave b/weave index e653a1bf..5aa20b30 100755 --- a/weave +++ b/weave @@ -142,7 +142,7 @@ if [ "$1" = "run" ] ; then collect_cidr_args "$@" shift $CIDR_ARG_COUNT CONTAINER=$(docker $DOCKER_CLIENT_ARGS run $DNS_ARGS -d "$@") - exec_remote attach $CIDR_ARGS $CONTAINER + exec_remote attach $CIDR_ARGS --or-die $CONTAINER echo $CONTAINER exit 0 elif [ "$1" != "--local" ] ; then @@ -824,6 +824,13 @@ ipam_cidrs() { done } +ipam_cidrs_or_die() { + if ! ipam_cidrs "$@" ; then + docker kill $1 >/dev/null 2>&1 || true + exit 1 + fi +} + ###################################################################### # weave proxy helpers ###################################################################### @@ -1026,7 +1033,7 @@ case "$COMMAND" in -p $DOCKER_BRIDGE_IP:53:53/udp \ -v /var/run/docker.sock:/var/run/docker.sock \ $WEAVEDNS_DOCKER_ARGS $DNS_IMAGE -iface $CONTAINER_IFNAME -httpiface "$DNS_HTTP_IFNAME" "$@") - ipam_cidrs $DNS_CONTAINER $CIDR_ARGS + ipam_cidrs_or_die $DNS_CONTAINER $CIDR_ARGS with_container_netns_or_die $DNS_CONTAINER attach $ALL_CIDRS populate_dns echo $DNS_CONTAINER @@ -1103,7 +1110,7 @@ case "$COMMAND" in shift $CIDR_ARG_COUNT CONTAINER=$(docker run $DNS_ARGS -d "$@") create_bridge - ipam_cidrs $CONTAINER $CIDR_ARGS + ipam_cidrs_or_die $CONTAINER $CIDR_ARGS with_container_netns_or_die $CONTAINER attach $ALL_CIDRS when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS echo $CONTAINER @@ -1123,7 +1130,7 @@ case "$COMMAND" in RES=$(docker start $1) CONTAINER=$(container_id $1) create_bridge - ipam_cidrs $CONTAINER $CIDR_ARGS + ipam_cidrs_or_die $CONTAINER $CIDR_ARGS with_container_netns_or_die $CONTAINER attach $ALL_CIDRS when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS echo $RES @@ -1131,11 +1138,15 @@ case "$COMMAND" in attach) collect_cidr_args "$@" shift $CIDR_ARG_COUNT + if [ "$1" = "--or-die" ]; then + ATTACH_TYPE="_or_die" + shift + fi [ $# -eq 1 ] || usage CONTAINER=$(container_id $1) create_bridge - ipam_cidrs $CONTAINER $CIDR_ARGS - with_container_netns $CONTAINER attach $ALL_CIDRS >/dev/null + ipam_cidrs$ATTACH_TYPE $CONTAINER $CIDR_ARGS + with_container_netns$ATTACH_TYPE $CONTAINER attach $ALL_CIDRS >/dev/null when_dns_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS ;; detach) From fd5ac633d351f5753bdcbff4c3c61f645a621fea Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 16 Jun 2015 16:05:41 +0100 Subject: [PATCH 3/3] Use `weave attach --or-die` in proxy instead of killing it in the proxy. --- proxy/start_container_interceptor.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/proxy/start_container_interceptor.go b/proxy/start_container_interceptor.go index 5ac2fdd2..774e3104 100644 --- a/proxy/start_container_interceptor.go +++ b/proxy/start_container_interceptor.go @@ -29,9 +29,8 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { Info.Printf("Attaching container %s with WEAVE_CIDR \"%s\" to weave network", container.ID, strings.Join(cidrs, " ")) args := []string{"attach"} args = append(args, cidrs...) - args = append(args, container.ID) + args = append(args, "--or-die", container.ID) if output, err := callWeave(args...); err != nil { - i.proxy.client.KillContainer(docker.KillContainerOptions{ID: container.ID}) Warning.Printf("Attaching container %s to weave network failed: %s", container.ID, string(output)) return errors.New(string(output)) }