From 5eaff802d4169b5c4904deae83dc6d17a72a451f Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 21 Oct 2015 18:09:33 +0100 Subject: [PATCH 1/3] Make inspect-rewrite wait if the start operation is still in progress --- proxy/inspect_container_interceptor.go | 5 +++-- proxy/inspect_exec_interceptor.go | 2 +- proxy/proxy.go | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/proxy/inspect_container_interceptor.go b/proxy/inspect_container_interceptor.go index 7916ea9a..f4d96903 100644 --- a/proxy/inspect_container_interceptor.go +++ b/proxy/inspect_container_interceptor.go @@ -22,19 +22,20 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error return err } - if err := updateContainerNetworkSettings(container); err != nil { + if err := updateContainerNetworkSettings(container, i.proxy); err != nil { Log.Warningf("Inspecting container %s failed: %s", container["Id"], err) } return marshalResponseBody(r, container) } -func updateContainerNetworkSettings(container jsonObject) error { +func updateContainerNetworkSettings(container jsonObject, proxy *Proxy) error { containerID, err := container.String("Id") if err != nil { return err } + proxy.waitForStartByIdent(containerID) mac, ips, nets, err := weaveContainerIPs(containerID) if err != nil || len(ips) == 0 { return err diff --git a/proxy/inspect_exec_interceptor.go b/proxy/inspect_exec_interceptor.go index 970c3142..339d3343 100644 --- a/proxy/inspect_exec_interceptor.go +++ b/proxy/inspect_exec_interceptor.go @@ -27,7 +27,7 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error { return err } - if err := updateContainerNetworkSettings(container); err != nil { + if err := updateContainerNetworkSettings(container, i.proxy); err != nil { Log.Warningf("Inspecting exec %s failed: %s", exec["Id"], err) } diff --git a/proxy/proxy.go b/proxy/proxy.go index f087d769..749de0ea 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -358,6 +358,23 @@ func (proxy *Proxy) waitForStart(r *http.Request) error { return nil } +// If some other operation is waiting for a container to start, join in the wait +func (proxy *Proxy) waitForStartByIdent(ident string) { + var ch chan struct{} + proxy.Lock() + for _, wait := range proxy.waiters { + if ident == wait.ident { + ch = wait.ch + break + } + } + proxy.Unlock() + if ch != nil { + Log.Debugf("Wait for start of container %s", ident) + <-ch + } +} + func (proxy *Proxy) ContainerDied(ident string) { } From 949f8bb2e7fdcc5da14de2e9e7ff9afab80bc875 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 30 Oct 2015 13:33:25 +0000 Subject: [PATCH 2/3] Move updateContainerNetworkSettings() into proxy --- proxy/inspect_container_interceptor.go | 24 +----------------------- proxy/inspect_exec_interceptor.go | 2 +- proxy/proxy.go | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/proxy/inspect_container_interceptor.go b/proxy/inspect_container_interceptor.go index f4d96903..590649d4 100644 --- a/proxy/inspect_container_interceptor.go +++ b/proxy/inspect_container_interceptor.go @@ -22,31 +22,9 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error return err } - if err := updateContainerNetworkSettings(container, i.proxy); err != nil { + if err := i.proxy.updateContainerNetworkSettings(container); err != nil { Log.Warningf("Inspecting container %s failed: %s", container["Id"], err) } return marshalResponseBody(r, container) } - -func updateContainerNetworkSettings(container jsonObject, proxy *Proxy) error { - containerID, err := container.String("Id") - if err != nil { - return err - } - - proxy.waitForStartByIdent(containerID) - mac, ips, nets, err := weaveContainerIPs(containerID) - if err != nil || len(ips) == 0 { - return err - } - - networkSettings, err := container.Object("NetworkSettings") - if err != nil { - return err - } - networkSettings["MacAddress"] = mac - networkSettings["IPAddress"] = ips[0].String() - networkSettings["IPPrefixLen"], _ = nets[0].Mask.Size() - return nil -} diff --git a/proxy/inspect_exec_interceptor.go b/proxy/inspect_exec_interceptor.go index 339d3343..32e70940 100644 --- a/proxy/inspect_exec_interceptor.go +++ b/proxy/inspect_exec_interceptor.go @@ -27,7 +27,7 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error { return err } - if err := updateContainerNetworkSettings(container, i.proxy); err != nil { + if err := i.proxy.updateContainerNetworkSettings(container); err != nil { Log.Warningf("Inspecting exec %s failed: %s", exec["Id"], err) } diff --git a/proxy/proxy.go b/proxy/proxy.go index 749de0ea..3a410ea8 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -512,3 +512,25 @@ func (proxy *Proxy) getDNSDomain() (domain string) { return string(b) } + +func (proxy *Proxy) updateContainerNetworkSettings(container jsonObject) error { + containerID, err := container.String("Id") + if err != nil { + return err + } + + proxy.waitForStartByIdent(containerID) + mac, ips, nets, err := weaveContainerIPs(containerID) + if err != nil || len(ips) == 0 { + return err + } + + networkSettings, err := container.Object("NetworkSettings") + if err != nil { + return err + } + networkSettings["MacAddress"] = mac + networkSettings["IPAddress"] = ips[0].String() + networkSettings["IPPrefixLen"], _ = nets[0].Mask.Size() + return nil +} From f7cb2ef25d990915a87672f644ee320a76553278 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 30 Oct 2015 13:46:30 +0000 Subject: [PATCH 3/3] have waitForStartByIdent() return an error --- proxy/proxy.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 3a410ea8..e90c7f57 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -359,11 +359,11 @@ func (proxy *Proxy) waitForStart(r *http.Request) error { } // If some other operation is waiting for a container to start, join in the wait -func (proxy *Proxy) waitForStartByIdent(ident string) { - var ch chan struct{} +func (proxy *Proxy) waitForStartByIdent(ident string) error { + var ch chan error proxy.Lock() for _, wait := range proxy.waiters { - if ident == wait.ident { + if ident == wait.ident && !wait.done { ch = wait.ch break } @@ -371,8 +371,9 @@ func (proxy *Proxy) waitForStartByIdent(ident string) { proxy.Unlock() if ch != nil { Log.Debugf("Wait for start of container %s", ident) - <-ch + return <-ch } + return nil } func (proxy *Proxy) ContainerDied(ident string) { @@ -519,7 +520,9 @@ func (proxy *Proxy) updateContainerNetworkSettings(container jsonObject) error { return err } - proxy.waitForStartByIdent(containerID) + if err := proxy.waitForStartByIdent(containerID); err != nil { + return err + } mac, ips, nets, err := weaveContainerIPs(containerID) if err != nil || len(ips) == 0 { return err