From 0d165487b8688ddde3b8816ca8d109ee129d2e55 Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Tue, 29 Sep 2015 11:55:07 +0200 Subject: [PATCH 1/5] Rewrite /etc/hosts in the containers --- weave | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 7 deletions(-) diff --git a/weave b/weave index 4e85fc9b..47d63de0 100755 --- a/weave +++ b/weave @@ -64,8 +64,8 @@ weave connect [--replace] [ ...] weave forget ... weave status [targets | connections | peers | dns] weave report [-f ] -weave run [--with-dns | --without-dns] [ ...] - ... +weave run [--with-dns | --without-dns] [--no-rewrite-hosts] + [ ...] ... weave start [ ...] weave attach [ ...] weave detach [ ...] @@ -207,6 +207,26 @@ dns_arg_count() { fi } +extra_hosts_args() { + DNS_EXTRA_HOSTS= + DNS_EXTRA_HOSTS_ARGS= + while [ $# -gt 0 ] ; do + case "$1" in + --add-host) + DNS_EXTRA_HOSTS="$2 $DNS_EXTRA_HOSTS" + DNS_EXTRA_HOSTS_ARGS="--add-host=$2 $DNS_EXTRA_HOSTS_ARGS" + shift + ;; + --add-host=*) + DNS_EXTRA_HOSTS="${1#*=} $DNS_EXTRA_HOSTS" + DNS_EXTRA_HOSTS_ARGS="--add-host=${1#*=} $DNS_EXTRA_HOSTS_ARGS" + ;; + esac + shift + done +} + + check_docker_version if [ "$1" = "--local" ] ; then @@ -244,10 +264,16 @@ elif [ "$1" = "run" -a -z "$IS_LOCAL" ] ; then DNS_ARGS=$(exec_remote dns-args "$@" || true) fi shift $(dns_arg_count "$@") + if [ "$1" = "--no-rewrite-hosts" ] ; then + shift + else + extra_hosts_args "$@" + DNS_EXTRA_HOSTS_ARGS="--rewrite-hosts $DNS_EXTRA_HOSTS_ARGS" + fi collect_cidr_args "$@" shift $CIDR_ARG_COUNT CONTAINER=$(docker $DOCKER_CLIENT_ARGS run -e WEAVE_CIDR=none $DNS_ARGS -d "$@") - exec_remote attach $CIDR_ARGS --or-die $CONTAINER >/dev/null + exec_remote attach $CIDR_ARGS --or-die $DNS_EXTRA_HOSTS_ARGS $CONTAINER >/dev/null echo $CONTAINER exit 0 fi @@ -955,6 +981,50 @@ dns_args() { fi } +etc_hosts_contents() { + NAME=$1 + shift + echo "# created by Weave - BEGIN" + echo "# container hostname" + for CIDR in $ALL_CIDRS ; do + echo "${CIDR%/*} $NAME" + done + echo + echo "# static names added with --add-host" + for EXTRA_HOST in "$@" ; do + echo "${EXTRA_HOST#*:} ${EXTRA_HOST%:*}" + done + + cat <<-EOF + +# default localhost entries +127.0.0.1 localhost +::1 ip6-localhost ip6-loopback +fe00::0 ip6-localnet +ff00::0 ip6-mcastprefix +ff02::1 ip6-allnodes +ff02::2 ip6-allrouters +EOF + + echo "# created by Weave - END" +} + +rewrite_etc_hosts() { + HOSTS_PATH_AND_NAME=$(docker inspect -f '{{.HostsPath}} {{.Config.Hostname}}' $CONTAINER) + HOSTS=${HOSTS_PATH_AND_NAME% *} + NAME=${HOSTS_PATH_AND_NAME#* } + CONTAINERS_PATH=$(dirname $HOSTS) + MNT=/container + MNT_HOSTS=$MNT/$(basename $HOSTS) + CONTENTS="$(etc_hosts_contents $NAME $@)" + # rewrite /etc/hosts, unlinking the file (so Docker does not modify it again) but + # leaving it with valid contents... + docker run --rm \ + -v $CONTAINERS_PATH:$MNT \ + --entrypoint=sh \ + $EXEC_IMAGE -c "echo '$CONTENTS' > $MNT_HOSTS && rm -f $MNT_HOSTS && echo '$CONTENTS' > $MNT_HOSTS" +} + # 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() { @@ -1613,11 +1683,20 @@ case "$COMMAND" in run) dns_args "$@" shift $(dns_arg_count "$@") + REWRITE_HOSTS=1 + if [ "$1" = "--no-rewrite-hosts" ] ; then + REWRITE_HOSTS= + shift + fi collect_cidr_args "$@" shift $CIDR_ARG_COUNT CONTAINER=$(docker run -e WEAVE_CIDR=none $DNS_ARGS -d "$@") create_bridge ipam_cidrs_or_die allocate $CONTAINER $CIDR_ARGS + if [ -n "$REWRITE_HOSTS" ] ; then + extra_hosts_args "$@" + rewrite_etc_hosts $DNS_EXTRA_HOSTS + fi with_container_netns_or_die $CONTAINER attach $ALL_CIDRS when_weave_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS echo $CONTAINER @@ -1643,6 +1722,8 @@ case "$COMMAND" in echo $RES ;; attach) + DNS_EXTRA_HOSTS= + REWRITE_HOSTS= collect_cidr_args "$@" shift $CIDR_ARG_COUNT while [ $# -gt 0 ]; do @@ -1653,6 +1734,13 @@ case "$COMMAND" in --rewrite-hosts) REWRITE_HOSTS=1 ;; + --add-host) + DNS_EXTRA_HOSTS="$2 $DNS_EXTRA_HOSTS" + shift + ;; + --add-host=*) + DNS_EXTRA_HOSTS="${1#*=} $DNS_EXTRA_HOSTS" + ;; *) break ;; @@ -1663,10 +1751,7 @@ case "$COMMAND" in CONTAINER=$(container_id $1) create_bridge ipam_cidrs$ATTACH_TYPE allocate $CONTAINER $CIDR_ARGS - if [ -n "$REWRITE_HOSTS" ] && command_exists weavehosts ; then - PATH_AND_NAME=$(docker inspect -f '{{.HostsPath}} {{.Config.Hostname}}' $CONTAINER) - weavehosts $PATH_AND_NAME $ALL_CIDRS - fi + [ -n "$REWRITE_HOSTS" ] && rewrite_etc_hosts $DNS_EXTRA_HOSTS with_container_netns$ATTACH_TYPE $CONTAINER attach $ALL_CIDRS >/dev/null when_weave_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS show_addrs $ALL_CIDRS From 6a674d0d678624abb852ab2f201a17fa8530772d Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Mon, 5 Oct 2015 13:28:36 +0200 Subject: [PATCH 2/5] Add hosts when attaching --- proxy/proxy.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/proxy/proxy.go b/proxy/proxy.go index 2f2f4e3d..e917aade 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -356,6 +356,12 @@ func (proxy *Proxy) attach(container *docker.Container) error { args = append(args, cidrs...) if !proxy.NoRewriteHosts { args = append(args, "--rewrite-hosts") + + if container.HostConfig != nil { + for _, eh := range container.HostConfig.ExtraHosts { + args = append(args, fmt.Sprintf("--add-host=%s", eh)) + } + } } args = append(args, "--or-die", container.ID) if _, stderr, err := callWeave(args...); err != nil { From 91f27ddd66a165ba9737590b9ad634823efeed78 Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Fri, 2 Oct 2015 13:12:13 +0200 Subject: [PATCH 3/5] Removed weavehosts --- Makefile | 9 +-- prog/weaveexec/Dockerfile | 2 +- prog/weavehosts/weavehosts.go | 120 ---------------------------------- 3 files changed, 4 insertions(+), 127 deletions(-) delete mode 100644 prog/weavehosts/weavehosts.go diff --git a/Makefile b/Makefile index f36f39ae..e1ff2156 100644 --- a/Makefile +++ b/Makefile @@ -13,12 +13,11 @@ WEAVER_EXE=prog/weaver/weaver WEAVEPROXY_EXE=prog/weaveproxy/weaveproxy SIGPROXY_EXE=prog/sigproxy/sigproxy WEAVEWAIT_EXE=prog/weavewait/weavewait -WEAVEHOSTS_EXE=prog/weavehosts/weavehosts NETCHECK_EXE=prog/netcheck/netcheck DOCKERTLSARGS_EXE=prog/docker_tls_args/docker_tls_args RUNNER_EXE=testing/runner/runner -EXES=$(WEAVER_EXE) $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEHOSTS_EXE) $(NETCHECK_EXE) $(DOCKERTLSARGS_EXE) $(RUNNER_EXE) +EXES=$(WEAVER_EXE) $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(NETCHECK_EXE) $(DOCKERTLSARGS_EXE) $(RUNNER_EXE) WEAVER_UPTODATE=.weaver.uptodate WEAVEEXEC_UPTODATE=.weaveexec.uptodate @@ -78,11 +77,10 @@ $(NETCHECK_EXE): prog/netcheck/netcheck.go # the main build stanza due to not importing net package $(SIGPROXY_EXE): prog/sigproxy/main.go $(WEAVEWAIT_EXE): prog/weavewait/main.go net/*.go -$(WEAVEHOSTS_EXE): prog/weavehosts/weavehosts.go $(RUNNER_EXE): testing/runner/runner.go $(DOCKERTLSARGS_EXE): prog/docker_tls_args/*.go -$(WEAVEWAIT_EXE) $(SIGPROXY_EXE) $(WEAVEHOSTS_EXE) $(RUNNER_EXE) $(DOCKERTLSARGS_EXE): +$(WEAVEWAIT_EXE) $(SIGPROXY_EXE) $(RUNNER_EXE) $(DOCKERTLSARGS_EXE): go get -tags netgo ./$(@D) go build $(BUILD_FLAGS) -o $@ ./$(@D) @@ -90,12 +88,11 @@ $(WEAVER_UPTODATE): prog/weaver/Dockerfile $(WEAVER_EXE) $(SUDO) docker build -t $(WEAVER_IMAGE) prog/weaver touch $@ -$(WEAVEEXEC_UPTODATE): prog/weaveexec/Dockerfile $(DOCKER_DISTRIB) weave $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEHOSTS_EXE) $(NETCHECK_EXE) $(DOCKERTLSARGS_EXE) +$(WEAVEEXEC_UPTODATE): prog/weaveexec/Dockerfile $(DOCKER_DISTRIB) weave $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(NETCHECK_EXE) $(DOCKERTLSARGS_EXE) cp weave prog/weaveexec/weave cp $(SIGPROXY_EXE) prog/weaveexec/sigproxy cp $(WEAVEPROXY_EXE) prog/weaveexec/weaveproxy cp $(WEAVEWAIT_EXE) prog/weaveexec/weavewait - cp $(WEAVEHOSTS_EXE) prog/weaveexec/weavehosts cp $(NETCHECK_EXE) prog/weaveexec/netcheck cp $(DOCKERTLSARGS_EXE) prog/weaveexec/docker_tls_args cp $(DOCKER_DISTRIB) prog/weaveexec/docker.tgz diff --git a/prog/weaveexec/Dockerfile b/prog/weaveexec/Dockerfile index bb189584..3cd1972d 100644 --- a/prog/weaveexec/Dockerfile +++ b/prog/weaveexec/Dockerfile @@ -18,6 +18,6 @@ RUN apk add --update \ && rm -rf /var/cache/apk/* ADD ./weave ./sigproxy ./weaveproxy /home/weave/ -ADD ./netcheck ./weavehosts ./docker_tls_args /usr/bin/ +ADD ./netcheck ./docker_tls_args /usr/bin/ ADD ./weavewait /w/w ADD ./docker.tgz / diff --git a/prog/weavehosts/weavehosts.go b/prog/weavehosts/weavehosts.go deleted file mode 100644 index b1694365..00000000 --- a/prog/weavehosts/weavehosts.go +++ /dev/null @@ -1,120 +0,0 @@ -/* weavehosts: rewrite /etc/hosts as necessary to put the weave address(es) at the top */ -package main - -import ( - "bufio" - "bytes" - "fmt" - "io/ioutil" - "net" - "os" - "sort" - "strings" -) - -func main() { - args := os.Args - if len(args) < 4 { - fmt.Fprintln(os.Stderr, "usage: weavehosts hostpath hostname cidr [cidr ...]") - os.Exit(1) - } - - checkErr(updateHosts(args[1], args[2], args[3:])) - - os.Exit(0) -} - -func checkErr(err error) { - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } -} - -func updateHosts(path, hostname string, cidrs []string) error { - hosts, err := parseHosts(path) - if err != nil { - return err - } - - // Remove existing ips pointing to our hostname - toRemove := []string{} - for ip, addrs := range hosts { - for _, addr := range addrs { - if addr == hostname { - toRemove = append(toRemove, ip) - break - } - } - } - for _, ip := range toRemove { - delete(hosts, ip) - } - - // Add the weave ip(s) - for _, cidr := range cidrs { - ip, _, err := net.ParseCIDR(cidr) - checkErr(err) - ipStr := ip.String() - hosts[ipStr] = append(hosts[ipStr], hostname) - } - - return writeHosts(path, hosts) -} - -func parseHosts(path string) (map[string][]string, error) { - f, err := os.Open(path) - if err != nil { - return nil, err - } - defer f.Close() - ips := map[string][]string{} - scanner := bufio.NewScanner(f) - for scanner.Scan() { - line := scanner.Text() - - // Remove any comments - if i := strings.IndexByte(line, '#'); i != -1 { - line = line[:i] - } - - fields := strings.Fields(line) - if len(fields) > 0 { - ips[fields[0]] = append(ips[fields[0]], fields[1:]...) - } - } - if scanner.Err() != nil { - return nil, scanner.Err() - } - return ips, nil -} - -func writeHosts(path string, contents map[string][]string) error { - ips := []string{} - for ip := range contents { - ips = append(ips, ip) - } - sort.Strings(ips) - - buf := &bytes.Buffer{} - fmt.Fprintln(buf, "# modified by weave") - for _, ip := range ips { - if addrs := contents[ip]; len(addrs) > 0 { - fmt.Fprintf(buf, "%s\t%s\n", ip, strings.Join(uniqueStrs(addrs), " ")) - } - } - return ioutil.WriteFile(path, buf.Bytes(), 644) -} - -func uniqueStrs(s []string) []string { - m := map[string]struct{}{} - result := []string{} - for _, str := range s { - if _, ok := m[str]; !ok { - m[str] = struct{}{} - result = append(result, str) - } - } - sort.Strings(result) - return result -} From 5d2892f6fc9d106a767448e0c442c83a74807eb3 Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Mon, 21 Sep 2015 12:50:34 +0200 Subject: [PATCH 4/5] Test we respect --add-host when rewriting /etc/hosts Simplify some tests --- test/200_dns_test.sh | 16 ++++++++++++-- test/280_with_or_without_dns_test.sh | 23 ++++++++++--------- test/630_proxy_dns_test.sh | 33 +++++++++++----------------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/test/200_dns_test.sh b/test/200_dns_test.sh index 78a845a5..7da0de0d 100755 --- a/test/200_dns_test.sh +++ b/test/200_dns_test.sh @@ -6,13 +6,25 @@ C1=10.2.0.78 C2=10.2.0.34 NAME=seetwo.weave.local +STATIC1=10.9.9.9 +STATIC1_NAME=static1.name +STATIC2=10.10.10.10 +STATIC2_NAME=static2.name +STATICS_ARGS="--add-host=$STATIC1_NAME:$STATIC1 --add-host $STATIC2_NAME:$STATIC2" + start_suite "Resolve names on a single host" weave_on $HOST1 launch -start_container $HOST1 $C2/24 --name=c2 -h $NAME -start_container_with_dns $HOST1 $C1/24 --name=c1 +start_container $HOST1 $C2/24 --name=c2 $STATICS_ARGS -h $NAME +start_container_with_dns $HOST1 $C1/24 --name=c1 $STATICS_ARGS assert_dns_record $HOST1 c1 $NAME $C2 +# check we respect the --add-host arguments +for C in c1 c2 ; do + assert_dns_a_record $HOST1 $C $STATIC1_NAME $STATIC1 + assert_dns_a_record $HOST1 $C $STATIC2_NAME $STATIC2 +done + end_suite diff --git a/test/280_with_or_without_dns_test.sh b/test/280_with_or_without_dns_test.sh index f38e23e9..21051f08 100755 --- a/test/280_with_or_without_dns_test.sh +++ b/test/280_with_or_without_dns_test.sh @@ -5,16 +5,16 @@ IP=10.2.0.34 TARGET=seetwo.weave.local TARGET_IP=10.2.0.78 +STATIC=static.name +STATIC_IP=10.9.9.9 -assert_no_resolution() { - container=$(weave_on $HOST1 run "$@" $IP/24 -t $DNS_IMAGE /bin/sh) - assert_no_dns_record $HOST1 $container $TARGET - rm_containers $HOST1 $container -} +check_dns() { + chk=$1 + shift -assert_resolution() { - container=$(weave_on $HOST1 run "$@" $IP/24 -t $DNS_IMAGE /bin/sh) - assert_dns_record $HOST1 $container $TARGET $TARGET_IP + container=$(weave_on $HOST1 run $@ $IP/24 -t --add-host=$STATIC:$STATIC_IP $DNS_IMAGE /bin/sh) + $chk $HOST1 $container $TARGET + assert_dns_record $HOST1 $container $STATIC $STATIC_IP rm_containers $HOST1 $container } @@ -24,10 +24,9 @@ start_suite "With or without DNS test" weave_on $HOST1 launch-router start_container $HOST1 $TARGET_IP/24 --name c2 -h $TARGET -DNS_IP=$(weave_on $HOST1 docker-bridge-ip) -assert_no_resolution --without-dns -assert_resolution -assert_resolution --with-dns +check_dns assert_no_dns_record --without-dns +check_dns assert_dns_record +check_dns assert_dns_record --with-dns end_suite diff --git a/test/630_proxy_dns_test.sh b/test/630_proxy_dns_test.sh index a0679e59..63b9f694 100755 --- a/test/630_proxy_dns_test.sh +++ b/test/630_proxy_dns_test.sh @@ -6,46 +6,39 @@ C1=10.2.0.78 C2=10.2.0.34 C1_NAME=c1.weave.local C2_NAME=seetwo.weave.local +STATIC=static.name +STATIC_IP=10.9.9.9 -boot_containers() { - proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C2/24 -dt --name=c2 -h $C2_NAME - proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C1/24 -dt --name=c1 -} - -kill_containers() { +do_assert_resolution() { + proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C2/24 -dt --name=c2 --add-host $STATIC:$STATIC_IP -h $C2_NAME + proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C1/24 -dt --name=c1 --add-host $STATIC:$STATIC_IP + $1 $HOST1 c1 $C2_NAME $C2 + assert_dns_record $HOST1 c1 $STATIC $STATIC_IP + $1 $HOST1 c2 $C1_NAME $C1 + assert_dns_record $HOST1 c2 $STATIC $STATIC_IP rm_containers $HOST1 c1 c2 } -assert_no_resolution() { - boot_containers - assert_no_dns_record $HOST1 c1 $C2_NAME $C2 - assert_no_dns_record $HOST1 c2 $C1_NAME $C1 - kill_containers -} - start_suite "Proxy registers containers with dns" bridge_ip=$(weave_on $HOST1 docker-bridge-ip) # Assert behaviour without weaveDNS running, but dns forced weave_on $HOST1 launch-proxy --with-dns -assert_no_resolution +do_assert_resolution assert_no_dns_record weave_on $HOST1 stop-proxy # Assert behaviour without weaveDNS weave_on $HOST1 launch-proxy -assert_no_resolution +do_assert_resolution assert_no_dns_record # Assert behaviour with weaveDNS running weave_on $HOST1 launch-router -boot_containers -assert_dns_record $HOST1 c1 $C2_NAME $C2 -assert_dns_record $HOST1 c2 $C1_NAME $C1 -kill_containers +do_assert_resolution assert_dns_record weave_on $HOST1 stop-proxy # Assert behaviour with weaveDNS running, but dns forced off weave_on $HOST1 launch-proxy --without-dns -assert_no_resolution +do_assert_resolution assert_no_dns_record end_suite From 4350b6402f8a4f28f200050a134afb8a01d47347 Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Wed, 7 Oct 2015 14:18:44 +0200 Subject: [PATCH 5/5] Updated docs --- site/proxy.md | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/site/proxy.md b/site/proxy.md index 2c033cdc..ca5d14c8 100644 --- a/site/proxy.md +++ b/site/proxy.md @@ -146,10 +146,27 @@ container with a blank `WEAVE_CIDR`, e.g. host1$ docker run -ti -e WEAVE_CIDR="" ubuntu -When launching weave-enabled containers, the proxy will automatically -rewrite `/etc/hosts` to replace the docker IP with the container's -weave IP. If you need the docker IP to remain in `/etc/hosts`, the -proxy must be launched with the `--no-rewrite-hosts` flag. +## Name resolution via `/etc/hosts` + +When starting weave-enabled containers, the proxy will automatically +replace the container's `/etc/hosts` file, and disable Docker's control +over it. The new file contains an entry for the container's hostname +and weave IP address, as well as additional entries that have been +specified with `--add-host` parameters. This ensures that + +- name resolution of the container's hostname, e.g. via `hostname -i`, +returns the weave IP address. This is required for many cluster-aware +applications to work. +- unqualified names get resolved via DNS, i.e. typically via weaveDNS +to weave IP addresses. This is required so that in a typical setup +one can simply "ping ", i.e. without having to +specify a `.weave.local` suffix. + +In case you prefer to keep `/etc/hosts` under Docker's control (for +example, because you need the hostname to resolve to the Docker-assigned +IP instead of the weave IP, or you require name resolution for +Docker-managed networks), the proxy must be launched with the +`--no-rewrite-hosts` flag. host1$ weave launch-router && weave launch-proxy --no-rewrite-hosts @@ -271,6 +288,13 @@ specify addresses manually instead: $ weave run 10.2.1.1/24 -ti ubuntu +`weave run` will rewrite `/etc/hosts` in the same way +[the proxy does](#etchosts). In case you prefer to keep +the original file, you must specify `--no-rewrite-hosts` when running +the container: + + $ weave run --no-rewrite-hosts 10.2.1.1/24 -ti ubuntu + There are some limitations to starting containers with `weave run`: * containers are always started in the background, i.e. the equivalent