diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index e6942a5a..44268bf4 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -55,10 +55,10 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { if container.HostConfig == nil { container.HostConfig = &docker.HostConfig{} } - container.HostConfig.VolumesFrom = append(container.HostConfig.VolumesFrom, "weaveproxy:ro") if container.Config == nil { container.Config = &docker.Config{} } + i.addWeaveWaitVolume(container.HostConfig) if err := i.setWeaveWaitEntrypoint(container.Config); err != nil { return err } @@ -79,6 +79,18 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { return nil } +func (i *createContainerInterceptor) addWeaveWaitVolume(hostConfig *docker.HostConfig) { + var binds []string + for _, bind := range hostConfig.Binds { + s := strings.Split(bind, ":") + if len(s) >= 2 && s[1] == "/w" { + continue + } + binds = append(binds, bind) + } + hostConfig.Binds = append(binds, fmt.Sprintf("%s:/w:ro", i.proxy.weaveWaitVolume)) +} + func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Config) error { if len(container.Entrypoint) == 0 { image, err := i.proxy.client.InspectImage(container.Image) diff --git a/proxy/proxy.go b/proxy/proxy.go index 5f1bbb06..a45a012b 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -50,6 +50,7 @@ type Proxy struct { client *docker.Client dockerBridgeIP string hostnameMatchRegexp *regexp.Regexp + weaveWaitVolume string } func NewProxy(c Config) (*Proxy, error) { @@ -79,6 +80,10 @@ func NewProxy(c Config) (*Proxy, error) { return nil, err } + if err = p.findWeaveWaitVolume(); err != nil { + return nil, err + } + return p, nil } @@ -86,6 +91,25 @@ func (proxy *Proxy) Dial() (net.Conn, error) { return net.Dial("unix", dockerSock) } +func (proxy *Proxy) findWeaveWaitVolume() error { + container, err := proxy.client.InspectContainer("weaveproxy") + if err != nil { + return fmt.Errorf("Could not find the weavewait volume: %s", err) + } + + if container.Volumes == nil { + return fmt.Errorf("Could not find the weavewait volume") + } + + volume, ok := container.Volumes["/w"] + if !ok { + return fmt.Errorf("Could not find the weavewait volume") + } + + proxy.weaveWaitVolume = volume + return nil +} + func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { Log.Infof("%s %s", r.Method, r.URL) path := r.URL.Path diff --git a/proxy/start_container_interceptor.go b/proxy/start_container_interceptor.go index 7353c07e..a699e510 100644 --- a/proxy/start_container_interceptor.go +++ b/proxy/start_container_interceptor.go @@ -16,6 +16,10 @@ func (i *startContainerInterceptor) InterceptRequest(r *http.Request) error { } func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { + if r.StatusCode < 200 || r.StatusCode >= 300 { // Docker didn't do the start + return nil + } + container, err := inspectContainerInPath(i.proxy.client, r.Request.URL.Path) if err != nil { return err diff --git a/test/610_proxy_wait_for_weave_test.sh b/test/610_proxy_wait_for_weave_test.sh index b995c040..79e452bb 100755 --- a/test/610_proxy_wait_for_weave_test.sh +++ b/test/610_proxy_wait_for_weave_test.sh @@ -29,4 +29,9 @@ assert_raises "proxy docker_on $HOST1 exec c3 true" # Check we can't modify weavewait assert_raises "proxy docker_on $HOST1 run -e 'WEAVE_CIDR=10.2.1.2/24' $BASE_IMAGE touch /w/w" 1 +# Check only user-specified volumes and /w are mounted +proxy docker_on $HOST1 create --name c4 -v /tmp/1:/srv1 -v /tmp/2:/srv2 -e 'WEAVE_CIDR=10.2.1.3/24' $BASE_IMAGE $CHECK_ETHWE_UP +proxy docker_on $HOST1 start -ai c4 +assert "docker_on $HOST1 inspect --format='{{range \$k, \$v := .Volumes}}{{\$k}} {{end}}' c4" "/srv1 /srv2 /w " + end_suite