Merge pull request #1384 from weaveworks/dns-hostname-from-labels

[proxy] Add --hostname-from-label

Closes #1381.
This commit is contained in:
Matthias Radestock committed 2015-09-04 17:14:44 +01:00
commit 99b482b363
6 files changed
+92 -29

No files matched your search

+1
View File
@@ -27,6 +27,7 @@ func main() {
mflag.BoolVar(&justVersion, []string{"#version", "-version"}, false, "print version and exit") mflag.BoolVar(&justVersion, []string{"#version", "-version"}, false, "print version and exit")
mflag.StringVar(&logLevel, []string{"-log-level"}, "info", "logging level (debug, info, warning, error)") mflag.StringVar(&logLevel, []string{"-log-level"}, "info", "logging level (debug, info, warning, error)")
mflagext.ListVar(&c.ListenAddrs, []string{"H"}, nil, "addresses on which to listen") mflagext.ListVar(&c.ListenAddrs, []string{"H"}, nil, "addresses on which to listen")
mflag.StringVar(&c.HostnameFromLabel, []string{"-hostname-from-label"}, "", "Key of container label from which to obtain the container's hostname")
mflag.StringVar(&c.HostnameMatch, []string{"-hostname-match"}, "(.*)", "Regexp pattern to apply on container names (e.g. '^aws-[0-9]+-(.*)$')") mflag.StringVar(&c.HostnameMatch, []string{"-hostname-match"}, "(.*)", "Regexp pattern to apply on container names (e.g. '^aws-[0-9]+-(.*)$')")
mflag.StringVar(&c.HostnameReplacement, []string{"-hostname-replacement"}, "$1", "Expression to generate hostnames based on matches from --hostname-match (e.g. 'my-app-$1')") mflag.StringVar(&c.HostnameReplacement, []string{"-hostname-replacement"}, "$1", "Expression to generate hostnames based on matches from --hostname-match (e.g. 'my-app-$1')")
mflag.BoolVar(&c.RewriteInspect, []string{"-rewrite-inspect"}, false, "Rewrite 'inspect' calls to return the weave network settings (if attached)") mflag.BoolVar(&c.RewriteInspect, []string{"-rewrite-inspect"}, false, "Rewrite 'inspect' calls to return the weave network settings (if attached)")
+7 -2
View File
@@ -59,8 +59,13 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error {
if err := i.setWeaveWaitEntrypoint(container.Config); err != nil { if err := i.setWeaveWaitEntrypoint(container.Config); err != nil {
return err return err
} }
containerName := r.URL.Query().Get("name") hostname := r.URL.Query().Get("name")
hostname := i.proxy.hostnameMatchRegexp.ReplaceAllString(containerName, i.proxy.HostnameReplacement) if i.proxy.Config.HostnameFromLabel != "" {
if labelValue, ok := container.Labels[i.proxy.Config.HostnameFromLabel]; ok {
hostname = labelValue
}
}
hostname = i.proxy.hostnameMatchRegexp.ReplaceAllString(hostname, i.proxy.HostnameReplacement)
if err := i.setWeaveDNS(&container, hostname); err != nil { if err := i.setWeaveDNS(&container, hostname); err != nil {
return err return err
} }
+1
View File
@@ -39,6 +39,7 @@ func dockerAPIEndpoint(endpoint string) *regexp.Regexp {
} }
type Config struct { type Config struct {
HostnameFromLabel string
HostnameMatch string HostnameMatch string
HostnameReplacement string HostnameReplacement string
ListenAddrs []string ListenAddrs []string
+27 -13
View File
@@ -164,29 +164,43 @@ for registration. However, there are situations in which the final container
name is out of the user's control (e.g. when using Docker orchestrators which name is out of the user's control (e.g. when using Docker orchestrators which
append control/namespacing identifiers to the original container names). append control/namespacing identifiers to the original container names).
For those situations, the proxy provides two flags: `--hostname-match <regexp>` For those situations, the proxy provides a few flags: `--hostname-from-label
and `--hostname-replacement <replacement>`. When launching a container, its name <labelkey>`, `--hostname-match <regexp>` and `--hostname-replacement
is matched against regular expression `<regexp>`. Then, based on that match, <replacement>`. When launching a container, the hostname is initialized to the
`<replacement>` will be used to generate a hostname, which will ultimately be value of the container label with key `<labelkey>`, if `<labelkey>` wasn't
handed over to weaveDNS for registration. provided, the container name is used. Additionally, the hostname is matched
against regular expression `<regexp>`. Then, based on that match,
`<replacement>` will be used to obtainer the final hostname, which will
ultimately be handed over to weaveDNS for registration.
For instance, if we launch the proxy using For instance, we can launch the proxy using all three flags
`--hostname-match '^aws-[0-9]+-(.*)$'` and `--hostname-replacement 'my-app-$1'`
host1$ weave launch-router && weave launch-proxy --hostname-match '^aws-[0-9]+-(.*)$' --hostname-replacement 'my-app-$1' host1$ weave launch-router && weave launch-proxy --hostname-from-label hostname-label --hostname-match '^aws-[0-9]+-(.*)$' --hostname-replacement 'my-app-$1'
host1$ eval "$(weave env)" host1$ eval "$(weave env)"
then, running a container named `aws-12798186823-foo` will lead to weaveDNS registering Note how regexp substitution groups should be prepended with a dollar sign
hostname `my-app-foo` and not `aws-12798186823-foo`. (e.g. `$1`). For further details on the regular expression syntax please see
[Google's re2 documentation](https://github.com/google/re2/wiki/Syntax).
Then, running a container named `aws-12798186823-foo` without labels will lead
to weaveDNS registering hostname `my-app-foo` and not `aws-12798186823-foo`.
host1$ docker run -ti --name=aws-12798186823-foo ubuntu ping my-app-foo host1$ docker run -ti --name=aws-12798186823-foo ubuntu ping my-app-foo
PING my-app-foo.weave.local (10.32.0.2) 56(84) bytes of data. PING my-app-foo.weave.local (10.32.0.2) 56(84) bytes of data.
64 bytes from my-app-foo.weave.local (10.32.0.2): icmp_seq=1 ttl=64 time=0.027 ms 64 bytes from my-app-foo.weave.local (10.32.0.2): icmp_seq=1 ttl=64 time=0.027 ms
64 bytes from my-app-foo.weave.local (10.32.0.2): icmp_seq=2 ttl=64 time=0.067 ms 64 bytes from my-app-foo.weave.local (10.32.0.2): icmp_seq=2 ttl=64 time=0.067 ms
Note how regexp substitution groups should be prepended with a dollar sign Also, running a container named `foo` with label
(e.g. `$1`). For further details on the regular expression syntax please see `hostname-label=aws-12798186823-foo` leads to the same hostname registration.
[Google's re2 documentation](https://github.com/google/re2/wiki/Syntax).
host1$ docker run -ti --name=foo --label=hostname-label=aws-12798186823-foo ubuntu ping my-app-foo
PING my-app-foo.weave.local (10.32.0.2) 56(84) bytes of data.
64 bytes from my-app-foo.weave.local (10.32.0.2): icmp_seq=1 ttl=64 time=0.031 ms
64 bytes from my-app-foo.weave.local (10.32.0.2): icmp_seq=2 ttl=64 time=0.042 ms
This is because, as we explained above, when providing `--hostname-from-label`
to the proxy, the specified label has precedence over the container's name.
## <a name="tls"></a>Securing the docker communication with TLS ## <a name="tls"></a>Securing the docker communication with TLS
+55 -14
View File
@@ -5,24 +5,65 @@
C1=10.2.0.78 C1=10.2.0.78
C2=10.2.0.34 C2=10.2.0.34
C3=10.2.0.57 C3=10.2.0.57
CNAME1=qiuds71y827hdi-seeone-1io9qd9i0wd ENAME1=qiuds71y827hdi-seeone-1io9qd9i0wd
NAME1=seeone.weave.local HNAME1=seeone
CNAME2=124DJKSNK812-seetwo-128hbaJ881 FQDN1=seeone.weave.local
NAME2=seetwo.weave.local ENAME2=124DJKSNK812-seetwo-128hbaJ881
CNAME3=doesnotmatchpattern HNAME2=seetwo
NAME3=doesnotmatchpattern.weave.local FQDN2=$HNAME2.weave.local
ENAME3=doesnotmatchpattern
HNAME3=doesnotmatchpattern
FQDN3=$HNAME3.weave.local
start_suite "Hostname derivation through container name substitutions" EXPANDED_NAMES="$ENAME1 $ENAME2 $ENAME3"
HOSTNAMES="$HNAME1 $HNAME2 $HNAME3"
check_dns_records() {
ARG_PREFIX=$1
shift
CID1=$(proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C1/24 ${ARG_PREFIX}$1)
CID2=$(proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C2/24 ${ARG_PREFIX}$2)
CID3=$(proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C3/24 ${ARG_PREFIX}$3)
assert_dns_a_record $HOST1 $CID1 $FQDN1 $C1
assert_dns_a_record $HOST1 $CID2 $FQDN3 $C3
assert_dns_a_record $HOST1 $CID2 $FQDN2 $C2
rm_containers $HOST1 $CID1 $CID2 $CID3
}
test_setup() {
weave_on $HOST1 launch-proxy $@
}
test_cleanup() {
weave_on $HOST1 stop-proxy
}
start_suite "Hostname derivation"
weave_on $HOST1 launch-router weave_on $HOST1 launch-router
weave_on $HOST1 launch-proxy --hostname-match '^[^-]+-(?P<appname>[^-]*)-[^-]+$' --hostname-replacement '$appname'
proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C1/24 --name=$CNAME1 # Hostname derivation through container name substitutions
proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C2/24 --name=$CNAME2 test_setup --hostname-match '^[^-]+-(?P<appname>[^-]*)-[^-]+$' --hostname-replacement '$appname'
proxy_start_container_with_dns $HOST1 -e WEAVE_CIDR=$C3/24 --name=$CNAME3 # check that the normal container_name->hostname derivation doesn't break
check_dns_records --name= $HOSTNAMES
check_dns_records --name= $EXPANDED_NAMES
test_cleanup
assert_dns_a_record $HOST1 $CNAME1 $NAME2 $C2 # Hostname derivation from container labels
assert_dns_a_record $HOST1 $CNAME2 $NAME3 $C3 test_setup --hostname-from-label hostname-label
assert_dns_a_record $HOST1 $CNAME3 $NAME1 $C1 check_dns_records --name= $HOSTNAMES
check_dns_records --label=hostname-label= $HOSTNAMES
test_cleanup
# Hostname derivation combining container labels and substitutions
test_setup --hostname-from-label hostname-label --hostname-match '^[^-]+-(?P<appname>[^-]*)-[^-]+$' --hostname-replacement '$appname'
check_dns_records --name= $HOSTNAMES
check_dns_records --name= $EXPANDED_NAMES
check_dns_records --label=hostname-label= $HOSTNAMES
check_dns_records --label=hostname-label= $EXPANDED_NAMES
test_cleanup
end_suite end_suite
+1
View File
@@ -54,6 +54,7 @@ weave launch-router [--password <password>] [--nickname <nickname>]
[--no-discovery] [--init-peer-count <count>] <peer> ... [--no-discovery] [--init-peer-count <count>] <peer> ...
weave launch-proxy [-H <endpoint>] [--with-dns | --without-dns] weave launch-proxy [-H <endpoint>] [--with-dns | --without-dns]
[--no-default-ipalloc] [--no-rewrite-hosts] [--no-default-ipalloc] [--no-rewrite-hosts]
[--hostname-from-label <labelkey>]
[--hostname-match <regexp>] [--hostname-match <regexp>]
[--hostname-replacement <replacement>] [--hostname-replacement <replacement>]
[--rewrite-inspect] [--rewrite-inspect]