From 6c26a55b5a20e7dfced4d167442ed49fa5a869ad Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 17 Jun 2015 14:16:40 +0100 Subject: [PATCH] [proxy] refactor weaveCIDRsFromConfig slightly --- proxy/common.go | 13 ------------- proxy/create_container_interceptor.go | 2 +- proxy/create_exec_interceptor.go | 2 +- proxy/proxy.go | 13 +++++++++++++ proxy/start_container_interceptor.go | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/proxy/common.go b/proxy/common.go index 20d9dada..45ed79d8 100644 --- a/proxy/common.go +++ b/proxy/common.go @@ -8,7 +8,6 @@ import ( "net/http" "os/exec" "regexp" - "strings" "github.com/fsouza/go-dockerclient" . "github.com/weaveworks/weave/common" @@ -28,18 +27,6 @@ func callWeave(args ...string) ([]byte, error) { return out, err } -func weaveCIDRsFromConfig(config *docker.Config, noDefaultIPAM bool) ([]string, bool) { - for _, e := range config.Env { - if strings.HasPrefix(e, "WEAVE_CIDR=") { - if e[11:] == "none" { - return nil, false - } - return strings.Fields(e[11:]), true - } - } - return nil, !noDefaultIPAM -} - func marshalRequestBody(r *http.Request, body interface{}) error { newBody, err := json.Marshal(body) if err != nil { diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index e4a7aced..cf7e0f22 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -33,7 +33,7 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { return err } - if cidrs, ok := weaveCIDRsFromConfig(container.Config, i.proxy.NoDefaultIPAM); ok { + if cidrs, ok := i.proxy.weaveCIDRsFromConfig(container.Config); ok { Info.Printf("Creating container with WEAVE_CIDR \"%s\"", strings.Join(cidrs, " ")) if container.HostConfig == nil { container.HostConfig = &docker.HostConfig{} diff --git a/proxy/create_exec_interceptor.go b/proxy/create_exec_interceptor.go index 97ab716e..72e62730 100644 --- a/proxy/create_exec_interceptor.go +++ b/proxy/create_exec_interceptor.go @@ -29,7 +29,7 @@ func (i *createExecInterceptor) InterceptRequest(r *http.Request) error { return err } - if cidrs, ok := weaveCIDRsFromConfig(container.Config, i.proxy.NoDefaultIPAM); ok { + if cidrs, ok := i.proxy.weaveCIDRsFromConfig(container.Config); ok { Info.Printf("Exec in container %s with WEAVE_CIDR \"%s\"", container.ID, strings.Join(cidrs, " ")) cmd := append(weaveWaitEntrypoint, "-s") options.Cmd = append(cmd, options.Cmd...) diff --git a/proxy/proxy.go b/proxy/proxy.go index b1e8a24f..c391431f 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -6,6 +6,7 @@ import ( "net/http" "net/url" "regexp" + "strings" "github.com/fsouza/go-dockerclient" . "github.com/weaveworks/weave/common" @@ -113,3 +114,15 @@ func (proxy *Proxy) ListenAndServe() error { return (&http.Server{Handler: proxy}).Serve(listener) } + +func (proxy *Proxy) weaveCIDRsFromConfig(config *docker.Config) ([]string, bool) { + for _, e := range config.Env { + if strings.HasPrefix(e, "WEAVE_CIDR=") { + if e[11:] == "none" { + return nil, false + } + return strings.Fields(e[11:]), true + } + } + return nil, !proxy.NoDefaultIPAM +} diff --git a/proxy/start_container_interceptor.go b/proxy/start_container_interceptor.go index 3823021e..4d54680d 100644 --- a/proxy/start_container_interceptor.go +++ b/proxy/start_container_interceptor.go @@ -21,7 +21,7 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { return err } - cidrs, ok := weaveCIDRsFromConfig(container.Config, i.proxy.NoDefaultIPAM) + cidrs, ok := i.proxy.weaveCIDRsFromConfig(container.Config) if !ok { Debug.Print("No Weave CIDR, ignoring") return nil