From 46221d2b565c4a2eb52fb85320deb45f922e25d1 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Fri, 30 Jun 2017 23:56:05 +0200 Subject: [PATCH] agent: do not modify agent config after NewAgent --- agent/agent.go | 92 ++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 54f96082e..6001a4ee3 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -221,6 +221,53 @@ func New(c *Config) (*Agent, error) { if err := a.resolveTmplAddrs(); err != nil { return nil, err } + + // Try to get an advertise address + switch { + case a.config.AdvertiseAddr != "": + ipStr, err := parseSingleIPTemplate(a.config.AdvertiseAddr) + if err != nil { + return nil, fmt.Errorf("Advertise address resolution failed: %v", err) + } + if net.ParseIP(ipStr) == nil { + return nil, fmt.Errorf("Failed to parse advertise address: %v", ipStr) + } + a.config.AdvertiseAddr = ipStr + + case a.config.BindAddr != "" && !ipaddr.IsAny(a.config.BindAddr): + a.config.AdvertiseAddr = a.config.BindAddr + + default: + ip, err := consul.GetPrivateIP() + if ipaddr.IsAnyV6(a.config.BindAddr) { + ip, err = consul.GetPublicIPv6() + } + if err != nil { + return nil, fmt.Errorf("Failed to get advertise address: %v", err) + } + a.config.AdvertiseAddr = ip.String() + } + + // Try to get an advertise address for the wan + if a.config.AdvertiseAddrWan != "" { + ipStr, err := parseSingleIPTemplate(a.config.AdvertiseAddrWan) + if err != nil { + return nil, fmt.Errorf("Advertise WAN address resolution failed: %v", err) + } + if net.ParseIP(ipStr) == nil { + return nil, fmt.Errorf("Failed to parse advertise address for WAN: %v", ipStr) + } + a.config.AdvertiseAddrWan = ipStr + } else { + a.config.AdvertiseAddrWan = a.config.AdvertiseAddr + } + + // Create the default set of tagged addresses. + a.config.TaggedAddresses = map[string]string{ + "lan": a.config.AdvertiseAddr, + "wan": a.config.AdvertiseAddrWan, + } + return a, nil } @@ -607,51 +654,6 @@ func (a *Agent) consulConfig() (*consul.Config, error) { if a.config.SerfWanBindAddr != "" { base.SerfWANConfig.MemberlistConfig.BindAddr = a.config.SerfWanBindAddr } - // Try to get an advertise address - switch { - case a.config.AdvertiseAddr != "": - ipStr, err := parseSingleIPTemplate(a.config.AdvertiseAddr) - if err != nil { - return nil, fmt.Errorf("Advertise address resolution failed: %v", err) - } - if net.ParseIP(ipStr) == nil { - return nil, fmt.Errorf("Failed to parse advertise address: %v", ipStr) - } - a.config.AdvertiseAddr = ipStr - - case a.config.BindAddr != "" && !ipaddr.IsAny(a.config.BindAddr): - a.config.AdvertiseAddr = a.config.BindAddr - - default: - ip, err := consul.GetPrivateIP() - if ipaddr.IsAnyV6(a.config.BindAddr) { - ip, err = consul.GetPublicIPv6() - } - if err != nil { - return nil, fmt.Errorf("Failed to get advertise address: %v", err) - } - a.config.AdvertiseAddr = ip.String() - } - - // Try to get an advertise address for the wan - if a.config.AdvertiseAddrWan != "" { - ipStr, err := parseSingleIPTemplate(a.config.AdvertiseAddrWan) - if err != nil { - return nil, fmt.Errorf("Advertise WAN address resolution failed: %v", err) - } - if net.ParseIP(ipStr) == nil { - return nil, fmt.Errorf("Failed to parse advertise address for WAN: %v", ipStr) - } - a.config.AdvertiseAddrWan = ipStr - } else { - a.config.AdvertiseAddrWan = a.config.AdvertiseAddr - } - - // Create the default set of tagged addresses. - a.config.TaggedAddresses = map[string]string{ - "lan": a.config.AdvertiseAddr, - "wan": a.config.AdvertiseAddrWan, - } if a.config.AdvertiseAddr != "" { base.SerfLANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr