From 5eaff802d4169b5c4904deae83dc6d17a72a451f Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 21 Oct 2015 18:09:33 +0100 Subject: [PATCH] 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) { }