From acbd744372f5524e2bf726eb18cf6c8a9a8c5062 Mon Sep 17 00:00:00 2001 From: Alex Kataev Date: Wed, 15 Jul 2020 16:34:28 +0500 Subject: [PATCH] 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 {