diff --git a/proxy/common.go b/proxy/common.go index ddfa85e6..1198ef1b 100644 --- a/proxy/common.go +++ b/proxy/common.go @@ -31,8 +31,7 @@ func callWeave(args ...string) ([]byte, error) { func weaveCIDRsFromConfig(config *docker.Config) ([]string, bool) { for _, e := range config.Env { if strings.HasPrefix(e, "WEAVE_CIDR=") { - result := strings.Trim(e[11:], " ") - return strings.Split(strings.TrimSpace(result), " "), result != "" + return strings.Fields(e[11:]), true } } return nil, false diff --git a/proxy/start_container_interceptor.go b/proxy/start_container_interceptor.go index d35c3afd..8c159d34 100644 --- a/proxy/start_container_interceptor.go +++ b/proxy/start_container_interceptor.go @@ -28,6 +28,14 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { Debug.Print("No Weave CIDR, ignoring") return nil } + if len(cidrs) == 0 { + ipamCIDR, err := callWeave("ipam-cidr", container.ID) + if err != nil { + Warning.Printf("Determining container %s IP via IPAM failed: %v, %v", container.ID, err, string(ipamCIDR)) + return nil + } + cidrs = strings.Fields(string(ipamCIDR)) + } Info.Printf("Attaching container %s with WEAVE_CIDR \"%s\" to weave network", container.ID, strings.Join(cidrs, " ")) args := []string{"attach"} args = append(args, cidrs...) diff --git a/test/660_proxy_ipam_test.sh b/test/660_proxy_ipam_test.sh new file mode 100755 index 00000000..622307b0 --- /dev/null +++ b/test/660_proxy_ipam_test.sh @@ -0,0 +1,19 @@ +#! /bin/bash + +. ./config.sh + +UNIVERSE=10.2.2.0/24 + +start_suite "Ping proxied containers over cross-host weave network (with IPAM)" + +weave_on $HOST1 launch -iprange $UNIVERSE +weave_on $HOST1 launch-proxy +weave_on $HOST2 launch -iprange $UNIVERSE $HOST1 +weave_on $HOST2 launch-proxy + +proxy_start_container $HOST1 -e WEAVE_CIDR= --name=c1 +proxy_start_container $HOST2 -e WEAVE_CIDR= --name=c2 +C2=$(container_ip $HOST2 c2) +assert_raises "exec_on $HOST1 c1 $PING $C2" + +end_suite diff --git a/test/config.sh b/test/config.sh index b8c657a1..c48844a8 100644 --- a/test/config.sh +++ b/test/config.sh @@ -112,6 +112,12 @@ start_container() { weave_on $host run "$@" -t gliderlabs/alpine /bin/sh } +proxy_start_container() { + host=$1 + shift 1 + docker -H tcp://$host:12375 run "$@" -dt gliderlabs/alpine /bin/sh +} + start_container_with_dns() { host=$1 shift 1 diff --git a/weave b/weave index 45295f52..fef6bd45 100755 --- a/weave +++ b/weave @@ -928,6 +928,9 @@ case "$COMMAND" in docker_bridge_ip echo -n $DOCKER_BRIDGE_IP ;; + ipam-cidr) + get_ipam_cidr "$@" && echo "$CIDR_ARGS" + ;; start) collect_cidr_args "$@" shift $CIDR_COUNT