From 5c70c7c71a731c0d5065f65f11484d8f3a512061 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 8 Jun 2015 22:49:54 +0100 Subject: [PATCH] simplify interceptor structs by referencing proxy --- proxy/create_container_interceptor.go | 15 +++++---------- proxy/create_exec_interceptor.go | 9 +++------ proxy/proxy.go | 6 +++--- proxy/start_container_interceptor.go | 14 +++++--------- 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index 7edf0500..4bd59e61 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -11,12 +11,7 @@ import ( . "github.com/weaveworks/weave/common" ) -type createContainerInterceptor struct { - client *docker.Client - dockerBridgeIP string - withDNS bool - withIPAM bool -} +type createContainerInterceptor struct{ proxy *Proxy } type createContainerRequestBody struct { *docker.Config @@ -36,7 +31,7 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { return err } - if cidrs, ok := weaveCIDRsFromConfig(container.Config); ok || i.withIPAM { + if cidrs, ok := weaveCIDRsFromConfig(container.Config); ok || i.proxy.WithIPAM { Info.Printf("Creating container with WEAVE_CIDR \"%s\"", strings.Join(cidrs, " ")) if container.HostConfig == nil { container.HostConfig = &docker.HostConfig{} @@ -65,7 +60,7 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Config) error { if len(container.Entrypoint) == 0 { - image, err := i.client.InspectImage(container.Image) + image, err := i.proxy.client.InspectImage(container.Image) if err != nil { return err } @@ -84,11 +79,11 @@ func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Co } func (i *createContainerInterceptor) setWeaveDNS(container *createContainerRequestBody) error { - if !i.withDNS { + if !i.proxy.WithDNS { return nil } - container.HostConfig.DNS = append(container.HostConfig.DNS, i.dockerBridgeIP) + container.HostConfig.DNS = append(container.HostConfig.DNS, i.proxy.dockerBridgeIP) if len(container.HostConfig.DNSSearch) == 0 { container.HostConfig.DNSSearch = []string{"."} diff --git a/proxy/create_exec_interceptor.go b/proxy/create_exec_interceptor.go index 6c604aee..a420c4d6 100644 --- a/proxy/create_exec_interceptor.go +++ b/proxy/create_exec_interceptor.go @@ -10,10 +10,7 @@ import ( . "github.com/weaveworks/weave/common" ) -type createExecInterceptor struct { - client *docker.Client - withIPAM bool -} +type createExecInterceptor struct{ proxy *Proxy } func (i *createExecInterceptor) InterceptRequest(r *http.Request) error { body, err := ioutil.ReadAll(r.Body) @@ -27,12 +24,12 @@ func (i *createExecInterceptor) InterceptRequest(r *http.Request) error { return err } - container, err := inspectContainerInPath(i.client, r.URL.Path) + container, err := inspectContainerInPath(i.proxy.client, r.URL.Path) if err != nil { return err } - if cidrs, ok := weaveCIDRsFromConfig(container.Config); ok || i.withIPAM { + if cidrs, ok := weaveCIDRsFromConfig(container.Config); ok || i.proxy.WithIPAM { 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 fc9c86b4..240e098b 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -87,11 +87,11 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { var i interceptor switch { case containerCreateRegexp.MatchString(path): - i = &createContainerInterceptor{proxy.client, proxy.dockerBridgeIP, proxy.WithDNS, proxy.WithIPAM} + i = &createContainerInterceptor{proxy} case containerStartRegexp.MatchString(path): - i = &startContainerInterceptor{proxy.client, proxy.WithDNS, proxy.WithIPAM} + i = &startContainerInterceptor{proxy} case execCreateRegexp.MatchString(path): - i = &createExecInterceptor{proxy.client, proxy.WithIPAM} + i = &createExecInterceptor{proxy} default: i = &nullInterceptor{} } diff --git a/proxy/start_container_interceptor.go b/proxy/start_container_interceptor.go index e615b86a..5ac2fdd2 100644 --- a/proxy/start_container_interceptor.go +++ b/proxy/start_container_interceptor.go @@ -9,24 +9,20 @@ import ( . "github.com/weaveworks/weave/common" ) -type startContainerInterceptor struct { - client *docker.Client - withDNS bool - withIPAM bool -} +type startContainerInterceptor struct{ proxy *Proxy } func (i *startContainerInterceptor) InterceptRequest(r *http.Request) error { return nil } func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { - container, err := inspectContainerInPath(i.client, r.Request.URL.Path) + container, err := inspectContainerInPath(i.proxy.client, r.Request.URL.Path) if err != nil { return err } cidrs, ok := weaveCIDRsFromConfig(container.Config) - if !ok && !i.withIPAM { + if !ok && !i.proxy.WithIPAM { Debug.Print("No Weave CIDR, ignoring") return nil } @@ -35,10 +31,10 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { args = append(args, cidrs...) args = append(args, container.ID) if output, err := callWeave(args...); err != nil { - i.client.KillContainer(docker.KillContainerOptions{ID: container.ID}) + i.proxy.client.KillContainer(docker.KillContainerOptions{ID: container.ID}) Warning.Printf("Attaching container %s to weave network failed: %s", container.ID, string(output)) return errors.New(string(output)) } - return i.client.KillContainer(docker.KillContainerOptions{ID: container.ID, Signal: docker.SIGUSR2}) + return i.proxy.client.KillContainer(docker.KillContainerOptions{ID: container.ID, Signal: docker.SIGUSR2}) }