From b9850ac200c0995c0f63bf648dbb931fdd2fbbe7 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Tue, 9 Jun 2015 17:20:47 +0100 Subject: [PATCH] [proxy] Set hostname and domainname based on container name, when dns Had to make the assert_dns_record test helper more robust against 'getent hosts' returning duplicates --- proxy/create_container_interceptor.go | 13 +++++++++++-- test/630_proxy_dns_test.sh | 10 ++++++---- test/config.sh | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index 4bd59e61..cedf2ef0 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -9,6 +9,7 @@ import ( "github.com/fsouza/go-dockerclient" . "github.com/weaveworks/weave/common" + "github.com/weaveworks/weave/nameserver" ) type createContainerInterceptor struct{ proxy *Proxy } @@ -43,7 +44,7 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { if err := i.setWeaveWaitEntrypoint(container.Config); err != nil { return err } - if err := i.setWeaveDNS(&container); err != nil { + if err := i.setWeaveDNS(&container, r); err != nil { return err } } @@ -78,7 +79,7 @@ func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Co return nil } -func (i *createContainerInterceptor) setWeaveDNS(container *createContainerRequestBody) error { +func (i *createContainerInterceptor) setWeaveDNS(container *createContainerRequestBody, r *http.Request) error { if !i.proxy.WithDNS { return nil } @@ -88,6 +89,14 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque if len(container.HostConfig.DNSSearch) == 0 { container.HostConfig.DNSSearch = []string{"."} } + + name := r.URL.Query().Get("name") + if container.Hostname == "" && name != "" { + container.Hostname = name + // TODO: Get weaveDNS domain + container.Domainname = nameserver.DefaultLocalDomain + } + return nil } diff --git a/test/630_proxy_dns_test.sh b/test/630_proxy_dns_test.sh index 21cd42c4..344d2951 100755 --- a/test/630_proxy_dns_test.sh +++ b/test/630_proxy_dns_test.sh @@ -4,15 +4,17 @@ C1=10.2.0.78 C2=10.2.0.34 -NAME=seetwo.weave.local +C1_NAME=c1.weave.local +C2_NAME=seetwo.weave.local start_suite "Proxy registers containers with dns" weave_on $HOST1 launch-dns 10.2.254.1/24 weave_on $HOST1 launch-proxy --with-dns -proxy docker_on $HOST1 run -e WEAVE_CIDR=$C2/24 -dt --name=c2 -h $NAME $SMALL_IMAGE /bin/sh -proxy docker_on $HOST1 run -e WEAVE_CIDR=$C1/24 -dt --name=c1 $DNS_IMAGE /bin/sh +proxy docker_on $HOST1 run -e WEAVE_CIDR=$C2/24 -dt --name=c2 -h $C2_NAME $SMALL_IMAGE /bin/sh +proxy docker_on $HOST1 run -e WEAVE_CIDR=$C1/24 -dt --name=c1 $DNS_IMAGE /bin/sh -assert_dns_record $HOST1 c1 $NAME $C2 +assert_dns_record $HOST1 c1 $C2_NAME $C2 +assert_dns_record $HOST1 c2 $C1_NAME $C1 end_suite diff --git a/test/config.sh b/test/config.sh index 5efbf5e7..a0ec0074 100644 --- a/test/config.sh +++ b/test/config.sh @@ -128,7 +128,7 @@ assert_dns_record() { [ -z "$DEBUG" ] || greyly echo "Checking whether the IPs '$@' exists at $host:$container" for ip in "$@" ; do - assert "exec_on $host $container getent hosts $ip | tr -s ' '" "$ip $name" + assert "exec_on $host $container getent hosts $ip | awk '{print \$2}'" "$name" done }