diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index 59c7e1eb..12d04df9 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -3,17 +3,16 @@ package proxy import ( "bytes" "encoding/json" - "fmt" "io/ioutil" "net/http" - "strings" "github.com/fsouza/go-dockerclient" ) type createContainerInterceptor struct { - client *docker.Client - withDNS bool + client *docker.Client + withDNS bool + dockerBridgeIP string } type createContainerRequestBody struct { @@ -97,11 +96,7 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque return nil } - dockerBridgeIP, err := getDockerBridgeIP() - if err != nil { - return err - } - container.HostConfig.DNS = append(container.HostConfig.DNS, dockerBridgeIP) + container.HostConfig.DNS = append(container.HostConfig.DNS, i.dockerBridgeIP) if len(container.HostConfig.DNSSearch) == 0 { container.HostConfig.DNSSearch = []string{"."} @@ -109,26 +104,6 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque return nil } -func getDockerBridgeIP() (string, error) { - out, err := callWeave("dns-args") - if err != nil { - return "", fmt.Errorf("Error fetching weave dns-args: %s", err) - } - - var dockerBridgeIP string - segments := strings.Split(string(out), " ") - for i := 0; i < len(segments)-1; i++ { - if segments[i] == "--dns" { - dockerBridgeIP = segments[i+1] - } - } - - if dockerBridgeIP == "" { - return "", fmt.Errorf("Docker bridge IP not found in weave output: %s", string(out)) - } - return dockerBridgeIP, nil -} - func (i *createContainerInterceptor) InterceptResponse(res *http.Response) (*http.Response, error) { return res, nil } diff --git a/proxy/proxy.go b/proxy/proxy.go index 03b1fa99..6bce75f2 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -12,9 +12,10 @@ import ( ) type Proxy struct { - Dial func() (net.Conn, error) - client *docker.Client - withDNS bool + Dial func() (net.Conn, error) + client *docker.Client + withDNS bool + dockerBridgeIP string } func NewProxy(targetURL string, withDNS bool) (*Proxy, error) { @@ -22,6 +23,15 @@ func NewProxy(targetURL string, withDNS bool) (*Proxy, error) { if err != nil { return nil, err } + + var dockerBridgeIP []byte + if withDNS { + dockerBridgeIP, err = callWeave("docker-bridge-ip") + if err != nil { + return nil, err + } + } + client, err := docker.NewClient(targetURL) if err != nil { return nil, err @@ -30,8 +40,9 @@ func NewProxy(targetURL string, withDNS bool) (*Proxy, error) { Dial: func() (net.Conn, error) { return net.Dial(targetNetwork(u), targetAddress(u)) }, - client: client, - withDNS: withDNS, + client: client, + withDNS: withDNS, + dockerBridgeIP: string(dockerBridgeIP), }, nil } @@ -82,7 +93,7 @@ func (proxy *Proxy) weaveStatus(w http.ResponseWriter, r *http.Request) { } func (proxy *Proxy) createContainer(w http.ResponseWriter, r *http.Request) { - newClient(proxy.Dial, &createContainerInterceptor{proxy.client, proxy.withDNS}).ServeHTTP(w, r) + newClient(proxy.Dial, &createContainerInterceptor{proxy.client, proxy.withDNS, proxy.dockerBridgeIP}).ServeHTTP(w, r) } func (proxy *Proxy) startContainer(w http.ResponseWriter, r *http.Request) { diff --git a/weave b/weave index 2c16c4b8..d8e0554b 100755 --- a/weave +++ b/weave @@ -921,6 +921,10 @@ case "$COMMAND" in dns_args "$@" echo -n $DNS_ARGS ;; + docker-bridge-ip) + docker_bridge_ip + echo -n $DOCKER_BRIDGE_IP + ;; start) collect_cidr_args "$@" shift $CIDR_COUNT