From e21a204eedacaefc6a0849cf5c4847808157058c Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Mon, 27 Jul 2015 17:25:42 +0100 Subject: [PATCH 1/5] [proxy] add --rewrite-inspect flag To return the weave ip/mac for docker inspect calls. --- prog/weaveproxy/main.go | 1 + proxy/common.go | 32 ++++++++++++++ proxy/inspect_container_interceptor.go | 60 ++++++++++++++++++++++++++ proxy/inspect_exec_interceptor.go | 48 +++++++++++++++++++++ proxy/proxy.go | 13 ++++-- site/proxy.md | 6 +++ test/605_proxy_inspect_test.sh | 16 +++++++ weave | 1 + 8 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 proxy/inspect_container_interceptor.go create mode 100644 proxy/inspect_exec_interceptor.go create mode 100755 test/605_proxy_inspect_test.sh diff --git a/prog/weaveproxy/main.go b/prog/weaveproxy/main.go index 23073b74..a6318321 100644 --- a/prog/weaveproxy/main.go +++ b/prog/weaveproxy/main.go @@ -29,6 +29,7 @@ func main() { mflagext.ListVar(&c.ListenAddrs, []string{"H"}, nil, "addresses on which to listen") 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.BoolVar(&c.RewriteInspect, []string{"-rewrite-inspect"}, false, "Rewrite 'inspect' calls to return the weave network settings (if attached)") mflag.BoolVar(&c.NoDefaultIPAM, []string{"#-no-default-ipam", "-no-default-ipalloc"}, false, "do not automatically allocate addresses for containers without a WEAVE_CIDR") mflag.BoolVar(&c.NoRewriteHosts, []string{"-no-rewrite-hosts"}, false, "do not automatically rewrite /etc/hosts. Use if you need the docker IP to remain in /etc/hosts") mflag.StringVar(&c.TLSConfig.CACert, []string{"#tlscacert", "-tlscacert"}, "", "Trust certs signed only by this CA") diff --git a/proxy/common.go b/proxy/common.go index 69d337e3..9da06f82 100644 --- a/proxy/common.go +++ b/proxy/common.go @@ -3,12 +3,15 @@ package proxy import ( "bytes" "encoding/json" + "errors" "fmt" "io/ioutil" + "net" "net/http" "os" "os/exec" "regexp" + "strings" "github.com/fsouza/go-dockerclient" . "github.com/weaveworks/weave/common" @@ -62,3 +65,32 @@ func inspectContainerInPath(client *docker.Client, path string) (*docker.Contain } return container, err } + +func weaveContainerIPs(container *docker.Container) (mac string, ips []net.IP, nets []*net.IPNet, err error) { + stdout, stderr, err := callWeave("ps", container.ID) + if err != nil || len(stderr) > 0 { + err = errors.New(string(stderr)) + return + } + if len(stdout) <= 0 { + return + } + + fields := strings.Fields(string(stdout)) + if len(fields) < 2 { + return + } + mac = fields[1] + + var ip net.IP + var ipnet *net.IPNet + for _, cidr := range fields[2:] { + ip, ipnet, err = net.ParseCIDR(cidr) + if err != nil { + return + } + ips = append(ips, ip) + nets = append(nets, ipnet) + } + return +} diff --git a/proxy/inspect_container_interceptor.go b/proxy/inspect_container_interceptor.go new file mode 100644 index 00000000..791fd653 --- /dev/null +++ b/proxy/inspect_container_interceptor.go @@ -0,0 +1,60 @@ +package proxy + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + + "github.com/fsouza/go-dockerclient" + . "github.com/weaveworks/weave/common" +) + +type inspectContainerInterceptor struct{ proxy *Proxy } + +func (i *inspectContainerInterceptor) InterceptRequest(r *http.Request) error { + return nil +} + +func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error { + if !i.proxy.RewriteInspect { + return nil + } + + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + r.Body.Close() + + container := &docker.Container{} + if err := json.Unmarshal(body, container); err != nil { + return err + } + + if err := updateContainerNetworkSettings(container); err != nil { + Log.Warningf("Inspecting container %s failed: %s", container.ID, err) + } + + newBody, err := json.Marshal(container) + if err != nil { + return err + } + r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) + r.ContentLength = int64(len(newBody)) + r.TransferEncoding = nil // Stop it being chunked, because that hangs + + return nil +} + +func updateContainerNetworkSettings(container *docker.Container) error { + mac, ips, nets, err := weaveContainerIPs(container) + if err != nil || len(ips) == 0 { + return err + } + + container.NetworkSettings.MacAddress = mac + container.NetworkSettings.IPAddress = ips[0].String() + container.NetworkSettings.IPPrefixLen, _ = nets[0].Mask.Size() + return nil +} diff --git a/proxy/inspect_exec_interceptor.go b/proxy/inspect_exec_interceptor.go new file mode 100644 index 00000000..f12ecbcb --- /dev/null +++ b/proxy/inspect_exec_interceptor.go @@ -0,0 +1,48 @@ +package proxy + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + + "github.com/fsouza/go-dockerclient" + . "github.com/weaveworks/weave/common" +) + +type inspectExecInterceptor struct{ proxy *Proxy } + +func (i *inspectExecInterceptor) InterceptRequest(r *http.Request) error { + return nil +} + +func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error { + if !i.proxy.RewriteInspect { + return nil + } + + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + r.Body.Close() + + exec := &docker.ExecInspect{} + if err := json.Unmarshal(body, exec); err != nil { + return err + } + + if err := updateContainerNetworkSettings(&exec.Container); err != nil { + Log.Warningf("Inspecting exec %s failed: %s", exec.ID, err) + } + + newBody, err := json.Marshal(exec) + if err != nil { + return err + } + r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) + r.ContentLength = int64(len(newBody)) + r.TransferEncoding = nil // Stop it being chunked, because that hangs + + return nil +} diff --git a/proxy/proxy.go b/proxy/proxy.go index 7d9f6ee8..0d91fe9e 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -24,9 +24,11 @@ const ( ) var ( - containerCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/create$") - containerStartRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/(re)?start$") - execCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/exec$") + containerCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/create$") + containerStartRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/(re)?start$") + containerInspectRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/json$") + execCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/exec$") + execInspectRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/exec/[^/]*/json$") ErrWeaveCIDRNone = errors.New("the container was created with the '-e WEAVE_CIDR=none' option") ErrNoDefaultIPAM = errors.New("the container was created without specifying an IP address with '-e WEAVE_CIDR=...' and the proxy was started with the '--no-default-ipalloc' option") @@ -36,6 +38,7 @@ type Config struct { HostnameMatch string HostnameReplacement string ListenAddrs []string + RewriteInspect bool NoDefaultIPAM bool NoRewriteHosts bool TLSConfig TLSConfig @@ -123,8 +126,12 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { i = &createContainerInterceptor{proxy} case containerStartRegexp.MatchString(path): i = &startContainerInterceptor{proxy} + case containerInspectRegexp.MatchString(path): + i = &inspectContainerInterceptor{proxy} case execCreateRegexp.MatchString(path): i = &createExecInterceptor{proxy} + case execInspectRegexp.MatchString(path): + i = &inspectExecInterceptor{proxy} default: i = &nullInterceptor{} } diff --git a/site/proxy.md b/site/proxy.md index 847ba8bb..ed54bd40 100644 --- a/site/proxy.md +++ b/site/proxy.md @@ -104,6 +104,12 @@ Multiple IP addresses and networks can be supplied in the `WEAVE_CIDR` variable by space-separating them, as in `WEAVE_CIDR="10.2.1.1/24 10.2.2.1/24"`. +The docker IP will still be returned by `docker inspect`. If you want +`docker inspect` to return the weave IP instead, then the proxy must +be launced with the `--rewrite-inspect` flag. + + host1$ weave launch-router && weave launch-proxy --rewrite-inspect + ## Automatic IP address assignment If [automatic IP address assignment](ipam.html) is enabled in weave, diff --git a/test/605_proxy_inspect_test.sh b/test/605_proxy_inspect_test.sh new file mode 100755 index 00000000..15ba4fc5 --- /dev/null +++ b/test/605_proxy_inspect_test.sh @@ -0,0 +1,16 @@ +#! /bin/bash + +. ./config.sh + +start_suite "Check that docker inspect returns the weave IP" + +weave_on $HOST1 launch-router +weave_on $HOST1 launch-proxy --rewrite-inspect + +proxy docker_on $HOST1 run -dt --name c1 $SMALL_IMAGE /bin/sh +inspect_format="{{.Name}} {{.NetworkSettings.MacAddress}} {{.NetworkSettings.IPAddress}}/{{.NetworkSettings.IPPrefixLen}}" +expected="/$(weave_on $HOST1 ps c1)" + +assert "proxy docker_on $HOST1 inspect --format='$inspect_format' c1" "$expected" + +end_suite diff --git a/weave b/weave index a1c4039b..6d6af191 100755 --- a/weave +++ b/weave @@ -56,6 +56,7 @@ weave launch-proxy [-H ] [--with-dns | --without-dns] [--no-default-ipalloc] [--no-rewrite-hosts] [--hostname-match ] [--hostname-replacement ] + [--rewrite-inspect] weave env [--restore] weave config weave connect [--replace] [ ...] From 19ceb424c37cdba986cd53c844dc35a38bd9c4a5 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 2 Sep 2015 13:37:22 +0100 Subject: [PATCH 2/5] [proxy] refactoring out common marshalling logic --- proxy/common.go | 39 ++++++++++++++++++++++++++ proxy/create_container_interceptor.go | 18 ++---------- proxy/create_exec_interceptor.go | 12 +------- proxy/inspect_container_interceptor.go | 21 ++------------ proxy/inspect_exec_interceptor.go | 21 ++------------ 5 files changed, 46 insertions(+), 65 deletions(-) diff --git a/proxy/common.go b/proxy/common.go index 9da06f82..323b9611 100644 --- a/proxy/common.go +++ b/proxy/common.go @@ -40,6 +40,19 @@ func callWeave(args ...string) ([]byte, []byte, error) { return stdout.Bytes(), stderr.Bytes(), err } +func unmarshalRequestBody(r *http.Request, target interface{}) error { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + if err := r.Body.Close(); err != nil { + return err + } + r.Body = ioutil.NopCloser(bytes.NewReader(body)) + + return json.Unmarshal(body, &target) +} + func marshalRequestBody(r *http.Request, body interface{}) error { newBody, err := json.Marshal(body) if err != nil { @@ -50,6 +63,32 @@ func marshalRequestBody(r *http.Request, body interface{}) error { return nil } +func unmarshalResponseBody(r *http.Response, target interface{}) error { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + if err := r.Body.Close(); err != nil { + return err + } + r.Body = ioutil.NopCloser(bytes.NewReader(body)) + + return json.Unmarshal(body, &target) +} + +func marshalResponseBody(r *http.Response, body interface{}) error { + newBody, err := json.Marshal(body) + if err != nil { + return err + } + r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) + r.ContentLength = int64(len(newBody)) + // Stop it being chunked, because that hangs + r.TransferEncoding = nil + return nil + +} + func inspectContainerInPath(client *docker.Client, path string) (*docker.Container, error) { subs := containerIDRegexp.FindStringSubmatch(path) if subs == nil { diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index 759ec18e..4674351f 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -1,8 +1,6 @@ package proxy import ( - "bytes" - "encoding/json" "errors" "fmt" "io/ioutil" @@ -42,15 +40,8 @@ func (err *ErrNoSuchImage) Error() string { } func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return err - } - r.Body.Close() - r.Body = ioutil.NopCloser(bytes.NewReader(body)) - container := createContainerRequestBody{} - if err := json.Unmarshal(body, &container); err != nil { + if err := unmarshalRequestBody(r, &container); err != nil { return err } @@ -74,12 +65,7 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { return err } - newBody, err := json.Marshal(container) - if err != nil { - return err - } - r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) - r.ContentLength = int64(len(newBody)) + return marshalRequestBody(r, container) } return nil diff --git a/proxy/create_exec_interceptor.go b/proxy/create_exec_interceptor.go index b015a55b..1308f9cc 100644 --- a/proxy/create_exec_interceptor.go +++ b/proxy/create_exec_interceptor.go @@ -1,9 +1,6 @@ package proxy import ( - "bytes" - "encoding/json" - "io/ioutil" "net/http" "strings" @@ -14,15 +11,8 @@ import ( type createExecInterceptor struct{ proxy *Proxy } func (i *createExecInterceptor) InterceptRequest(r *http.Request) error { - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return err - } - r.Body.Close() - r.Body = ioutil.NopCloser(bytes.NewReader(body)) - options := docker.CreateExecOptions{} - if err := json.Unmarshal(body, &options); err != nil { + if err := unmarshalRequestBody(r, &options); err != nil { return err } diff --git a/proxy/inspect_container_interceptor.go b/proxy/inspect_container_interceptor.go index 791fd653..2010660b 100644 --- a/proxy/inspect_container_interceptor.go +++ b/proxy/inspect_container_interceptor.go @@ -1,9 +1,6 @@ package proxy import ( - "bytes" - "encoding/json" - "io/ioutil" "net/http" "github.com/fsouza/go-dockerclient" @@ -21,14 +18,8 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error return nil } - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return err - } - r.Body.Close() - container := &docker.Container{} - if err := json.Unmarshal(body, container); err != nil { + if err := unmarshalResponseBody(r, container); err != nil { return err } @@ -36,15 +27,7 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error Log.Warningf("Inspecting container %s failed: %s", container.ID, err) } - newBody, err := json.Marshal(container) - if err != nil { - return err - } - r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) - r.ContentLength = int64(len(newBody)) - r.TransferEncoding = nil // Stop it being chunked, because that hangs - - return nil + return marshalResponseBody(r, container) } func updateContainerNetworkSettings(container *docker.Container) error { diff --git a/proxy/inspect_exec_interceptor.go b/proxy/inspect_exec_interceptor.go index f12ecbcb..d787fcdf 100644 --- a/proxy/inspect_exec_interceptor.go +++ b/proxy/inspect_exec_interceptor.go @@ -1,9 +1,6 @@ package proxy import ( - "bytes" - "encoding/json" - "io/ioutil" "net/http" "github.com/fsouza/go-dockerclient" @@ -21,14 +18,8 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error { return nil } - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return err - } - r.Body.Close() - exec := &docker.ExecInspect{} - if err := json.Unmarshal(body, exec); err != nil { + if err := unmarshalResponseBody(r, &exec); err != nil { return err } @@ -36,13 +27,5 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error { Log.Warningf("Inspecting exec %s failed: %s", exec.ID, err) } - newBody, err := json.Marshal(exec) - if err != nil { - return err - } - r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) - r.ContentLength = int64(len(newBody)) - r.TransferEncoding = nil // Stop it being chunked, because that hangs - - return nil + return marshalResponseBody(r, exec) } From 11b655154c2d38e200bdf139ab65c30cdc6c1751 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 2 Sep 2015 13:38:59 +0100 Subject: [PATCH 3/5] "Clarifying" proxy --rewrite-inspect documentation --- site/proxy.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/site/proxy.md b/site/proxy.md index ed54bd40..cb47673d 100644 --- a/site/proxy.md +++ b/site/proxy.md @@ -104,9 +104,13 @@ Multiple IP addresses and networks can be supplied in the `WEAVE_CIDR` variable by space-separating them, as in `WEAVE_CIDR="10.2.1.1/24 10.2.2.1/24"`. -The docker IP will still be returned by `docker inspect`. If you want -`docker inspect` to return the weave IP instead, then the proxy must -be launced with the `--rewrite-inspect` flag. +The docker NetworkSettings (including IP address, MacAddress, and +IPPrefixLen), will still be returned by `docker inspect`. If you want +`docker inspect` to return the weave NetworkSettings instead, then the +proxy must be launced with the `--rewrite-inspect` flag. This will +only substitute in the weave network settings when the container has a +weave IP. If a container has more than one weave IP, the inspect call +will only include one of them. host1$ weave launch-router && weave launch-proxy --rewrite-inspect From 32a12578f3b7d33951fa684abfc485a8b308fe5b Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 2 Sep 2015 13:39:57 +0100 Subject: [PATCH 4/5] Don't use go-dockerclient to en/decode inspects in the proxy --- proxy/common.go | 4 +-- proxy/inspect_container_interceptor.go | 26 ++++++++++----- proxy/inspect_exec_interceptor.go | 12 ++++--- proxy/json.go | 46 ++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 proxy/json.go diff --git a/proxy/common.go b/proxy/common.go index 323b9611..7579cf99 100644 --- a/proxy/common.go +++ b/proxy/common.go @@ -105,8 +105,8 @@ func inspectContainerInPath(client *docker.Client, path string) (*docker.Contain return container, err } -func weaveContainerIPs(container *docker.Container) (mac string, ips []net.IP, nets []*net.IPNet, err error) { - stdout, stderr, err := callWeave("ps", container.ID) +func weaveContainerIPs(containerID string) (mac string, ips []net.IP, nets []*net.IPNet, err error) { + stdout, stderr, err := callWeave("ps", containerID) if err != nil || len(stderr) > 0 { err = errors.New(string(stderr)) return diff --git a/proxy/inspect_container_interceptor.go b/proxy/inspect_container_interceptor.go index 2010660b..59f9a062 100644 --- a/proxy/inspect_container_interceptor.go +++ b/proxy/inspect_container_interceptor.go @@ -3,7 +3,6 @@ package proxy import ( "net/http" - "github.com/fsouza/go-dockerclient" . "github.com/weaveworks/weave/common" ) @@ -18,26 +17,35 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error return nil } - container := &docker.Container{} - if err := unmarshalResponseBody(r, container); err != nil { + container := map[string]interface{}{} + if err := unmarshalResponseBody(r, &container); err != nil { return err } if err := updateContainerNetworkSettings(container); err != nil { - Log.Warningf("Inspecting container %s failed: %s", container.ID, err) + Log.Warningf("Inspecting container %s failed: %s", container["Id"], err) } return marshalResponseBody(r, container) } -func updateContainerNetworkSettings(container *docker.Container) error { - mac, ips, nets, err := weaveContainerIPs(container) +func updateContainerNetworkSettings(container map[string]interface{}) error { + containerID, err := lookupString(container, "Id") + if err != nil { + return err + } + + mac, ips, nets, err := weaveContainerIPs(containerID) if err != nil || len(ips) == 0 { return err } - container.NetworkSettings.MacAddress = mac - container.NetworkSettings.IPAddress = ips[0].String() - container.NetworkSettings.IPPrefixLen, _ = nets[0].Mask.Size() + networkSettings, err := lookupObject(container, "NetworkSettings") + if err != nil { + return err + } + networkSettings["MacAddress"] = mac + networkSettings["IPAddress"] = ips[0].String() + networkSettings["IPPrefixLen"], _ = nets[0].Mask.Size() return nil } diff --git a/proxy/inspect_exec_interceptor.go b/proxy/inspect_exec_interceptor.go index d787fcdf..89707146 100644 --- a/proxy/inspect_exec_interceptor.go +++ b/proxy/inspect_exec_interceptor.go @@ -3,7 +3,6 @@ package proxy import ( "net/http" - "github.com/fsouza/go-dockerclient" . "github.com/weaveworks/weave/common" ) @@ -18,13 +17,18 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error { return nil } - exec := &docker.ExecInspect{} + exec := map[string]interface{}{} if err := unmarshalResponseBody(r, &exec); err != nil { return err } - if err := updateContainerNetworkSettings(&exec.Container); err != nil { - Log.Warningf("Inspecting exec %s failed: %s", exec.ID, err) + container, err := lookupObject(exec, "Container") + if err != nil { + return err + } + + if err := updateContainerNetworkSettings(container); err != nil { + Log.Warningf("Inspecting exec %s failed: %s", exec["Id"], err) } return marshalResponseBody(r, exec) diff --git a/proxy/json.go b/proxy/json.go new file mode 100644 index 00000000..d3728e18 --- /dev/null +++ b/proxy/json.go @@ -0,0 +1,46 @@ +package proxy + +import ( + "fmt" +) + +type UnmarshalWrongTypeError struct { + Field, Expected string + Got interface{} +} + +func (e *UnmarshalWrongTypeError) Error() string { + return fmt.Sprintf("Wrong type for %s field, expected %s, but got %T", e.Field, e.Expected, e.Got) +} + +// For parsing json objects. Look up (or create) a child object in the given map. +func lookupObject(m map[string]interface{}, key string) (map[string]interface{}, error) { + iface, ok := m[key] + if !ok || iface == nil { + result := map[string]interface{}{} + m[key] = result + return result, nil + } + + result, ok := iface.(map[string]interface{}) + if !ok { + return nil, &UnmarshalWrongTypeError{key, "object", iface} + } + + return result, nil +} + +// For parsing json objects. Look up (or create) a string in the given map. +func lookupString(m map[string]interface{}, key string) (string, error) { + iface, ok := m[key] + if !ok || iface == nil { + return "", nil + } + + result, ok := iface.(string) + if !ok { + return "", &UnmarshalWrongTypeError{key, "string", iface} + } + + return result, nil +} From 633df0cdafb5ac3930e8bd34c2035a7470be04d7 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 4 Sep 2015 13:35:14 +0100 Subject: [PATCH 5/5] refactor: extract docker API endpoint regexp creation --- proxy/proxy.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 0d91fe9e..f9a64076 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -24,16 +24,20 @@ const ( ) var ( - containerCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/create$") - containerStartRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/(re)?start$") - containerInspectRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/json$") - execCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/exec$") - execInspectRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/exec/[^/]*/json$") + containerCreateRegexp = dockerAPIEndpoint("containers/create") + containerStartRegexp = dockerAPIEndpoint("containers/[^/]*/(re)?start") + containerInspectRegexp = dockerAPIEndpoint("containers/[^/]*/json") + execCreateRegexp = dockerAPIEndpoint("containers/[^/]*/exec") + execInspectRegexp = dockerAPIEndpoint("exec/[^/]*/json") ErrWeaveCIDRNone = errors.New("the container was created with the '-e WEAVE_CIDR=none' option") ErrNoDefaultIPAM = errors.New("the container was created without specifying an IP address with '-e WEAVE_CIDR=...' and the proxy was started with the '--no-default-ipalloc' option") ) +func dockerAPIEndpoint(endpoint string) *regexp.Regexp { + return regexp.MustCompile("^(/v[0-9\\.]*)?/" + endpoint + "$") +} + type Config struct { HostnameMatch string HostnameReplacement string