Skip to content

Commit

Permalink
Merge pull request #1616 from weaveworks/1549-inspect-wait
Browse files Browse the repository at this point in the history
Make inspect-rewrite wait for start
  • Loading branch information
paulbellamy committed Nov 2, 2015
2 parents a633614 + f7cb2ef commit fa97261
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
23 changes: 1 addition & 22 deletions proxy/inspect_container_interceptor.go
Expand Up @@ -22,30 +22,9 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error
return err
}

if err := updateContainerNetworkSettings(container); 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) error {
containerID, err := container.String("Id")
if err != nil {
return err
}

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
}
2 changes: 1 addition & 1 deletion proxy/inspect_exec_interceptor.go
Expand Up @@ -27,7 +27,7 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error {
return err
}

if err := updateContainerNetworkSettings(container); err != nil {
if err := i.proxy.updateContainerNetworkSettings(container); err != nil {
Log.Warningf("Inspecting exec %s failed: %s", exec["Id"], err)
}

Expand Down
42 changes: 42 additions & 0 deletions proxy/proxy.go
Expand Up @@ -358,6 +358,24 @@ 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) error {
var ch chan error
proxy.Lock()
for _, wait := range proxy.waiters {
if ident == wait.ident && !wait.done {
ch = wait.ch
break
}
}
proxy.Unlock()
if ch != nil {
Log.Debugf("Wait for start of container %s", ident)
return <-ch
}
return nil
}

func (proxy *Proxy) ContainerDied(ident string) {
}

Expand Down Expand Up @@ -495,3 +513,27 @@ 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
}

if err := proxy.waitForStartByIdent(containerID); err != nil {
return err
}
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
}

0 comments on commit fa97261

Please sign in to comment.