From a0f0367e60e4eca5dc6d963679721dc48a305c2f Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Fri, 16 Oct 2015 15:33:43 +0100 Subject: [PATCH 1/2] [proxy] don't kill containers when starting the proxy --- proxy/proxy.go | 21 +++++++++++++++------ test/640_proxy_restart_reattaches_test.sh | 6 ++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 2f2f4e3d..adc784c4 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -116,10 +116,16 @@ func NewProxy(c Config) (*Proxy, error) { func (proxy *Proxy) AttachExistingContainers() { containers, _ := proxy.client.ListContainers(docker.ListContainersOptions{}) - for _, cont := range containers { - if strings.HasPrefix(cont.Command, weaveWaitEntrypoint[0]) { - proxy.ContainerStarted(cont.ID) + for _, apiContainer := range containers { + container, err := proxy.client.InspectContainer(apiContainer.ID) + if err != nil { + Log.Warningf("Error inspecting container %s: %v", apiContainer.ID, err) + continue } + if containerShouldAttach(container) { + proxy.attach(container, false) + } + proxy.notifyWaiters(container.ID) } } @@ -295,7 +301,7 @@ func (proxy *Proxy) ContainerStarted(ident string) { } // If this was a container we modified the entrypoint for, attach it to the network if containerShouldAttach(container) { - proxy.attach(container) + proxy.attach(container, true) } proxy.notifyWaiters(container.ID) } @@ -345,7 +351,7 @@ func (proxy *Proxy) waitForStart(r *http.Request) { func (proxy *Proxy) ContainerDied(ident string) { } -func (proxy *Proxy) attach(container *docker.Container) error { +func (proxy *Proxy) attach(container *docker.Container, orDie bool) error { cidrs, err := proxy.weaveCIDRs(container.HostConfig.NetworkMode, container.Config.Env) if err != nil { Log.Infof("Leaving container %s alone because %s", container.ID, err) @@ -357,7 +363,10 @@ func (proxy *Proxy) attach(container *docker.Container) error { if !proxy.NoRewriteHosts { args = append(args, "--rewrite-hosts") } - args = append(args, "--or-die", container.ID) + if orDie { + args = append(args, "--or-die") + } + args = append(args, container.ID) if _, stderr, err := callWeave(args...); err != nil { Log.Warningf("Attaching container %s to weave network failed: %s", container.ID, string(stderr)) return errors.New(string(stderr)) diff --git a/test/640_proxy_restart_reattaches_test.sh b/test/640_proxy_restart_reattaches_test.sh index 428092f3..ff02e678 100755 --- a/test/640_proxy_restart_reattaches_test.sh +++ b/test/640_proxy_restart_reattaches_test.sh @@ -25,6 +25,12 @@ run_on $HOST1 sudo kill -KILL $(docker_on $HOST1 inspect --format='{{.State.Pid} sleep 1 check_attached +# Restarting proxy shouldn't kill unattachable containers +proxy_start_container $HOST1 -di --name=c3 --restart=always # Use ipam, so it won't be attachable w/o weave +weave_on $HOST1 stop +weave_on $HOST1 launch-proxy +assert_raises "proxy exec_on $HOST1 c3 $CHECK_ETHWE_UP" + # Restart docker itself, using different commands for systemd- and upstart-managed. run_on $HOST1 sh -c "command -v systemctl >/dev/null && sudo systemctl restart docker || sudo service docker restart" sleep 1 From ace6c01b00113a8cfa80f06de7f93a01f94f6252 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Mon, 19 Oct 2015 11:33:12 +0100 Subject: [PATCH 2/2] Review feedback --- proxy/proxy.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index adc784c4..3997426e 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -116,16 +116,17 @@ func NewProxy(c Config) (*Proxy, error) { func (proxy *Proxy) AttachExistingContainers() { containers, _ := proxy.client.ListContainers(docker.ListContainersOptions{}) - for _, apiContainer := range containers { - container, err := proxy.client.InspectContainer(apiContainer.ID) + for _, c := range containers { + container, err := proxy.client.InspectContainer(c.ID) if err != nil { - Log.Warningf("Error inspecting container %s: %v", apiContainer.ID, err) + if _, ok := err.(*docker.NoSuchContainer); !ok { + Log.Warningf("unable to attach existing container %s since inspecting it failed: %v", c.ID, err) + } continue } - if containerShouldAttach(container) { + if containerShouldAttach(container) && (container.State.Running || container.State.Paused) { proxy.attach(container, false) } - proxy.notifyWaiters(container.ID) } } @@ -296,7 +297,9 @@ func (proxy *Proxy) listen(protoAndAddr string) (net.Listener, string, error) { func (proxy *Proxy) ContainerStarted(ident string) { container, err := proxy.client.InspectContainer(ident) if err != nil { - Log.Warningf("Error inspecting container %s: %v", ident, err) + if _, ok := err.(*docker.NoSuchContainer); !ok { + Log.Warningf("unable to attach new container %s since inspecting it failed: %v", ident, err) + } return } // If this was a container we modified the entrypoint for, attach it to the network