From cc82213147c7d46cd1520d44c432846c9309db02 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 20 May 2015 16:01:17 +0100 Subject: [PATCH] add 'weave launch-proxy --with-ipam' flag --- proxy/proxy.go | 6 ++++-- proxy/start_container_interceptor.go | 16 ++++------------ site/proxy.md | 6 +++++- test/660_proxy_ipam_test.sh | 11 +++++++---- weave | 5 +---- weaveproxy/main.go | 5 +++-- 6 files changed, 24 insertions(+), 25 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index de565f6b..03ba90eb 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -22,9 +22,10 @@ type Proxy struct { client *docker.Client withDNS bool dockerBridgeIP string + withIPAM bool } -func NewProxy(targetURL string, withDNS bool) (*Proxy, error) { +func NewProxy(targetURL string, withDNS, withIPAM bool) (*Proxy, error) { u, err := url.Parse(targetURL) if err != nil { return nil, err @@ -58,6 +59,7 @@ func NewProxy(targetURL string, withDNS bool) (*Proxy, error) { client: client, withDNS: withDNS, dockerBridgeIP: string(dockerBridgeIP), + withIPAM: withIPAM, }, nil } @@ -68,7 +70,7 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { case containerCreateRegexp.MatchString(path): proxy.serveWithInterceptor(&createContainerInterceptor{proxy.client, proxy.withDNS, proxy.dockerBridgeIP}, w, r) case containerStartRegexp.MatchString(path): - proxy.serveWithInterceptor(&startContainerInterceptor{proxy.client, proxy.withDNS}, w, r) + proxy.serveWithInterceptor(&startContainerInterceptor{proxy.client, proxy.withDNS, proxy.withIPAM}, w, r) case execCreateRegexp.MatchString(path): proxy.serveWithInterceptor(&createExecInterceptor{proxy.client}, w, r) case strings.HasPrefix(path, "/weave"): diff --git a/proxy/start_container_interceptor.go b/proxy/start_container_interceptor.go index 8c159d34..bce86074 100644 --- a/proxy/start_container_interceptor.go +++ b/proxy/start_container_interceptor.go @@ -9,8 +9,9 @@ import ( ) type startContainerInterceptor struct { - client *docker.Client - withDNS bool + client *docker.Client + withDNS bool + withIPAM bool } func (i *startContainerInterceptor) InterceptRequest(r *http.Request) error { @@ -24,25 +25,16 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { } cidrs, ok := weaveCIDRsFromConfig(container.Config) - if !ok { + if !ok && !i.withIPAM { 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...) args = append(args, container.ID) if _, err := callWeave(args...); err != nil { Warning.Printf("Attaching container %s to weave network failed: %v", container.ID, err) - return nil } return nil } diff --git a/site/proxy.md b/site/proxy.md index 5ab679ff..9643b236 100644 --- a/site/proxy.md +++ b/site/proxy.md @@ -65,7 +65,11 @@ With this done, we can automtaically assign an address to a container by providi host1$ docker run -e WEAVE_CIDR= -ti ubuntu /bin/sh -More details on IPAM can be found in the [IPAM documentstion](ipam.html). +Alternatively, to enable automatic allocation of all containers without a `WEAVE_CIDR`, we can launch the proxy with the `--with-ipam` option. For example: + + host1$ weave launch-proxy --with-ipam + +More details on IPAM can be found in the [IPAM documentation](ipam.html). ## Limitations diff --git a/test/660_proxy_ipam_test.sh b/test/660_proxy_ipam_test.sh index 74a91ca1..601b2fd3 100755 --- a/test/660_proxy_ipam_test.sh +++ b/test/660_proxy_ipam_test.sh @@ -9,11 +9,14 @@ 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 +weave_on $HOST2 launch-proxy --with-ipam -proxy start_container $HOST1 -e WEAVE_CIDR= --name=c1 -proxy start_container $HOST2 -e WEAVE_CIDR= --name=c2 +proxy docker_on $HOST1 run -e WEAVE_CIDR= --name=c1 -dt $SMALL_IMAGE /bin/sh +proxy docker_on $HOST2 run --name=c2 -dt $SMALL_IMAGE /bin/sh + +C1=$(container_ip $HOST1 c1) C2=$(container_ip $HOST2 c2) -assert_raises "exec_on $HOST1 c1 $PING $C2" +assert_raises "proxy exec_on $HOST1 c1 $PING $C2" +assert_raises "proxy exec_on $HOST2 c2 $PING $C1" end_suite diff --git a/weave b/weave index 7bf657a7..87965cee 100755 --- a/weave +++ b/weave @@ -27,7 +27,7 @@ usage() { echo "weave setup" echo "weave launch [-password ] [-nickname ] [-iprange ] ..." echo "weave launch-dns " - echo "weave launch-proxy [-H ] [--with-dns]" + echo "weave launch-proxy [-H ] [--with-dns] [--with-ipam]" echo "weave connect " echo "weave forget " echo "weave run [--with-dns] [ ...] ..." @@ -928,9 +928,6 @@ 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 diff --git a/weaveproxy/main.go b/weaveproxy/main.go index 2a395ae1..2d0baf93 100644 --- a/weaveproxy/main.go +++ b/weaveproxy/main.go @@ -16,12 +16,13 @@ var ( func main() { var target, listen string - var withDNS, debug bool + var withDNS, withIPAM, debug bool getopt.BoolVarLong(&debug, "debug", 'd', "log debugging information") getopt.StringVar(&target, 'H', fmt.Sprintf("docker daemon URL to proxy (default %s)", defaultTarget)) getopt.StringVar(&listen, 'L', fmt.Sprintf("address on which to listen (default %s)", defaultListen)) getopt.BoolVarLong(&withDNS, "with-dns", 'w', "instruct created containers to use weaveDNS as their nameserver") + getopt.BoolVarLong(&withIPAM, "with-ipam", 'i', "automatically allocate addresses for containers without a WEAVE_CIDR") getopt.Parse() if target == "" { @@ -36,7 +37,7 @@ func main() { InitDefaultLogging(true) } - p, err := proxy.NewProxy(target, withDNS) + p, err := proxy.NewProxy(target, withDNS, withIPAM) if err != nil { Error.Fatalf("Could not start proxy: %s", err) }