From fa5e032e495bd8749fe252a6ec7f0739aba70dd8 Mon Sep 17 00:00:00 2001 From: Konstantin Suvorov Date: Tue, 15 Aug 2017 20:25:48 +0300 Subject: [PATCH 1/3] Register container name and its network aliases with weaveDNS --- plugin/net/watcher.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugin/net/watcher.go b/plugin/net/watcher.go index b763d1a6..f0c91fdb 100644 --- a/plugin/net/watcher.go +++ b/plugin/net/watcher.go @@ -39,8 +39,16 @@ func (w *watcher) ContainerStarted(id string) { if network.isOurs { if w.driver.dns { fqdn := fmt.Sprintf("%s.%s", info.Config.Hostname, info.Config.Domainname) - if err := w.weave.RegisterWithDNS(id, fqdn, net.IPAddress); err != nil { - w.driver.warn("ContainerStarted", "unable to register %s with weaveDNS: %s", id, err) + weaveDomain, _ := w.weave.DNSDomain() + // Container name is prefixed with '/' so we cut first character + name := fmt.Sprintf("%s.%s", info.Name[1:], weaveDomain) + aliases := append([]string(nil), fqdn, name) + aliases = append(aliases, net.Aliases...) + for _, alias := range aliases { + w.driver.debug("ContainerStarted", "going to register %s with weaveDNS", alias) + if err := w.weave.RegisterWithDNS(id, alias, net.IPAddress); err != nil { + w.driver.warn("ContainerStarted", "unable to register %s with weaveDNS: %s", id, err) + } } } netNSPath := weavenet.NSPathByPidWithProc(w.driver.procPath, info.State.Pid) From acbd744372f5524e2bf726eb18cf6c8a9a8c5062 Mon Sep 17 00:00:00 2001 From: Alex Kataev Date: Wed, 15 Jul 2020 16:34:28 +0500 Subject: [PATCH 2/3] Refactor: additional DNS names registration --- plugin/net/watcher.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/plugin/net/watcher.go b/plugin/net/watcher.go index f0c91fdb..c78cb7e1 100644 --- a/plugin/net/watcher.go +++ b/plugin/net/watcher.go @@ -29,6 +29,12 @@ func (w *watcher) ContainerStarted(id string) { w.driver.warn("ContainerStarted", "error inspecting container %s: %s", id, err) return } + + domain, err := w.weave.DNSDomain() + if err != nil { + w.driver.warn("ContainerStarted", "unable to get weave dns domain: %s", err) + } + // check that it's on our network for _, net := range info.NetworkSettings.Networks { network, err := w.driver.findNetworkInfo(net.NetworkID) @@ -39,11 +45,17 @@ func (w *watcher) ContainerStarted(id string) { if network.isOurs { if w.driver.dns { fqdn := fmt.Sprintf("%s.%s", info.Config.Hostname, info.Config.Domainname) - weaveDomain, _ := w.weave.DNSDomain() - // Container name is prefixed with '/' so we cut first character - name := fmt.Sprintf("%s.%s", info.Name[1:], weaveDomain) - aliases := append([]string(nil), fqdn, name) + + aliases := make([]string, 0, len(net.Aliases)+2) + aliases = append(aliases, fqdn) + + if len(domain) > 0 && len(info.Name) > 1 && info.Name[0] == "/" { + name := fmt.Sprintf("%s.%s", info.Name[1:], domain) + aliases = append(aliases, name) + } + aliases = append(aliases, net.Aliases...) + for _, alias := range aliases { w.driver.debug("ContainerStarted", "going to register %s with weaveDNS", alias) if err := w.weave.RegisterWithDNS(id, alias, net.IPAddress); err != nil { From 0f276a4d63abfc8387a99f89242017731bb86e60 Mon Sep 17 00:00:00 2001 From: Alex Kataev Date: Fri, 17 Jul 2020 10:45:53 +0500 Subject: [PATCH 3/3] Fixed mismatched types in the condition --- plugin/net/watcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/net/watcher.go b/plugin/net/watcher.go index c78cb7e1..f524fd79 100644 --- a/plugin/net/watcher.go +++ b/plugin/net/watcher.go @@ -49,7 +49,7 @@ func (w *watcher) ContainerStarted(id string) { aliases := make([]string, 0, len(net.Aliases)+2) aliases = append(aliases, fqdn) - if len(domain) > 0 && len(info.Name) > 1 && info.Name[0] == "/" { + if len(domain) > 0 && len(info.Name) > 1 && info.Name[0] == '/' { name := fmt.Sprintf("%s.%s", info.Name[1:], domain) aliases = append(aliases, name) }