Merge pull request #879 from rade/proxy_interceptor_refactor

Proxy interceptor refactor
This commit is contained in:
Paul Bellamy committed 2015-06-09 14:50:54 +01:00
commit e324c15b04
5 files changed
+23 -49

No files matched your search

+5 -10
View File
@@ -11,12 +11,7 @@ import (
. "github.com/weaveworks/weave/common" . "github.com/weaveworks/weave/common"
) )
type createContainerInterceptor struct { type createContainerInterceptor struct{ proxy *Proxy }
client *docker.Client
dockerBridgeIP string
withDNS bool
withIPAM bool
}
type createContainerRequestBody struct { type createContainerRequestBody struct {
*docker.Config *docker.Config
@@ -36,7 +31,7 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error {
return err 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, " ")) Info.Printf("Creating container with WEAVE_CIDR \"%s\"", strings.Join(cidrs, " "))
if container.HostConfig == nil { if container.HostConfig == nil {
container.HostConfig = &docker.HostConfig{} container.HostConfig = &docker.HostConfig{}
@@ -65,7 +60,7 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error {
func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Config) error { func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Config) error {
if len(container.Entrypoint) == 0 { if len(container.Entrypoint) == 0 {
image, err := i.client.InspectImage(container.Image) image, err := i.proxy.client.InspectImage(container.Image)
if err != nil { if err != nil {
return err return err
} }
@@ -84,11 +79,11 @@ func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Co
} }
func (i *createContainerInterceptor) setWeaveDNS(container *createContainerRequestBody) error { func (i *createContainerInterceptor) setWeaveDNS(container *createContainerRequestBody) error {
if !i.withDNS { if !i.proxy.WithDNS {
return nil 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 { if len(container.HostConfig.DNSSearch) == 0 {
container.HostConfig.DNSSearch = []string{"."} container.HostConfig.DNSSearch = []string{"."}
+3 -6
View File
@@ -10,10 +10,7 @@ import (
. "github.com/weaveworks/weave/common" . "github.com/weaveworks/weave/common"
) )
type createExecInterceptor struct { type createExecInterceptor struct{ proxy *Proxy }
client *docker.Client
withIPAM bool
}
func (i *createExecInterceptor) InterceptRequest(r *http.Request) error { func (i *createExecInterceptor) InterceptRequest(r *http.Request) error {
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
@@ -27,12 +24,12 @@ func (i *createExecInterceptor) InterceptRequest(r *http.Request) error {
return err return err
} }
container, err := inspectContainerInPath(i.client, r.URL.Path) container, err := inspectContainerInPath(i.proxy.client, r.URL.Path)
if err != nil { if err != nil {
return err 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, " ")) Info.Printf("Exec in container %s with WEAVE_CIDR \"%s\"", container.ID, strings.Join(cidrs, " "))
cmd := append(weaveWaitEntrypoint, "-s") cmd := append(weaveWaitEntrypoint, "-s")
options.Cmd = append(cmd, options.Cmd...) options.Cmd = append(cmd, options.Cmd...)
+6 -8
View File
@@ -84,20 +84,18 @@ func NewProxy(c Config) (*Proxy, error) {
func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Info.Printf("%s %s", r.Method, r.URL) Info.Printf("%s %s", r.Method, r.URL)
path := r.URL.Path path := r.URL.Path
var i interceptor
switch { switch {
case containerCreateRegexp.MatchString(path): case containerCreateRegexp.MatchString(path):
proxy.serveWithInterceptor(&createContainerInterceptor{proxy.client, proxy.dockerBridgeIP, proxy.WithDNS, proxy.WithIPAM}, w, r) i = &createContainerInterceptor{proxy}
case containerStartRegexp.MatchString(path): case containerStartRegexp.MatchString(path):
proxy.serveWithInterceptor(&startContainerInterceptor{proxy.client, proxy.WithDNS, proxy.WithIPAM}, w, r) i = &startContainerInterceptor{proxy}
case execCreateRegexp.MatchString(path): case execCreateRegexp.MatchString(path):
proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.WithIPAM}, w, r) i = &createExecInterceptor{proxy}
default: default:
proxy.serveWithInterceptor(&nullInterceptor{}, w, r) i = &nullInterceptor{}
} }
} proxy.Intercept(i, w, r)
func (proxy *Proxy) serveWithInterceptor(i interceptor, w http.ResponseWriter, r *http.Request) {
newClient(proxy.dial, i).ServeHTTP(w, r)
} }
func (proxy *Proxy) ListenAndServe() error { func (proxy *Proxy) ListenAndServe() error {
+4 -16
View File
@@ -14,20 +14,8 @@ import (
. "github.com/weaveworks/weave/common" . "github.com/weaveworks/weave/common"
) )
type client struct { func (proxy *Proxy) Intercept(i interceptor, w http.ResponseWriter, r *http.Request) {
interceptor if err := i.InterceptRequest(r); err != nil {
Dial func() (net.Conn, error)
}
func newClient(dial func() (net.Conn, error), i interceptor) *client {
return &client{
interceptor: i,
Dial: dial,
}
}
func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := c.InterceptRequest(r); err != nil {
_, isNoSuchContainer := err.(*docker.NoSuchContainer) _, isNoSuchContainer := err.(*docker.NoSuchContainer)
switch { switch {
case isNoSuchContainer: case isNoSuchContainer:
@@ -41,7 +29,7 @@ func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
conn, err := c.Dial() conn, err := proxy.dial()
if err != nil { if err != nil {
http.Error(w, "Could not connect to target", http.StatusInternalServerError) http.Error(w, "Could not connect to target", http.StatusInternalServerError)
Warning.Print(err) Warning.Print(err)
@@ -56,7 +44,7 @@ func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Warning.Print("Error forwarding request: ", err) Warning.Print("Error forwarding request: ", err)
return return
} }
err = c.InterceptResponse(resp) err = i.InterceptResponse(resp)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
Warning.Print("Error intercepting response: ", err) Warning.Print("Error intercepting response: ", err)
+5 -9
View File
@@ -9,24 +9,20 @@ import (
. "github.com/weaveworks/weave/common" . "github.com/weaveworks/weave/common"
) )
type startContainerInterceptor struct { type startContainerInterceptor struct{ proxy *Proxy }
client *docker.Client
withDNS bool
withIPAM bool
}
func (i *startContainerInterceptor) InterceptRequest(r *http.Request) error { func (i *startContainerInterceptor) InterceptRequest(r *http.Request) error {
return nil return nil
} }
func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { 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 { if err != nil {
return err return err
} }
cidrs, ok := weaveCIDRsFromConfig(container.Config) cidrs, ok := weaveCIDRsFromConfig(container.Config)
if !ok && !i.withIPAM { if !ok && !i.proxy.WithIPAM {
Debug.Print("No Weave CIDR, ignoring") Debug.Print("No Weave CIDR, ignoring")
return nil return nil
} }
@@ -35,10 +31,10 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error {
args = append(args, cidrs...) args = append(args, cidrs...)
args = append(args, container.ID) args = append(args, container.ID)
if output, err := callWeave(args...); err != nil { 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)) Warning.Printf("Attaching container %s to weave network failed: %s", container.ID, string(output))
return errors.New(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})
} }