From 8873d69b77b3d54cf5dd64e0045be7bfaa4420fa Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Thu, 11 Jun 2015 14:03:07 +0100 Subject: [PATCH] [proxy] Add --without-dns flag, and change meaning of --with-dns flag --- prog/weaveproxy/main.go | 7 +++++- proxy/create_container_interceptor.go | 13 +++++++---- proxy/proxy.go | 3 ++- site/proxy.md | 18 +++++++++------ test/630_proxy_dns_test.sh | 33 +++++++++++++++++++++++---- weave | 2 +- 6 files changed, 58 insertions(+), 18 deletions(-) diff --git a/prog/weaveproxy/main.go b/prog/weaveproxy/main.go index 4b8f039b..8f66e59f 100644 --- a/prog/weaveproxy/main.go +++ b/prog/weaveproxy/main.go @@ -36,7 +36,8 @@ func main() { getopt.BoolVarLong(&c.TLSConfig.Enabled, "tls", 0, "Use TLS; implied by --tlsverify") getopt.StringVarLong(&c.TLSConfig.Key, "tlskey", 0, "Path to TLS key file") getopt.BoolVarLong(&c.TLSConfig.Verify, "tlsverify", 0, "Use TLS and verify the remote") - getopt.BoolVarLong(&c.WithDNS, "with-dns", 'w', "instruct created containers to use weaveDNS as their nameserver") + getopt.BoolVarLong(&c.WithDNS, "with-dns", 'w', "instruct created containers to always use weaveDNS as their nameserver") + getopt.BoolVarLong(&c.WithoutDNS, "without-dns", 0, "instruct created containers to never use weaveDNS as their nameserver") getopt.BoolVarLong(&c.WithIPAM, "with-ipam", 'i', "automatically allocate addresses for containers without a WEAVE_CIDR") getopt.Parse() @@ -45,6 +46,10 @@ func main() { os.Exit(0) } + if c.WithDNS && c.WithoutDNS { + Error.Fatalf("Cannot use both '--with-dns' and '--without-dns' flags") + } + if debug { InitDefaultLogging(true) } diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index 22d29214..dbb3c920 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -81,7 +81,12 @@ func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Co } func (i *createContainerInterceptor) setWeaveDNS(container *createContainerRequestBody, r *http.Request) error { - if !i.proxy.WithDNS { + if i.proxy.WithoutDNS { + return nil + } + + dnsDomain, dnsRunning := i.getDNSDomain() + if !(dnsRunning || i.proxy.WithDNS) { return nil } @@ -95,13 +100,13 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque if container.Hostname == "" && name != "" { container.Hostname = name // Strip trailing period because it's unusual to see it used on the end of a host name - container.Domainname = strings.TrimSuffix(i.getDNSDomain(), ".") + container.Domainname = strings.TrimSuffix(dnsDomain, ".") } return nil } -func (i *createContainerInterceptor) getDNSDomain() (domain string) { +func (i *createContainerInterceptor) getDNSDomain() (domain string, running bool) { domain = nameserver.DefaultLocalDomain dnsContainer, err := i.proxy.client.InspectContainer("weavedns") if err != nil || @@ -122,7 +127,7 @@ func (i *createContainerInterceptor) getDNSDomain() (domain string) { return } - return string(b) + return string(b), true } func (i *createContainerInterceptor) InterceptResponse(r *http.Response) error { diff --git a/proxy/proxy.go b/proxy/proxy.go index 1751b48c..1c44b679 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -37,6 +37,7 @@ type Config struct { TLSConfig TLSConfig Version string WithDNS bool + WithoutDNS bool WithIPAM bool } @@ -61,7 +62,7 @@ func NewProxy(c Config) (*Proxy, error) { }, } - if p.WithDNS { + if !p.WithoutDNS { dockerBridgeIP, err := callWeave("docker-bridge-ip") if err != nil { return nil, err diff --git a/site/proxy.md b/site/proxy.md index 0c666afe..2ddc5d81 100644 --- a/site/proxy.md +++ b/site/proxy.md @@ -115,13 +115,17 @@ are handled as before. ## Automatic discovery -Containers started via the proxy are automatically registered in -[weaveDNS](weavedns.html) if they have a hostname in the weaveDNS -domain (usually `.weave.local`). In order for containers to be able to -look up such names, their DNS resolver needs to be configured to point -at weaveDNS. This can be done [manually](weavedns.html#without-run), -or automatically by the proxy by launching it with the `--with-dns` -option, i.e. +Containers started via the proxy are +automatically registered in [weaveDNS](weavedns.html) if they have a +hostname in the weaveDNS domain (usually `.weave.local`). In order for +containers to be able to look up such names, their DNS resolver needs +to be configured to point at weaveDNS. This will be done automatically by the proxy + while weaveDNS is running. To disable this launch the proxy with the `--without-dns` option, i.e. + + host1$ weave launch-proxy --without-dns + +To enable weaveDNS resolution when weaveDNS is not running, launch the +proxy with the `--with-dns` option, i.e. host1$ weave launch-proxy --with-dns diff --git a/test/630_proxy_dns_test.sh b/test/630_proxy_dns_test.sh index e0284f60..6fcf4c79 100755 --- a/test/630_proxy_dns_test.sh +++ b/test/630_proxy_dns_test.sh @@ -7,14 +7,39 @@ C2=10.2.0.34 C1_NAME=c1.weave.local C2_NAME=seetwo.weave.local +boot_containers() { + proxy docker_on $HOST1 run -e WEAVE_CIDR=$C2/24 -dt --name=c2 -h $C2_NAME $DNS_IMAGE /bin/sh + proxy docker_on $HOST1 run -e WEAVE_CIDR=$C1/24 -dt --name=c1 $DNS_IMAGE /bin/sh +} + +kill_containers() { + docker_on $HOST1 kill c1 c2 + docker_on $HOST1 rm c1 c2 +} + start_suite "Proxy registers containers with dns" -weave_on $HOST1 launch-dns 10.2.254.1/24 $WEAVEDNS_ARGS -weave_on $HOST1 launch-proxy --with-dns -proxy docker_on $HOST1 run -e WEAVE_CIDR=$C2/24 -dt --name=c2 -h $C2_NAME $DNS_IMAGE /bin/sh -proxy docker_on $HOST1 run -e WEAVE_CIDR=$C1/24 -dt --name=c1 $DNS_IMAGE /bin/sh +bridge_ip=$(weave_on $HOST1 docker-bridge-ip) +weave_on $HOST1 launch-proxy --with-dns +boot_containers +assert "exec_on $HOST1 c1 cat /etc/resolv.conf" "nameserver $bridge_ip" +assert "exec_on $HOST1 c2 cat /etc/resolv.conf" "nameserver $bridge_ip" +weave_on $HOST1 stop-proxy +kill_containers + +weave_on $HOST1 launch-dns 10.2.254.1/24 $WEAVEDNS_ARGS + +weave_on $HOST1 launch-proxy +boot_containers assert_dns_record $HOST1 c1 $C2_NAME $C2 assert_dns_record $HOST1 c2 $C1_NAME $C1 +weave_on $HOST1 stop-proxy +kill_containers + +weave_on $HOST1 launch-proxy --without-dns +boot_containers +assert_no_dns_record $HOST1 c1 $C2_NAME +assert_no_dns_record $HOST1 c2 $C1_NAME end_suite diff --git a/weave b/weave index 339a1c0e..9b3cfadc 100755 --- a/weave +++ b/weave @@ -29,7 +29,7 @@ usage() { echo "weave launch [-password ] [-nickname ] [-nodiscovery]" echo " [-iprange [-ipsubnet ]] ..." echo "weave launch-dns []" - echo "weave launch-proxy [-D ] [-H ] [--with-dns] [--with-ipam]" + echo "weave launch-proxy [-D ] [-H ] [--with-dns|--without-dns] [--with-ipam]" echo "weave connect [--replace] [ ...]" echo "weave forget ..." echo "weave run [--with-dns] [ ...] ..."