-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[proxy] More flexible container name -> hostname derivation
Provides a new "--hostname=substitution" flag to WeaveProxy, where 'substitution' is a sed-style substitution command. 'substitution' will be applied to container names at launch-time, as a means to control what hostnames will be registered in WeaveDNS. The substitution is implemented with https://golang.org/pkg/regexp/#Regexp.ReplaceAllString . Thus, references to regexp substitution groups should be prepended with a dollar sign. For instance, if we provide "--hostname='/aws-[0-9]+-(.*)/my-app-$1/'", running a container named 'aws-12798186823-foo' through WeaveProxy will lead to WeaveDNS registering 'my-app-foo'. Closes #1018
- Loading branch information
Alfonso Acosta
committed
Jul 14, 2015
1 parent
bbacaa4
commit d91beb9
Showing
9 changed files
with
212 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package proxy | ||
|
||
import ( | ||
"reflect" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestFindStringNamedSubmatch(t *testing.T) { | ||
re := regexp.MustCompile(`^(prefix) (?P<fooGroup>foo)(?P<barGroup>bar) (suffix)$`) | ||
expectedResult := map[string]string{ | ||
"fooGroup": "foo", | ||
"barGroup": "bar", | ||
} | ||
namedSubmatches := findStringNamedSubmatch(re, "prefix foobar suffix") | ||
if !reflect.DeepEqual(namedSubmatches, expectedResult) { | ||
t.Error("Unexpected result", namedSubmatches) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package proxy | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseHostname(t *testing.T) { | ||
hostnameExpr := `/aws-[0-9]+-(.*)/my-app-$1/` | ||
|
||
patternRegexp, replacementStr, err := parseHostname(hostnameExpr) | ||
if err != nil { | ||
t.Error("Unexpected error", err) | ||
} | ||
if replacementStr != "my-app-$1" { | ||
t.Error("Unexpected replacement string", replacementStr) | ||
} | ||
|
||
hostname := patternRegexp.ReplaceAllString("aws-17651276812-name", replacementStr) | ||
if hostname != "my-app-name" { | ||
t.Error("Unexpected replacement hostname", hostname) | ||
} | ||
} | ||
|
||
func TestParseWrongHostname(t *testing.T) { | ||
wrongHostnameExprs := []string{ | ||
`/`, | ||
`//`, | ||
`/ /`, | ||
`/ / / /`, | ||
`/aws-[0-9]+-(.*)/my-app-$1/ /`, | ||
`/ /aws-[0-9]+-(.*)/my-app-$1/`, | ||
`/ /aws-[0-9]+-(.*)/my-app-$1/ /`, | ||
} | ||
|
||
for _, hostnameExpr := range wrongHostnameExprs { | ||
_, _, err := parseHostname(hostnameExpr) | ||
if err == nil { | ||
t.Error("Should not succesully parse wrong hostname expression", hostnameExpr) | ||
} | ||
} | ||
} | ||
|
||
func TestEscapedHostname(t *testing.T) { | ||
hostnameExpr := `/aws\/[0-9]+\/(.*)/my\/app\/$1/` | ||
|
||
patternRegexp, replacementStr, err := parseHostname(hostnameExpr) | ||
if err != nil { | ||
t.Error("Unexpected error", err) | ||
} | ||
if replacementStr != "my/app/$1" { | ||
t.Error("Unexpected replacement string", replacementStr) | ||
} | ||
|
||
hostname := patternRegexp.ReplaceAllString("aws/17651276812/name", replacementStr) | ||
if hostname != "my/app/name" { | ||
t.Error("Unexpected replacement hostname", hostname) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#! /bin/bash | ||
|
||
. ./config.sh | ||
|
||
C1=10.2.0.78 | ||
C2=10.2.0.34 | ||
C3=10.2.0.57 | ||
CNAME1=qiuds71y827hdi-seeone-1io9qd9i0wd | ||
NAME1=seeone.weave.local | ||
CNAME2=124DJKSNK812-seetwo-128hbaJ881 | ||
NAME2=seetwo.weave.local | ||
CNAME3=doesnotmatchpattern | ||
NAME3=doesnotmatchpattern.weave.local | ||
|
||
start_container() { | ||
proxy docker_on $HOST1 run "$@" -dt $DNS_IMAGE /bin/sh | ||
} | ||
|
||
start_suite "Hostname derivation through container name substitutions" | ||
|
||
weave_on $HOST1 launch-dns 10.2.254.1/24 | ||
weave_on $HOST1 launch-proxy --hostname '/[^-]+-(?P<appname>[^-]*)-[^-]+/$appname/' | ||
|
||
start_container -e WEAVE_CIDR=$C1/24 --name=$CNAME1 | ||
start_container -e WEAVE_CIDR=$C2/24 --name=$CNAME2 | ||
start_container -e WEAVE_CIDR=$C3/24 --name=$CNAME3 | ||
|
||
check() { | ||
assert "proxy exec_on $HOST1 $1 getent hosts $2 | tr -s ' '" "$3 $2" | ||
} | ||
|
||
check $CNAME1 $NAME2 $C2 | ||
check $CNAME2 $NAME3 $C3 | ||
check $CNAME3 $NAME1 $C1 | ||
|
||
end_suite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters