Add dns support for the proxy

In the proxy, I swapped the 'flag' library for
'code.google.com/p/getopt', which uses double-dash flags.

proxy lets 'weave attach' do dns registration on container start

proxy needs to set DNSSearch if not specified
This commit is contained in:
Paul Bellamy committed 2015-05-19 10:34:45 +01:00
1 parent 444b1482fa
commit 96a69d2303
11 files changed
+229 -102

No files matched your search

+15 -34
View File
@@ -2,7 +2,6 @@ package proxy
import (
"net/http"
"os/exec"
"regexp"
"strings"
@@ -11,13 +10,8 @@ import (
)
type startContainerInterceptor struct {
client *docker.Client
}
func StartContainerInterceptor(client *docker.Client) Interceptor {
return &startContainerInterceptor{
client: client,
}
client *docker.Client
withDNS bool
}
func (i *startContainerInterceptor) InterceptRequest(r *http.Request) (*http.Request, error) {
@@ -25,21 +19,19 @@ func (i *startContainerInterceptor) InterceptRequest(r *http.Request) (*http.Req
}
func (i *startContainerInterceptor) InterceptResponse(res *http.Response) (*http.Response, error) {
containerId := containerFromPath(res.Request.URL.Path)
containerID := containerFromPath(res.Request.URL.Path)
container, err := i.client.InspectContainer(containerId)
container, err := i.client.InspectContainer(containerID)
if err != nil {
Warning.Print("Error Inspecting Container: ", err)
return res, nil
}
if cidr, ok := weaveAddrFromConfig(container.Config); ok {
Info.Printf("Container %s was started with CIDR \"%s\"", containerId, cidr)
args := []string{"attach"}
args = append(args, strings.Split(strings.TrimSpace(cidr), " ")...)
args = append(args, containerId)
if out, err := callWeave(args...); err != nil {
Warning.Print("Calling weave failed: ", err, string(out))
if cidrs, ok := weaveCIDRsFromConfig(container.Config); ok {
Info.Printf("Container %s was started with CIDR \"%s\"", containerID, strings.Join(cidrs, " "))
if err := i.attachContainerToWeave(containerID, cidrs); err != nil {
Warning.Print("Attaching container to weave failed: ", err)
return res, nil
}
} else {
Debug.Print("No Weave CIDR, ignoring")
@@ -55,21 +47,10 @@ func containerFromPath(path string) string {
return ""
}
func callWeave(args ...string) ([]byte, error) {
args = append([]string{"--local"}, args...)
Debug.Print("Calling weave", args)
cmd := exec.Command("./weave", args...)
cmd.Env = []string{"PROCFS=/hostproc", "PATH=/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
out, err := cmd.CombinedOutput()
return out, err
}
func weaveAddrFromConfig(config *docker.Config) (string, bool) {
for _, e := range config.Env {
if strings.HasPrefix(e, "WEAVE_CIDR=") {
result := strings.Trim(e[11:], " ")
return result, result != ""
}
}
return "", false
func (i *startContainerInterceptor) attachContainerToWeave(containerID string, cidrs []string) error {
args := []string{"attach"}
args = append(args, cidrs...)
args = append(args, containerID)
_, err := callWeave(args...)
return err
}