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"
)
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{"."}
+3 -6
View File
@@ -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...)
+6 -8
View File
@@ -84,20 +84,18 @@ func NewProxy(c Config) (*Proxy, error) {
func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Info.Printf("%s %s", r.Method, r.URL)
path := r.URL.Path
var i interceptor
switch {
case containerCreateRegexp.MatchString(path):
proxy.serveWithInterceptor(&createContainerInterceptor{proxy.client, proxy.dockerBridgeIP, proxy.WithDNS, proxy.WithIPAM}, w, r)
i = &createContainerInterceptor{proxy}
case containerStartRegexp.MatchString(path):
proxy.serveWithInterceptor(&startContainerInterceptor{proxy.client, proxy.WithDNS, proxy.WithIPAM}, w, r)
i = &startContainerInterceptor{proxy}
case execCreateRegexp.MatchString(path):
proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.WithIPAM}, w, r)
i = &createExecInterceptor{proxy}
default:
proxy.serveWithInterceptor(&nullInterceptor{}, w, r)
i = &nullInterceptor{}
}
}
func (proxy *Proxy) serveWithInterceptor(i interceptor, w http.ResponseWriter, r *http.Request) {
newClient(proxy.dial, i).ServeHTTP(w, r)
proxy.Intercept(i, w, r)
}
func (proxy *Proxy) ListenAndServe() error {
+4 -16
View File
@@ -14,20 +14,8 @@ import (
. "github.com/weaveworks/weave/common"
)
type client struct {
interceptor
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 {
func (proxy *Proxy) Intercept(i interceptor, w http.ResponseWriter, r *http.Request) {
if err := i.InterceptRequest(r); err != nil {
_, isNoSuchContainer := err.(*docker.NoSuchContainer)
switch {
case isNoSuchContainer:
@@ -41,7 +29,7 @@ func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
conn, err := c.Dial()
conn, err := proxy.dial()
if err != nil {
http.Error(w, "Could not connect to target", http.StatusInternalServerError)
Warning.Print(err)
@@ -56,7 +44,7 @@ func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Warning.Print("Error forwarding request: ", err)
return
}
err = c.InterceptResponse(resp)
err = i.InterceptResponse(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Warning.Print("Error intercepting response: ", err)
+5 -9
View File
@@ -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})
}