From a1007411ade94f63ea4cae815ef4280c3e1c6512 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 10 Jun 2015 10:04:32 +0100 Subject: [PATCH 01/17] Add /domain resource to DNS REST API --- nameserver/http.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nameserver/http.go b/nameserver/http.go index ce438bee..2854d5f8 100644 --- a/nameserver/http.go +++ b/nameserver/http.go @@ -34,6 +34,10 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain fmt.Fprintln(w, db.Status()) }) + muxRouter.Methods("GET").Path("/domain").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, db.Domain()) + }) + muxRouter.Methods("PUT").Path("/name/{id:.+}/{ip:.+}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reqError := func(msg string, logmsg string, logargs ...interface{}) { httpErrorAndLog(Warning, w, msg, http.StatusBadRequest, logmsg, logargs...) From 9fddad02adaa100a03c007de1e6b7cece3918030 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 10 Jun 2015 11:14:51 +0100 Subject: [PATCH 02/17] Add dns_domain helper to weave script --- weave | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/weave b/weave index 0383ac6b..4b1aeee0 100755 --- a/weave +++ b/weave @@ -660,6 +660,16 @@ dns_args() { DNS_ARGS="$DNS_ARG $DNS_SEARCH_ARG" } +# Use the weaveDNS REST API to determine the local domain. If weaveDNS is not +# running, return the default value ('weave.local.') +dns_domain() { + if DOMAIN=$(http_call $DNS_CONTAINER_NAME $DNS_HTTP_PORT GET /domain 2>/dev/null) ; then + echo $DOMAIN + else + echo 'weave.local.' + fi +} + # Print an error to stderr and return with an indicative exit status # if the container $1 does not exist or isn't running. check_running() { @@ -1069,6 +1079,9 @@ case "$COMMAND" in dns_args "$@" echo -n $DNS_ARGS ;; + dns-domain) + echo $(dns_domain) + ;; docker-bridge-ip) docker_bridge_ip echo -n $DOCKER_BRIDGE_IP From b9850ac200c0995c0f63bf648dbb931fdd2fbbe7 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Tue, 9 Jun 2015 17:20:47 +0100 Subject: [PATCH 03/17] [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 } From 99d9ad704275ef1bde76006da92ebf93ce30550b Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 10 Jun 2015 10:25:34 +0100 Subject: [PATCH 04/17] [proxy] query weavedns for domain --- proxy/create_container_interceptor.go | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index cedf2ef0..616d4d93 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -3,6 +3,7 @@ package proxy import ( "bytes" "encoding/json" + "fmt" "io/ioutil" "net/http" "strings" @@ -93,13 +94,36 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque name := r.URL.Query().Get("name") if container.Hostname == "" && name != "" { container.Hostname = name - // TODO: Get weaveDNS domain - container.Domainname = nameserver.DefaultLocalDomain + container.Domainname = i.getDNSDomain() } return nil } +func (i *createContainerInterceptor) getDNSDomain() (domain string) { + domain = nameserver.DefaultLocalDomain + dnsContainer, err := i.proxy.client.InspectContainer("weavedns") + if err != nil || + dnsContainer.NetworkSettings == nil || + dnsContainer.NetworkSettings.IPAddress == "" { + return + } + + url := fmt.Sprintf("http://%s:6785/domain", dnsContainer.NetworkSettings.IPAddress) + resp, err := http.Get(url) + if err != nil || resp.StatusCode != http.StatusOK { + return + } + defer resp.Body.Close() + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return + } + + return string(b) +} + func (i *createContainerInterceptor) InterceptResponse(r *http.Response) error { return nil } From 678063efece3159759162966183194f9474a0ac9 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 10 Jun 2015 16:44:40 +0100 Subject: [PATCH 05/17] updating proxy docs on hostname/domainname dns --- site/proxy.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/site/proxy.md b/site/proxy.md index 5f2ae23b..0c666afe 100644 --- a/site/proxy.md +++ b/site/proxy.md @@ -127,7 +127,9 @@ option, i.e. Now any containers started via the proxy, and with a `WEAVE_CIDR=...` environment variable (or even without it, if IPAM is -[configured](#ipam)), will use weaveDNS for name resolution. +[configured](#ipam)), will use weaveDNS for name resolution. If not +provided, the hostname will default to the container name and will be +registered in weaveDNS. ## Multi-host example From 108eea8b79e241aba9d828a01c9279eeb55aa93b Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 10 Jun 2015 16:42:59 +0100 Subject: [PATCH 06/17] Modify `weave run` to use container name as fqdn When run is invoked with `--with-dns` and a container name but no hostname, synthesise a fully qualified hostname from the container name and the weaveDNS domain and supply that as a hostname for the container. --- weave | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/weave b/weave index 4b1aeee0..9a5f8f08 100755 --- a/weave +++ b/weave @@ -648,25 +648,34 @@ dns_args() { docker_bridge_ip DNS_ARG="--dns $DOCKER_BRIDGE_IP" DNS_SEARCH_ARG="--dns-search=." + HOSTNAME_ARG="" + HOSTNAME_SPECIFIED=false for arg in "$@" ; do case $arg in --dns-search=*) DNS_SEARCH_ARG="" ;; - *) + --name=*) + HOSTNAME_ARG="--hostname=${arg#--name=}.$(dns_domain)" + ;; + -h|--hostname=*) + HOSTNAME_SPECIFIED=true ;; esac done - DNS_ARGS="$DNS_ARG $DNS_SEARCH_ARG" + if [ "$HOSTNAME_SPECIFIED" = "true" ] ; then + HOSTNAME_ARG="" + fi + DNS_ARGS="$DNS_ARG $DNS_SEARCH_ARG $HOSTNAME_ARG" } # Use the weaveDNS REST API to determine the local domain. If weaveDNS is not -# running, return the default value ('weave.local.') +# running, return the default value ('weave.local') dns_domain() { if DOMAIN=$(http_call $DNS_CONTAINER_NAME $DNS_HTTP_PORT GET /domain 2>/dev/null) ; then - echo $DOMAIN + echo ${DOMAIN%.} else - echo 'weave.local.' + echo 'weave.local' fi } From bed6a130d6df41561c4526850d9fa9865251358c Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 10 Jun 2015 16:50:43 +0100 Subject: [PATCH 07/17] Smoke test for new `weave run` behaviour --- test/260_use_name_as_hostname_test.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 test/260_use_name_as_hostname_test.sh diff --git a/test/260_use_name_as_hostname_test.sh b/test/260_use_name_as_hostname_test.sh new file mode 100755 index 00000000..f4073487 --- /dev/null +++ b/test/260_use_name_as_hostname_test.sh @@ -0,0 +1,26 @@ +#! /bin/bash + +. ./config.sh + +# Docker inspect hostname + domainname of container $2 on host $1 +docker_inspect_fqdn() { + docker_on $1 inspect --format='{{.Config.Hostname}}.{{.Config.Domainname}}' $2 +} + +start_suite "Use container name as hostname" + +weave_on $HOST1 launch -iprange 10.2.0.0/24 + +start_container_with_dns $HOST1 --name=c1-name +start_container_with_dns $HOST1 --name=c2-name -h c2-hostname.weave.local +start_container_with_dns $HOST1 --name=c3-name --hostname=c3-hostname.weave.local +start_container_with_dns $HOST1 -h c4-hostname.weave.local --name=c4-name +start_container_with_dns $HOST1 --hostname=c5-hostname.weave.local --name=c5-name + +assert "docker_inspect_fqdn $HOST1 c1-name" "c1-name.weave.local" +assert "docker_inspect_fqdn $HOST1 c2-name" "c2-hostname.weave.local" +assert "docker_inspect_fqdn $HOST1 c3-name" "c3-hostname.weave.local" +assert "docker_inspect_fqdn $HOST1 c4-name" "c4-hostname.weave.local" +assert "docker_inspect_fqdn $HOST1 c5-name" "c5-hostname.weave.local" + +end_suite From bccb5065db20d9d624d845f56cf032a064f03e83 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 10 Jun 2015 17:27:54 +0100 Subject: [PATCH 08/17] Update docs with new `weave run` behaviour --- site/weavedns.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/site/weavedns.md b/site/weavedns.md index 4564482f..c23189ac 100644 --- a/site/weavedns.md +++ b/site/weavedns.md @@ -30,7 +30,8 @@ on every weave host, by invoking the weave script command `launch-dns`. Application containers are then instructed to use weaveDNS as their nameserver by supplying the `--with-dns` option when starting them. Giving any container a hostname in the `.weave.local` -domain registers it in weaveDNS. For example: +domain registers it in weaveDNS; containers started with `--with-dns` +have a default hostname derived from their container name. For example: ```bash $ weave launch From 4f6ace8b109ae9d9e91474dd95054e7fababa7fc Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 10 Jun 2015 18:34:47 +0100 Subject: [PATCH 09/17] Permit optional trailing dot on hostname --- test/260_use_name_as_hostname_test.sh | 18 +++++++++--------- weave | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/test/260_use_name_as_hostname_test.sh b/test/260_use_name_as_hostname_test.sh index f4073487..e5d22b5d 100755 --- a/test/260_use_name_as_hostname_test.sh +++ b/test/260_use_name_as_hostname_test.sh @@ -12,15 +12,15 @@ start_suite "Use container name as hostname" weave_on $HOST1 launch -iprange 10.2.0.0/24 start_container_with_dns $HOST1 --name=c1-name -start_container_with_dns $HOST1 --name=c2-name -h c2-hostname.weave.local -start_container_with_dns $HOST1 --name=c3-name --hostname=c3-hostname.weave.local -start_container_with_dns $HOST1 -h c4-hostname.weave.local --name=c4-name -start_container_with_dns $HOST1 --hostname=c5-hostname.weave.local --name=c5-name +start_container_with_dns $HOST1 --name=c2-name -h c2-hostname.weave.local. +start_container_with_dns $HOST1 --name=c3-name --hostname=c3-hostname.weave.local. +start_container_with_dns $HOST1 -h c4-hostname.weave.local. --name=c4-name +start_container_with_dns $HOST1 --hostname=c5-hostname.weave.local. --name=c5-name -assert "docker_inspect_fqdn $HOST1 c1-name" "c1-name.weave.local" -assert "docker_inspect_fqdn $HOST1 c2-name" "c2-hostname.weave.local" -assert "docker_inspect_fqdn $HOST1 c3-name" "c3-hostname.weave.local" -assert "docker_inspect_fqdn $HOST1 c4-name" "c4-hostname.weave.local" -assert "docker_inspect_fqdn $HOST1 c5-name" "c5-hostname.weave.local" +assert "docker_inspect_fqdn $HOST1 c1-name" "c1-name.weave.local." +assert "docker_inspect_fqdn $HOST1 c2-name" "c2-hostname.weave.local." +assert "docker_inspect_fqdn $HOST1 c3-name" "c3-hostname.weave.local." +assert "docker_inspect_fqdn $HOST1 c4-name" "c4-hostname.weave.local." +assert "docker_inspect_fqdn $HOST1 c5-name" "c5-hostname.weave.local." end_suite diff --git a/weave b/weave index 9a5f8f08..c2fc26f3 100755 --- a/weave +++ b/weave @@ -670,12 +670,12 @@ dns_args() { } # Use the weaveDNS REST API to determine the local domain. If weaveDNS is not -# running, return the default value ('weave.local') +# running, return the default value ('weave.local.') dns_domain() { if DOMAIN=$(http_call $DNS_CONTAINER_NAME $DNS_HTTP_PORT GET /domain 2>/dev/null) ; then - echo ${DOMAIN%.} + echo $DOMAIN else - echo 'weave.local' + echo 'weave.local.' fi } @@ -706,9 +706,9 @@ with_container_fqdn() { COMMAND="$2" shift 2 - CONTAINER_FQDN=$(docker inspect --format='{{.Config.Hostname}}.{{.Config.Domainname}}.' $CONTAINER 2>/dev/null) || return 0 + CONTAINER_FQDN=$(docker inspect --format='{{.Config.Hostname}}.{{.Config.Domainname}}' $CONTAINER 2>/dev/null) || return 0 - if echo "$CONTAINER_FQDN" | grep -qv '\.\.$' ; then + if echo "$CONTAINER_FQDN" | grep -Eq '[^.]+\..+' ; then $COMMAND "$CONTAINER" "$CONTAINER_FQDN" "$@" fi } From a424bfe715a88acc0de53e60aa5d197884df369d Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Wed, 10 Jun 2015 19:34:51 +0100 Subject: [PATCH 10/17] Correctly interpret long form arguments that lack an equals symbol --- test/260_use_name_as_hostname_test.sh | 8 ++++++-- weave | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/test/260_use_name_as_hostname_test.sh b/test/260_use_name_as_hostname_test.sh index e5d22b5d..005a4f4c 100755 --- a/test/260_use_name_as_hostname_test.sh +++ b/test/260_use_name_as_hostname_test.sh @@ -14,13 +14,17 @@ weave_on $HOST1 launch -iprange 10.2.0.0/24 start_container_with_dns $HOST1 --name=c1-name start_container_with_dns $HOST1 --name=c2-name -h c2-hostname.weave.local. start_container_with_dns $HOST1 --name=c3-name --hostname=c3-hostname.weave.local. -start_container_with_dns $HOST1 -h c4-hostname.weave.local. --name=c4-name -start_container_with_dns $HOST1 --hostname=c5-hostname.weave.local. --name=c5-name +start_container_with_dns $HOST1 --name=c4-name --hostname c4-hostname.weave.local. +start_container_with_dns $HOST1 -h c5-hostname.weave.local. --name=c5-name +start_container_with_dns $HOST1 --hostname=c6-hostname.weave.local. --name=c6-name +start_container_with_dns $HOST1 --hostname c7-hostname.weave.local. --name=c7-name assert "docker_inspect_fqdn $HOST1 c1-name" "c1-name.weave.local." assert "docker_inspect_fqdn $HOST1 c2-name" "c2-hostname.weave.local." assert "docker_inspect_fqdn $HOST1 c3-name" "c3-hostname.weave.local." assert "docker_inspect_fqdn $HOST1 c4-name" "c4-hostname.weave.local." assert "docker_inspect_fqdn $HOST1 c5-name" "c5-hostname.weave.local." +assert "docker_inspect_fqdn $HOST1 c6-name" "c6-hostname.weave.local." +assert "docker_inspect_fqdn $HOST1 c7-name" "c7-hostname.weave.local." end_suite diff --git a/weave b/weave index c2fc26f3..eb161e14 100755 --- a/weave +++ b/weave @@ -650,16 +650,26 @@ dns_args() { DNS_SEARCH_ARG="--dns-search=." HOSTNAME_ARG="" HOSTNAME_SPECIFIED=false - for arg in "$@" ; do - case $arg in + while [ $# -gt 0 ] ; do + case "$1" in --dns-search=*) DNS_SEARCH_ARG="" + shift + ;; + --name) + HOSTNAME_ARG="--hostname=$2.$(dns_domain)" + shift 2 ;; --name=*) - HOSTNAME_ARG="--hostname=${arg#--name=}.$(dns_domain)" + HOSTNAME_ARG="--hostname=${1#*=}.$(dns_domain)" + shift ;; - -h|--hostname=*) + -h|--hostname|--hostname=*) HOSTNAME_SPECIFIED=true + shift + ;; + *) + shift ;; esac done From 96bda1639e21bea89380d9d62455103e3d71caa0 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Thu, 11 Jun 2015 10:25:27 +0100 Subject: [PATCH 11/17] define weavedns http port as a constant --- nameserver/server.go | 1 + prog/weavedns/main.go | 2 +- proxy/create_container_interceptor.go | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/nameserver/server.go b/nameserver/server.go index 598e8171..194a2eb4 100644 --- a/nameserver/server.go +++ b/nameserver/server.go @@ -15,6 +15,7 @@ import ( const ( DefaultServerPort = 53 // The default server port + DefaultHTTPPort = 6785 // The default http port DefaultCLICfgFile = "/etc/resolv.conf" // default "resolv.conf" file to try to load DefaultLocalTTL = 30 // default TTL for responses for local domain queries DefaultUDPBuflen = 4096 // bigger than the default 512 diff --git a/prog/weavedns/main.go b/prog/weavedns/main.go index 9b6ab270..8702719a 100644 --- a/prog/weavedns/main.go +++ b/prog/weavedns/main.go @@ -46,7 +46,7 @@ func main() { flag.StringVar(&domain, "domain", weavedns.DefaultLocalDomain, "local domain (ie, 'weave.local.')") flag.IntVar(&wait, "wait", 0, "number of seconds to wait for interface to be created and come up") flag.IntVar(&dnsPort, "dnsport", weavedns.DefaultServerPort, "port to listen to DNS requests") - flag.IntVar(&httpPort, "httpport", 6785, "port to listen to HTTP requests") + flag.IntVar(&httpPort, "httpport", weavedns.DefaultHTTPPort, "port to listen to HTTP requests") flag.IntVar(&cacheLen, "cache", weavedns.DefaultCacheLen, "cache length") flag.IntVar(&ttl, "ttl", weavedns.DefaultLocalTTL, "TTL (in secs) for responses for local names") flag.BoolVar(&watch, "watch", true, "watch the docker socket for container events") diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index 616d4d93..b1f10522 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -109,7 +109,7 @@ func (i *createContainerInterceptor) getDNSDomain() (domain string) { return } - url := fmt.Sprintf("http://%s:6785/domain", dnsContainer.NetworkSettings.IPAddress) + url := fmt.Sprintf("http://%s:%d/domain", dnsContainer.NetworkSettings.IPAddress, nameserver.DefaultHTTPPort) resp, err := http.Get(url) if err != nil || resp.StatusCode != http.StatusOK { return From 35cfafc319f616c7326acebbf315d965a7a99bd8 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Thu, 11 Jun 2015 11:52:33 +0100 Subject: [PATCH 12/17] Defer evaluation of $(dns_domain) until necessary --- weave | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/weave b/weave index eb161e14..27e8f27d 100755 --- a/weave +++ b/weave @@ -648,7 +648,7 @@ dns_args() { docker_bridge_ip DNS_ARG="--dns $DOCKER_BRIDGE_IP" DNS_SEARCH_ARG="--dns-search=." - HOSTNAME_ARG="" + NAME_ARG="" HOSTNAME_SPECIFIED=false while [ $# -gt 0 ] ; do case "$1" in @@ -657,11 +657,11 @@ dns_args() { shift ;; --name) - HOSTNAME_ARG="--hostname=$2.$(dns_domain)" + NAME_ARG="$2" shift 2 ;; --name=*) - HOSTNAME_ARG="--hostname=${1#*=}.$(dns_domain)" + NAME_ARG="${1#*=}" shift ;; -h|--hostname|--hostname=*) @@ -673,10 +673,10 @@ dns_args() { ;; esac done - if [ "$HOSTNAME_SPECIFIED" = "true" ] ; then - HOSTNAME_ARG="" + DNS_ARGS="$DNS_ARG $DNS_SEARCH_ARG" + if [ -n "$NAME_ARG" -a "$HOSTNAME_SPECIFIED" = "false" ] ; then + DNS_ARGS="$DNS_ARGS --hostname=$NAME_ARG.$(dns_domain)" fi - DNS_ARGS="$DNS_ARG $DNS_SEARCH_ARG $HOSTNAME_ARG" } # Use the weaveDNS REST API to determine the local domain. If weaveDNS is not From 6b80dc035aa53754fd3d14213e0a360fe469ddaf Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Thu, 11 Jun 2015 11:53:24 +0100 Subject: [PATCH 13/17] Simplify smoke test --- test/260_use_name_as_hostname_test.sh | 36 ++++++++++++++++----------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/test/260_use_name_as_hostname_test.sh b/test/260_use_name_as_hostname_test.sh index 005a4f4c..317d893f 100755 --- a/test/260_use_name_as_hostname_test.sh +++ b/test/260_use_name_as_hostname_test.sh @@ -2,29 +2,35 @@ . ./config.sh +NAME=c1 +DOMAIN=weave.local. +HOSTNAME=$NAME-hostname.$DOMAIN + # Docker inspect hostname + domainname of container $2 on host $1 docker_inspect_fqdn() { docker_on $1 inspect --format='{{.Config.Hostname}}.{{.Config.Domainname}}' $2 } +# Start container with args $2.. and assert fqdn of $1 +assert_expected_fqdn() { + EXPECTED_FQDN=$1 + shift + start_container_with_dns $HOST1 "$@" + assert "docker_inspect_fqdn $HOST1 $NAME" $EXPECTED_FQDN + docker_on $HOST1 rm -f $NAME +} + start_suite "Use container name as hostname" weave_on $HOST1 launch -iprange 10.2.0.0/24 -start_container_with_dns $HOST1 --name=c1-name -start_container_with_dns $HOST1 --name=c2-name -h c2-hostname.weave.local. -start_container_with_dns $HOST1 --name=c3-name --hostname=c3-hostname.weave.local. -start_container_with_dns $HOST1 --name=c4-name --hostname c4-hostname.weave.local. -start_container_with_dns $HOST1 -h c5-hostname.weave.local. --name=c5-name -start_container_with_dns $HOST1 --hostname=c6-hostname.weave.local. --name=c6-name -start_container_with_dns $HOST1 --hostname c7-hostname.weave.local. --name=c7-name - -assert "docker_inspect_fqdn $HOST1 c1-name" "c1-name.weave.local." -assert "docker_inspect_fqdn $HOST1 c2-name" "c2-hostname.weave.local." -assert "docker_inspect_fqdn $HOST1 c3-name" "c3-hostname.weave.local." -assert "docker_inspect_fqdn $HOST1 c4-name" "c4-hostname.weave.local." -assert "docker_inspect_fqdn $HOST1 c5-name" "c5-hostname.weave.local." -assert "docker_inspect_fqdn $HOST1 c6-name" "c6-hostname.weave.local." -assert "docker_inspect_fqdn $HOST1 c7-name" "c7-hostname.weave.local." +assert_expected_fqdn "$NAME.$DOMAIN" --name=$NAME +assert_expected_fqdn "$NAME.$DOMAIN" --name $NAME +assert_expected_fqdn "$HOSTNAME" --name=$NAME -h $HOSTNAME +assert_expected_fqdn "$HOSTNAME" --name=$NAME --hostname=$HOSTNAME +assert_expected_fqdn "$HOSTNAME" --name=$NAME --hostname $HOSTNAME +assert_expected_fqdn "$HOSTNAME" -h $HOSTNAME --name=$NAME +assert_expected_fqdn "$HOSTNAME" --hostname=$HOSTNAME --name=$NAME +assert_expected_fqdn "$HOSTNAME" --hostname $HOSTNAME --name=$NAME end_suite From 1fa7c24a2add850149573b4cd9fb054510c53b1c Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Thu, 11 Jun 2015 11:54:05 +0100 Subject: [PATCH 14/17] Prefer --name to -h in examples --- site/weavedns.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/site/weavedns.md b/site/weavedns.md index c23189ac..c8e1fa86 100644 --- a/site/weavedns.md +++ b/site/weavedns.md @@ -37,7 +37,7 @@ have a default hostname derived from their container name. For example: $ weave launch $ weave launch-dns 10.2.254.1/24 $ weave run 10.2.1.25/24 -ti -h pingme.weave.local ubuntu -$ shell1=$(weave run --with-dns 10.2.1.26/24 -ti -h ubuntu.weave.local ubuntu) +$ shell1=$(weave run --with-dns 10.2.1.26/24 -ti --name=ubuntu ubuntu) $ docker attach $shell1 root@ubuntu:/# ping pingme @@ -56,7 +56,7 @@ use weaveDNS on a second host we would run: ```bash host2$ weave launch $HOST1 host2$ weave launch-dns 10.2.254.2/24 -host2$ shell2=$(weave run --with-dns 10.2.1.36/24 -ti -h ubuntu2.weave.local ubuntu) +host2$ shell2=$(weave run --with-dns 10.2.1.36/24 -ti --name=ubuntu2 ubuntu) host2$ docker attach $shell2 root@ubuntu2:/# ping pingme @@ -111,7 +111,7 @@ Returning to our earlier example, let us start an additional `pingme` container, this time on the 2nd host, and then run some ping tests... ```bash -host2$ weave run 10.2.1.35/24 -ti -h pingme.weave.local ubuntu +host2$ weave run --with-dns 10.2.1.35/24 -ti --name=pingme ubuntu host2$ docker attach $shell2 root@ubuntu2:/# ping -nq -c 1 pingme From 8d7fe2b9e7fad5136a6ea90dd4b59470b365e901 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Thu, 11 Jun 2015 11:55:21 +0100 Subject: [PATCH 15/17] Renamed smoke test to avoid number clash --- ...e_name_as_hostname_test.sh => 270_use_name_as_hostname_test.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/260_use_name_as_hostname_test.sh => 270_use_name_as_hostname_test.sh (100%) diff --git a/test/260_use_name_as_hostname_test.sh b/270_use_name_as_hostname_test.sh similarity index 100% rename from test/260_use_name_as_hostname_test.sh rename to 270_use_name_as_hostname_test.sh From d2b17690f91d30570138db8d094b274f2dfcbe8b Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Thu, 11 Jun 2015 12:13:01 +0100 Subject: [PATCH 16/17] use dnsutils image in one test instead of awk on all the tests. --- test/630_proxy_dns_test.sh | 4 ++-- test/config.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/630_proxy_dns_test.sh b/test/630_proxy_dns_test.sh index 344d2951..ddc9b2d4 100755 --- a/test/630_proxy_dns_test.sh +++ b/test/630_proxy_dns_test.sh @@ -11,8 +11,8 @@ 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 $C2_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 $DNS_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 $C2_NAME $C2 assert_dns_record $HOST1 c2 $C1_NAME $C1 diff --git a/test/config.sh b/test/config.sh index a0ec0074..5efbf5e7 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 | awk '{print \$2}'" "$name" + assert "exec_on $host $container getent hosts $ip | tr -s ' '" "$ip $name" done } From 12fc760e4a1b801426f715de4f77b34ae12e40bb Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Thu, 11 Jun 2015 12:16:51 +0100 Subject: [PATCH 17/17] Refocus documentation around --name --- site/weavedns.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/site/weavedns.md b/site/weavedns.md index c8e1fa86..bddd0bab 100644 --- a/site/weavedns.md +++ b/site/weavedns.md @@ -29,14 +29,13 @@ other over the weave network. One such container needs to be started on every weave host, by invoking the weave script command `launch-dns`. Application containers are then instructed to use weaveDNS as their nameserver by supplying the `--with-dns` option when -starting them. Giving any container a hostname in the `.weave.local` -domain registers it in weaveDNS; containers started with `--with-dns` -have a default hostname derived from their container name. For example: +starting them; containers so started also automatically register their +container name in the weaveDNS domain. For example: ```bash $ weave launch $ weave launch-dns 10.2.254.1/24 -$ weave run 10.2.1.25/24 -ti -h pingme.weave.local ubuntu +$ weave run --with-dns 10.2.1.25/24 -ti --name=pingme ubuntu $ shell1=$(weave run --with-dns 10.2.1.26/24 -ti --name=ubuntu ubuntu) $ docker attach $shell1 @@ -44,6 +43,14 @@ root@ubuntu:/# ping pingme ... ``` +If you start an application container sans `--with-dns` you can still register +it in weaveDNS simply by giving the container a hostname in the +`.weave.local.` domain: + +``` +$ weave run 10.2.1.25/24 -ti -h pingme.weave.local ubuntu +``` + Each weaveDNS container started with `launch-dns` needs to be given its own, unique, IP address, in a subnet that is a) common to all weaveDNS containers, b) disjoint from the application subnets, and c)