Move go-socketaddr template parsing into config package to make it happen before creating a new agent. Also removed redundant parsetemplate calls from agent.go.

This commit is contained in:
Preetha Appan 2017-07-27 15:59:58 -05:00
parent 357c50c86e
commit 398c1e450c
3 changed files with 99 additions and 110 deletions

View File

@ -228,21 +228,9 @@ func New(c *Config) (*Agent, error) {
httpAddrs: httpAddrs,
tokens: new(token.Store),
}
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
@ -259,16 +247,7 @@ func New(c *Config) (*Agent, error) {
}
// 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 {
if a.config.AdvertiseAddrWan == "" {
a.config.AdvertiseAddrWan = a.config.AdvertiseAddr
}
@ -839,94 +818,6 @@ func parseSingleIPTemplate(ipTmpl string) (string, error) {
}
}
// resolveTmplAddrs iterates over the myriad of addresses in the agent's config
// and performs go-sockaddr/template Parse on each known address in case the
// user specified a template config for any of their values.
func (a *Agent) resolveTmplAddrs() error {
if a.config.AdvertiseAddr != "" {
ipStr, err := parseSingleIPTemplate(a.config.AdvertiseAddr)
if err != nil {
return fmt.Errorf("Advertise address resolution failed: %v", err)
}
a.config.AdvertiseAddr = ipStr
}
if a.config.Addresses.DNS != "" {
ipStr, err := parseSingleIPTemplate(a.config.Addresses.DNS)
if err != nil {
return fmt.Errorf("DNS address resolution failed: %v", err)
}
a.config.Addresses.DNS = ipStr
}
if a.config.Addresses.HTTP != "" {
ipStr, err := parseSingleIPTemplate(a.config.Addresses.HTTP)
if err != nil {
return fmt.Errorf("HTTP address resolution failed: %v", err)
}
a.config.Addresses.HTTP = ipStr
}
if a.config.Addresses.HTTPS != "" {
ipStr, err := parseSingleIPTemplate(a.config.Addresses.HTTPS)
if err != nil {
return fmt.Errorf("HTTPS address resolution failed: %v", err)
}
a.config.Addresses.HTTPS = ipStr
}
if a.config.AdvertiseAddrWan != "" {
ipStr, err := parseSingleIPTemplate(a.config.AdvertiseAddrWan)
if err != nil {
return fmt.Errorf("Advertise WAN address resolution failed: %v", err)
}
a.config.AdvertiseAddrWan = ipStr
}
if a.config.BindAddr != "" {
ipStr, err := parseSingleIPTemplate(a.config.BindAddr)
if err != nil {
return fmt.Errorf("Bind address resolution failed: %v", err)
}
a.config.BindAddr = ipStr
}
if a.config.ClientAddr != "" {
ipStr, err := parseSingleIPTemplate(a.config.ClientAddr)
if err != nil {
return fmt.Errorf("Client address resolution failed: %v", err)
}
a.config.ClientAddr = ipStr
}
if a.config.SerfLanBindAddr != "" {
ipStr, err := parseSingleIPTemplate(a.config.SerfLanBindAddr)
if err != nil {
return fmt.Errorf("Serf LAN Address resolution failed: %v", err)
}
a.config.SerfLanBindAddr = ipStr
}
if a.config.SerfWanBindAddr != "" {
ipStr, err := parseSingleIPTemplate(a.config.SerfWanBindAddr)
if err != nil {
return fmt.Errorf("Serf WAN Address resolution failed: %v", err)
}
a.config.SerfWanBindAddr = ipStr
}
// Parse all tagged addresses
for k, v := range a.config.TaggedAddresses {
ipStr, err := parseSingleIPTemplate(v)
if err != nil {
return fmt.Errorf("%s address resolution failed: %v", k, err)
}
a.config.TaggedAddresses[k] = ipStr
}
return nil
}
// makeRandomID will generate a random UUID for a node.
func (a *Agent) makeRandomID() (string, error) {
id, err := uuid.GenerateUUID()

View File

@ -2121,6 +2121,100 @@ func ReadConfigPaths(paths []string) (*Config, error) {
return result, nil
}
// ResolveTmplAddrs iterates over the myriad of addresses in the agent's config
// and performs go-sockaddr/template Parse on each known address in case the
// user specified a template config for any of their values.
func (c *Config) ResolveTmplAddrs() error {
if c.AdvertiseAddr != "" {
ipStr, err := parseSingleIPTemplate(c.AdvertiseAddr)
if err != nil {
return fmt.Errorf("Advertise address resolution failed: %v", err)
}
if net.ParseIP(ipStr) == nil {
return fmt.Errorf("Failed to parse advertise address: %v", ipStr)
}
c.AdvertiseAddr = ipStr
}
if c.Addresses.DNS != "" {
ipStr, err := parseSingleIPTemplate(c.Addresses.DNS)
if err != nil {
return fmt.Errorf("DNS address resolution failed: %v", err)
}
c.Addresses.DNS = ipStr
}
if c.Addresses.HTTP != "" {
ipStr, err := parseSingleIPTemplate(c.Addresses.HTTP)
if err != nil {
return fmt.Errorf("HTTP address resolution failed: %v", err)
}
c.Addresses.HTTP = ipStr
}
if c.Addresses.HTTPS != "" {
ipStr, err := parseSingleIPTemplate(c.Addresses.HTTPS)
if err != nil {
return fmt.Errorf("HTTPS address resolution failed: %v", err)
}
c.Addresses.HTTPS = ipStr
}
if c.AdvertiseAddrWan != "" {
ipStr, err := parseSingleIPTemplate(c.AdvertiseAddrWan)
if err != nil {
return fmt.Errorf("Advertise WAN address resolution failed: %v", err)
}
if net.ParseIP(ipStr) == nil {
return fmt.Errorf("Failed to parse Advertise WAN address: %v", ipStr)
}
c.AdvertiseAddrWan = ipStr
}
if c.BindAddr != "" {
ipStr, err := parseSingleIPTemplate(c.BindAddr)
if err != nil {
return fmt.Errorf("Bind address resolution failed: %v", err)
}
c.BindAddr = ipStr
}
if c.ClientAddr != "" {
ipStr, err := parseSingleIPTemplate(c.ClientAddr)
if err != nil {
return fmt.Errorf("Client address resolution failed: %v", err)
}
c.ClientAddr = ipStr
}
if c.SerfLanBindAddr != "" {
ipStr, err := parseSingleIPTemplate(c.SerfLanBindAddr)
if err != nil {
return fmt.Errorf("Serf LAN Address resolution failed: %v", err)
}
c.SerfLanBindAddr = ipStr
}
if c.SerfWanBindAddr != "" {
ipStr, err := parseSingleIPTemplate(c.SerfWanBindAddr)
if err != nil {
return fmt.Errorf("Serf WAN Address resolution failed: %v", err)
}
c.SerfWanBindAddr = ipStr
}
// Parse all tagged addresses
for k, v := range c.TaggedAddresses {
ipStr, err := parseSingleIPTemplate(v)
if err != nil {
return fmt.Errorf("%s address resolution failed: %v", k, err)
}
c.TaggedAddresses[k] = ipStr
}
return nil
}
// Implement the sort interface for dirEnts
func (d dirEnts) Len() int {
return len(d)

View File

@ -462,6 +462,10 @@ func (cmd *AgentCommand) readConfig() *agent.Config {
cfg.Version = cmd.Version
cfg.VersionPrerelease = cmd.VersionPrerelease
if err := cfg.ResolveTmplAddrs(); err != nil {
cmd.UI.Error(fmt.Sprintf("Failed to parse config: %v", err))
return nil
}
return cfg
}