Don't use go-dockerclient to en/decode inspects in the proxy
This commit is contained in:
1 parent
11b655154c
commit
32a12578f3
4 files changed
+73
-15
No files matched your search
+2
-2
@@ -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
|
||||
|
||||
@@ -3,7 +3,6 @@ package proxy
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
. "github.com/weaveworks/weave/common"
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package proxy
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
. "github.com/weaveworks/weave/common"
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user