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
|
return container, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func weaveContainerIPs(container *docker.Container) (mac string, ips []net.IP, nets []*net.IPNet, err error) {
|
func weaveContainerIPs(containerID string) (mac string, ips []net.IP, nets []*net.IPNet, err error) {
|
||||||
stdout, stderr, err := callWeave("ps", container.ID)
|
stdout, stderr, err := callWeave("ps", containerID)
|
||||||
if err != nil || len(stderr) > 0 {
|
if err != nil || len(stderr) > 0 {
|
||||||
err = errors.New(string(stderr))
|
err = errors.New(string(stderr))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
. "github.com/weaveworks/weave/common"
|
. "github.com/weaveworks/weave/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,26 +17,35 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
container := &docker.Container{}
|
container := map[string]interface{}{}
|
||||||
if err := unmarshalResponseBody(r, container); err != nil {
|
if err := unmarshalResponseBody(r, &container); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := updateContainerNetworkSettings(container); err != nil {
|
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)
|
return marshalResponseBody(r, container)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateContainerNetworkSettings(container *docker.Container) error {
|
func updateContainerNetworkSettings(container map[string]interface{}) error {
|
||||||
mac, ips, nets, err := weaveContainerIPs(container)
|
containerID, err := lookupString(container, "Id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
mac, ips, nets, err := weaveContainerIPs(containerID)
|
||||||
if err != nil || len(ips) == 0 {
|
if err != nil || len(ips) == 0 {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
container.NetworkSettings.MacAddress = mac
|
networkSettings, err := lookupObject(container, "NetworkSettings")
|
||||||
container.NetworkSettings.IPAddress = ips[0].String()
|
if err != nil {
|
||||||
container.NetworkSettings.IPPrefixLen, _ = nets[0].Mask.Size()
|
return err
|
||||||
|
}
|
||||||
|
networkSettings["MacAddress"] = mac
|
||||||
|
networkSettings["IPAddress"] = ips[0].String()
|
||||||
|
networkSettings["IPPrefixLen"], _ = nets[0].Mask.Size()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,6 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
. "github.com/weaveworks/weave/common"
|
. "github.com/weaveworks/weave/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,13 +17,18 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
exec := &docker.ExecInspect{}
|
exec := map[string]interface{}{}
|
||||||
if err := unmarshalResponseBody(r, &exec); err != nil {
|
if err := unmarshalResponseBody(r, &exec); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := updateContainerNetworkSettings(&exec.Container); err != nil {
|
container, err := lookupObject(exec, "Container")
|
||||||
Log.Warningf("Inspecting exec %s failed: %s", exec.ID, err)
|
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)
|
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