[proxy] More flexible container name -> hostname derivation

Provides to new two new flags: `--hostname-match <regexp>`
and `--hostname-replacement <replacement>`. When launching a container, its name
matched against regular expression `<regexp>`. Then, based on that match,
`<replacement>` will be used to generate a hostname, which will ultimately be
handed over to weaveDNS for registration.

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-match '^aws-[0-9]+-(.*)$'` and
`--hostname-replacement 'my-app-$1'`, running a container named
`aws-12798186823-foo` through WeaveProxy will lead to WeaveDNS registering
`my-app-foo`.

Closes #1018
This commit is contained in:
Alfonso Acosta committed 2015-07-14 14:41:44 +00:00
1 parent bbacaa42b8
commit 4f82dc97fd
6 files changed
+92 -15

No files matched your search

+3 -1
View File
@@ -58,7 +58,9 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error {
if err := i.setWeaveWaitEntrypoint(container.Config); err != nil {
return err
}
if err := i.setWeaveDNS(&container, r.URL.Query().Get("name")); err != nil {
containerName := r.URL.Query().Get("name")
hostname := i.proxy.hostnameMatchRegexp.ReplaceAllString(containerName, i.proxy.HostnameReplacement)
if err := i.setWeaveDNS(&container, hostname); err != nil {
return err
}
}
+25 -14
View File
@@ -15,11 +15,13 @@ import (
)
const (
defaultCaFile = "ca.pem"
defaultKeyFile = "key.pem"
defaultCertFile = "cert.pem"
dockerSock = "/var/run/docker.sock"
dockerSockUnix = "unix://" + dockerSock
defaultCaFile = "ca.pem"
defaultKeyFile = "key.pem"
defaultCertFile = "cert.pem"
dockerSock = "/var/run/docker.sock"
dockerSockUnix = "unix://" + dockerSock
substitutionPatternName = "pattern"
substitutionReplacementName = "replacement"
)
var (
@@ -29,19 +31,22 @@ var (
)
type Config struct {
ListenAddrs []string
NoDefaultIPAM bool
NoRewriteHosts bool
TLSConfig TLSConfig
Version string
WithDNS bool
WithoutDNS bool
HostnameMatch string
HostnameReplacement string
ListenAddrs []string
NoDefaultIPAM bool
NoRewriteHosts bool
TLSConfig TLSConfig
Version string
WithDNS bool
WithoutDNS bool
}
type Proxy struct {
Config
client *docker.Client
dockerBridgeIP string
client *docker.Client
dockerBridgeIP string
hostnameMatchRegexp *regexp.Regexp
}
func NewProxy(c Config) (*Proxy, error) {
@@ -65,6 +70,12 @@ func NewProxy(c Config) (*Proxy, error) {
p.dockerBridgeIP = string(dockerBridgeIP)
}
p.hostnameMatchRegexp, err = regexp.Compile(c.HostnameMatch)
if err != nil {
err := fmt.Errorf("Incorrect hostname match '%s': %s", c.HostnameMatch, err.Error())
return nil, err
}
return p, nil
}