extract docker-bridge-ip fetching from proxy into bash script

This commit is contained in:
Paul Bellamy committed 2015-05-19 10:34:46 +01:00
1 parent 96a69d2303
commit bedc20c7c1
3 files changed
+25 -35

No files matched your search

+4 -29
View File
@@ -3,17 +3,16 @@ package proxy
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings"
"github.com/fsouza/go-dockerclient" "github.com/fsouza/go-dockerclient"
) )
type createContainerInterceptor struct { type createContainerInterceptor struct {
client *docker.Client client *docker.Client
withDNS bool withDNS bool
dockerBridgeIP string
} }
type createContainerRequestBody struct { type createContainerRequestBody struct {
@@ -97,11 +96,7 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque
return nil return nil
} }
dockerBridgeIP, err := getDockerBridgeIP() container.HostConfig.DNS = append(container.HostConfig.DNS, i.dockerBridgeIP)
if err != nil {
return err
}
container.HostConfig.DNS = append(container.HostConfig.DNS, dockerBridgeIP)
if len(container.HostConfig.DNSSearch) == 0 { if len(container.HostConfig.DNSSearch) == 0 {
container.HostConfig.DNSSearch = []string{"."} container.HostConfig.DNSSearch = []string{"."}
@@ -109,26 +104,6 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque
return nil 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) { func (i *createContainerInterceptor) InterceptResponse(res *http.Response) (*http.Response, error) {
return res, nil return res, nil
} }
+17 -6
View File
@@ -12,9 +12,10 @@ import (
) )
type Proxy struct { type Proxy struct {
Dial func() (net.Conn, error) Dial func() (net.Conn, error)
client *docker.Client client *docker.Client
withDNS bool withDNS bool
dockerBridgeIP string
} }
func NewProxy(targetURL string, withDNS bool) (*Proxy, error) { func NewProxy(targetURL string, withDNS bool) (*Proxy, error) {
@@ -22,6 +23,15 @@ func NewProxy(targetURL string, withDNS bool) (*Proxy, error) {
if err != nil { if err != nil {
return nil, err 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) client, err := docker.NewClient(targetURL)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -30,8 +40,9 @@ func NewProxy(targetURL string, withDNS bool) (*Proxy, error) {
Dial: func() (net.Conn, error) { Dial: func() (net.Conn, error) {
return net.Dial(targetNetwork(u), targetAddress(u)) return net.Dial(targetNetwork(u), targetAddress(u))
}, },
client: client, client: client,
withDNS: withDNS, withDNS: withDNS,
dockerBridgeIP: string(dockerBridgeIP),
}, nil }, 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) { 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) { func (proxy *Proxy) startContainer(w http.ResponseWriter, r *http.Request) {
+4
View File
@@ -921,6 +921,10 @@ case "$COMMAND" in
dns_args "$@" dns_args "$@"
echo -n $DNS_ARGS echo -n $DNS_ARGS
;; ;;
docker-bridge-ip)
docker_bridge_ip
echo -n $DOCKER_BRIDGE_IP
;;
start) start)
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_COUNT shift $CIDR_COUNT