From 99d9ad704275ef1bde76006da92ebf93ce30550b Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Wed, 10 Jun 2015 10:25:34 +0100 Subject: [PATCH] [proxy] query weavedns for domain --- proxy/create_container_interceptor.go | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index cedf2ef0..616d4d93 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -3,6 +3,7 @@ package proxy import ( "bytes" "encoding/json" + "fmt" "io/ioutil" "net/http" "strings" @@ -93,13 +94,36 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque name := r.URL.Query().Get("name") if container.Hostname == "" && name != "" { container.Hostname = name - // TODO: Get weaveDNS domain - container.Domainname = nameserver.DefaultLocalDomain + container.Domainname = i.getDNSDomain() } return nil } +func (i *createContainerInterceptor) getDNSDomain() (domain string) { + domain = nameserver.DefaultLocalDomain + dnsContainer, err := i.proxy.client.InspectContainer("weavedns") + if err != nil || + dnsContainer.NetworkSettings == nil || + dnsContainer.NetworkSettings.IPAddress == "" { + return + } + + url := fmt.Sprintf("http://%s:6785/domain", dnsContainer.NetworkSettings.IPAddress) + resp, err := http.Get(url) + if err != nil || resp.StatusCode != http.StatusOK { + return + } + defer resp.Body.Close() + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return + } + + return string(b) +} + func (i *createContainerInterceptor) InterceptResponse(r *http.Response) error { return nil }