Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Parses consul address, if address is ipv6 adds [] #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion consul/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consul

import (
"fmt"
"net"
"strings"

"github.com/CiscoCloud/mesos-consul/registry"
Expand All @@ -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,
}
Expand Down
19 changes: 16 additions & 3 deletions consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"time"

"github.com/CiscoCloud/mesos-consul/registry"
"github.com/mantl/mesos-consul/registry"

consulapi "github.com/hashicorp/consul/api"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -53,8 +53,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
Expand Down