From 5fd013dd82e7c2aaaa6a25c6d9330bfabef3ca26 Mon Sep 17 00:00:00 2001 From: Brad Peters Date: Thu, 22 Sep 2016 12:50:44 -0300 Subject: [PATCH] Parses consul address, if address is ipv6 adds [] --- consul/cache.go | 17 ++++++++++++++++- consul/consul.go | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/consul/cache.go b/consul/cache.go index c14b3a5..3931aae 100644 --- a/consul/cache.go +++ b/consul/cache.go @@ -2,6 +2,7 @@ package consul import ( "fmt" + "net" "strings" "github.com/CiscoCloud/mesos-consul/registry" @@ -17,8 +18,22 @@ type cacheEntry struct { } func newCacheEntry(service *consulapi.AgentServiceRegistration, agent string) *cacheEntry { + + // test if address is an ip + agentAddress := agent + ip := net.ParseIP(agentAddress) + if ip != nil { + ipv4 := ip.To4() + log.Debugf("agentAddress is an ip address %s", agentAddress) + // if not an ipv4 address assume ipv6 and add [ ] to address + if ipv4 == nil { + agentAddress = fmt.Sprintf("[%s]", agentAddress) + log.Debugf("agentAddress is ipv6 address %s", agentAddress) + } + } + return &cacheEntry{ - agent: agent, + agent: agentAddress, service: service, validityCounter: 0, } diff --git a/consul/consul.go b/consul/consul.go index d4feb06..2b05d10 100644 --- a/consul/consul.go +++ b/consul/consul.go @@ -3,6 +3,7 @@ package consul import ( "crypto/tls" "fmt" + "net" "net/http" "time" @@ -51,8 +52,21 @@ func (c *Consul) newAgent(address string) *consulapi.Client { } config := consulapi.DefaultConfig() + agentAddress := address + + // test if address is an ip + ip := net.ParseIP(agentAddress) + if ip != nil { + ipv4 := ip.To4() + log.Debugf("agentAddress is an ip address %s", agentAddress) + // if not an ipv4 address assume ipv6 and add [ ] to address + if ipv4 == nil { + agentAddress = fmt.Sprintf("[%s]", agentAddress) + log.Debugf("agentAddress is ipv6 address %s", agentAddress) + } + } - config.Address = fmt.Sprintf("%s:%s", address, c.config.port) + config.Address = fmt.Sprintf("%s:%s", agentAddress, c.config.port) log.Debugf("consul address: %s", config.Address) config.HttpClient.Timeout = time.Duration(c.config.timeout) * time.Second