[proxy] add --rewrite-inspect flag

To return the weave ip/mac for docker inspect calls.
This commit is contained in:
Paul Bellamy authored and Matthias Radestock committed 2015-09-04 13:40:06 +01:00
1 parent 145a527626
commit e21a204eed
8 files changed
+174 -3

No files matched your search

+32
View File
@@ -3,12 +3,15 @@ package proxy
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"github.com/fsouza/go-dockerclient"
. "github.com/weaveworks/weave/common"
@@ -62,3 +65,32 @@ 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)
if err != nil || len(stderr) > 0 {
err = errors.New(string(stderr))
return
}
if len(stdout) <= 0 {
return
}
fields := strings.Fields(string(stdout))
if len(fields) < 2 {
return
}
mac = fields[1]
var ip net.IP
var ipnet *net.IPNet
for _, cidr := range fields[2:] {
ip, ipnet, err = net.ParseCIDR(cidr)
if err != nil {
return
}
ips = append(ips, ip)
nets = append(nets, ipnet)
}
return
}
+60
View File
@@ -0,0 +1,60 @@
package proxy
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/fsouza/go-dockerclient"
. "github.com/weaveworks/weave/common"
)
type inspectContainerInterceptor struct{ proxy *Proxy }
func (i *inspectContainerInterceptor) InterceptRequest(r *http.Request) error {
return nil
}
func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error {
if !i.proxy.RewriteInspect {
return nil
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
r.Body.Close()
container := &docker.Container{}
if err := json.Unmarshal(body, container); err != nil {
return err
}
if err := updateContainerNetworkSettings(container); err != nil {
Log.Warningf("Inspecting container %s failed: %s", container.ID, err)
}
newBody, err := json.Marshal(container)
if err != nil {
return err
}
r.Body = ioutil.NopCloser(bytes.NewReader(newBody))
r.ContentLength = int64(len(newBody))
r.TransferEncoding = nil // Stop it being chunked, because that hangs
return nil
}
func updateContainerNetworkSettings(container *docker.Container) error {
mac, ips, nets, err := weaveContainerIPs(container)
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()
return nil
}
+48
View File
@@ -0,0 +1,48 @@
package proxy
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/fsouza/go-dockerclient"
. "github.com/weaveworks/weave/common"
)
type inspectExecInterceptor struct{ proxy *Proxy }
func (i *inspectExecInterceptor) InterceptRequest(r *http.Request) error {
return nil
}
func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error {
if !i.proxy.RewriteInspect {
return nil
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
r.Body.Close()
exec := &docker.ExecInspect{}
if err := json.Unmarshal(body, exec); err != nil {
return err
}
if err := updateContainerNetworkSettings(&exec.Container); err != nil {
Log.Warningf("Inspecting exec %s failed: %s", exec.ID, err)
}
newBody, err := json.Marshal(exec)
if err != nil {
return err
}
r.Body = ioutil.NopCloser(bytes.NewReader(newBody))
r.ContentLength = int64(len(newBody))
r.TransferEncoding = nil // Stop it being chunked, because that hangs
return nil
}
+10 -3
View File
@@ -24,9 +24,11 @@ const (
)
var (
containerCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/create$")
containerStartRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/(re)?start$")
execCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/exec$")
containerCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/create$")
containerStartRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/(re)?start$")
containerInspectRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/json$")
execCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/exec$")
execInspectRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/exec/[^/]*/json$")
ErrWeaveCIDRNone = errors.New("the container was created with the '-e WEAVE_CIDR=none' option")
ErrNoDefaultIPAM = errors.New("the container was created without specifying an IP address with '-e WEAVE_CIDR=...' and the proxy was started with the '--no-default-ipalloc' option")
@@ -36,6 +38,7 @@ type Config struct {
HostnameMatch string
HostnameReplacement string
ListenAddrs []string
RewriteInspect bool
NoDefaultIPAM bool
NoRewriteHosts bool
TLSConfig TLSConfig
@@ -123,8 +126,12 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
i = &createContainerInterceptor{proxy}
case containerStartRegexp.MatchString(path):
i = &startContainerInterceptor{proxy}
case containerInspectRegexp.MatchString(path):
i = &inspectContainerInterceptor{proxy}
case execCreateRegexp.MatchString(path):
i = &createExecInterceptor{proxy}
case execInspectRegexp.MatchString(path):
i = &inspectExecInterceptor{proxy}
default:
i = &nullInterceptor{}
}