Skip to content

Commit

Permalink
Don't use go-dockerclient to en/decode inspects in the proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Sep 4, 2015
1 parent 570f3cb commit 75fc999
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
4 changes: 2 additions & 2 deletions proxy/common.go
Expand Up @@ -105,8 +105,8 @@ func inspectContainerInPath(client *docker.Client, path string) (*docker.Contain
return container, err
}

func weaveContainerIPs(container *docker.Container) (mac string, ips []net.IP, nets []*net.IPNet, err error) {
stdout, stderr, err := callWeave("ps", container.ID)
func weaveContainerIPs(containerID string) (mac string, ips []net.IP, nets []*net.IPNet, err error) {
stdout, stderr, err := callWeave("ps", containerID)
if err != nil || len(stderr) > 0 {
err = errors.New(string(stderr))
return
Expand Down
26 changes: 17 additions & 9 deletions proxy/inspect_container_interceptor.go
Expand Up @@ -3,7 +3,6 @@ package proxy
import (
"net/http"

"github.com/fsouza/go-dockerclient"
. "github.com/weaveworks/weave/common"
)

Expand All @@ -18,26 +17,35 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error
return nil
}

container := &docker.Container{}
if err := unmarshalResponseBody(r, container); err != nil {
container := map[string]interface{}{}
if err := unmarshalResponseBody(r, &container); err != nil {
return err
}

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

return marshalResponseBody(r, container)
}

func updateContainerNetworkSettings(container *docker.Container) error {
mac, ips, nets, err := weaveContainerIPs(container)
func updateContainerNetworkSettings(container map[string]interface{}) error {
containerID, err := lookupString(container, "Id")
if err != nil {
return err
}

mac, ips, nets, err := weaveContainerIPs(containerID)
if err != nil || len(ips) == 0 {
return err
}

container.NetworkSettings.MacAddress = mac
container.NetworkSettings.IPAddress = ips[0].String()
container.NetworkSettings.IPPrefixLen, _ = nets[0].Mask.Size()
networkSettings, err := lookupObject(container, "NetworkSettings")
if err != nil {
return err
}
networkSettings["MacAddress"] = mac
networkSettings["IPAddress"] = ips[0].String()
networkSettings["IPPrefixLen"], _ = nets[0].Mask.Size()
return nil
}
12 changes: 8 additions & 4 deletions proxy/inspect_exec_interceptor.go
Expand Up @@ -3,7 +3,6 @@ package proxy
import (
"net/http"

"github.com/fsouza/go-dockerclient"
. "github.com/weaveworks/weave/common"
)

Expand All @@ -18,13 +17,18 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error {
return nil
}

exec := &docker.ExecInspect{}
exec := map[string]interface{}{}
if err := unmarshalResponseBody(r, &exec); err != nil {
return err
}

if err := updateContainerNetworkSettings(&exec.Container); err != nil {
Log.Warningf("Inspecting exec %s failed: %s", exec.ID, err)
container, err := lookupObject(exec, "Container")
if err != nil {
return err
}

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

return marshalResponseBody(r, exec)
Expand Down
46 changes: 46 additions & 0 deletions proxy/json.go
@@ -0,0 +1,46 @@
package proxy

import (
"fmt"
)

type UnmarshalWrongTypeError struct {
Field, Expected string
Got interface{}
}

func (e *UnmarshalWrongTypeError) Error() string {
return fmt.Sprintf("Wrong type for %s field, expected %s, but got %T", e.Field, e.Expected, e.Got)
}

// For parsing json objects. Look up (or create) a child object in the given map.
func lookupObject(m map[string]interface{}, key string) (map[string]interface{}, error) {
iface, ok := m[key]
if !ok || iface == nil {
result := map[string]interface{}{}
m[key] = result
return result, nil
}

result, ok := iface.(map[string]interface{})
if !ok {
return nil, &UnmarshalWrongTypeError{key, "object", iface}
}

return result, nil
}

// For parsing json objects. Look up (or create) a string in the given map.
func lookupString(m map[string]interface{}, key string) (string, error) {
iface, ok := m[key]
if !ok || iface == nil {
return "", nil
}

result, ok := iface.(string)
if !ok {
return "", &UnmarshalWrongTypeError{key, "string", iface}
}

return result, nil
}

0 comments on commit 75fc999

Please sign in to comment.