Skip to content

Commit

Permalink
agent: Address overrides. Fixes #301 and #253
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Sep 2, 2014
1 parent cc728a1 commit e86c5b2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
10 changes: 5 additions & 5 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log
c.agent = agent

// Setup the RPC listener
rpcAddr, err := config.ClientListener(config.Ports.RPC)
rpcAddr, err := config.ClientListener(config.Addresses.RPC, config.Ports.RPC)
if err != nil {
c.Ui.Error(fmt.Sprintf("Invalid RPC bind address: %s", err))
return err
Expand All @@ -261,7 +261,7 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log
c.rpcServer = NewAgentRPC(agent, rpcListener, logOutput, logWriter)

if config.Ports.HTTP > 0 {
httpAddr, err := config.ClientListener(config.Ports.HTTP)
httpAddr, err := config.ClientListener(config.Addresses.HTTP, config.Ports.HTTP)
if err != nil {
c.Ui.Error(fmt.Sprintf("Invalid HTTP bind address: %s", err))
return err
Expand All @@ -277,7 +277,7 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log
}

if config.Ports.DNS > 0 {
dnsAddr, err := config.ClientListener(config.Ports.DNS)
dnsAddr, err := config.ClientListener(config.Addresses.DNS, config.Ports.DNS)
if err != nil {
c.Ui.Error(fmt.Sprintf("Invalid DNS bind address: %s", err))
return err
Expand Down Expand Up @@ -416,7 +416,7 @@ func (c *Command) Run(args []string) int {
}

// Get the new client listener addr
httpAddr, err := config.ClientListenerAddr(config.Ports.HTTP)
httpAddr, err := config.ClientListenerAddr(config.Addresses.HTTP, config.Ports.HTTP)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to determine HTTP address: %v", err))
}
Expand Down Expand Up @@ -574,7 +574,7 @@ func (c *Command) handleReload(config *Config) *Config {
}

// Get the new client listener addr
httpAddr, err := newConf.ClientListenerAddr(config.Ports.HTTP)
httpAddr, err := newConf.ClientListenerAddr(config.Addresses.HTTP, config.Ports.HTTP)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to determine HTTP address: %v", err))
}
Expand Down
16 changes: 11 additions & 5 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,24 @@ func (c *Config) EncryptBytes() ([]byte, error) {

// ClientListener is used to format a listener for a
// port on a ClientAddr
func (c *Config) ClientListener(port int) (*net.TCPAddr, error) {
ip := net.ParseIP(c.ClientAddr)
func (c *Config) ClientListener(override string, port int) (*net.TCPAddr, error) {
var addr string
if override != "" {
addr = override
} else {
addr = c.ClientAddr
}
ip := net.ParseIP(addr)
if ip == nil {
return nil, fmt.Errorf("Failed to parse IP: %v", c.ClientAddr)
return nil, fmt.Errorf("Failed to parse IP: %v", addr)
}
return &net.TCPAddr{IP: ip, Port: port}, nil
}

// ClientListenerAddr is used to format an address for a
// port on a ClientAddr, handling the zero IP.
func (c *Config) ClientListenerAddr(port int) (string, error) {
addr, err := c.ClientListener(port)
func (c *Config) ClientListenerAddr(override string, port int) (string, error) {
addr, err := c.ClientListener(override, port)
if err != nil {
return "", err
}
Expand Down
47 changes: 24 additions & 23 deletions command/agent/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package agent

import (
"fmt"
"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/testutil"
"github.com/miekg/dns"
"os"
"strings"
"testing"
"time"

"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/testutil"
"github.com/miekg/dns"
)

func makeDNSServer(t *testing.T) (string, *DNSServer) {
Expand All @@ -18,7 +19,7 @@ func makeDNSServer(t *testing.T) (string, *DNSServer) {

func makeDNSServerConfig(t *testing.T, config *DNSConfig) (string, *DNSServer) {
conf := nextConfig()
addr, _ := conf.ClientListener(conf.Ports.DNS)
addr, _ := conf.ClientListener(conf.Addresses.DNS, conf.Ports.DNS)
dir, agent := makeAgent(t, conf)
server, err := NewDNSServer(agent, config, agent.logOutput,
conf.Domain, addr.String(), "8.8.8.8:53")
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestDNS_IsAlive(t *testing.T) {
m.SetQuestion("_test.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestDNS_NodeLookup(t *testing.T) {
m.SetQuestion("foo.node.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -159,7 +160,7 @@ func TestDNS_CaseInsensitiveNodeLookup(t *testing.T) {
m.SetQuestion("fOO.node.dc1.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -193,7 +194,7 @@ func TestDNS_NodeLookup_PeriodName(t *testing.T) {
m.SetQuestion("foo.bar.node.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -235,7 +236,7 @@ func TestDNS_NodeLookup_AAAA(t *testing.T) {
m.SetQuestion("bar.node.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -280,7 +281,7 @@ func TestDNS_NodeLookup_CNAME(t *testing.T) {
m.SetQuestion("google.node.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -331,7 +332,7 @@ func TestDNS_ServiceLookup(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeSRV)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -398,7 +399,7 @@ func TestDNS_CaseInsensitiveServiceLookup(t *testing.T) {
m.SetQuestion("mASTER.dB.service.consul.", dns.TypeSRV)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -437,7 +438,7 @@ func TestDNS_ServiceLookup_TagPeriod(t *testing.T) {
m.SetQuestion("v1.master.db.service.consul.", dns.TypeSRV)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -528,7 +529,7 @@ func TestDNS_ServiceLookup_Dedup(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -605,7 +606,7 @@ func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeSRV)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -662,7 +663,7 @@ func TestDNS_Recurse(t *testing.T) {

c := new(dns.Client)
c.Net = "tcp"
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -762,7 +763,7 @@ func TestDNS_ServiceLookup_FilterCritical(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -807,7 +808,7 @@ func TestDNS_ServiceLookup_Randomize(t *testing.T) {

// Ensure the response is randomized each time
uniques := map[string]struct{}{}
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
for i := 0; i < 5; i++ {
m := new(dns.Msg)
m.SetQuestion("web.service.consul.", dns.TypeANY)
Expand Down Expand Up @@ -871,7 +872,7 @@ func TestDNS_ServiceLookup_CNAME(t *testing.T) {
m.SetQuestion("search.service.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -927,7 +928,7 @@ func TestDNS_NodeLookup_TTL(t *testing.T) {
m.SetQuestion("foo.node.consul.", dns.TypeANY)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -1067,7 +1068,7 @@ func TestDNS_ServiceLookup_TTL(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeSRV)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -1149,7 +1150,7 @@ func TestDNS_ServiceLookup_SRV_RFC(t *testing.T) {
m.SetQuestion("_db._master.service.consul.", dns.TypeSRV)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -1216,7 +1217,7 @@ func TestDNS_ServiceLookup_SRV_RFC_TCP_Default(t *testing.T) {
m.SetQuestion("_db._tcp.service.consul.", dns.TypeSRV)

c := new(dns.Client)
addr, _ := srv.agent.config.ClientListener(srv.agent.config.Ports.DNS)
addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down
7 changes: 4 additions & 3 deletions command/agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/testutil"
"io"
"io/ioutil"
"net/http"
Expand All @@ -15,6 +13,9 @@ import (
"strconv"
"testing"
"time"

"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/testutil"
)

func makeHTTPServer(t *testing.T) (string, *HTTPServer) {
Expand All @@ -24,7 +25,7 @@ func makeHTTPServer(t *testing.T) (string, *HTTPServer) {
if err := os.Mkdir(uiDir, 755); err != nil {
t.Fatalf("err: %v", err)
}
addr, _ := agent.config.ClientListener(agent.config.Ports.HTTP)
addr, _ := agent.config.ClientListener("", agent.config.Ports.HTTP)
server, err := NewHTTPServer(agent, uiDir, true, agent.logOutput, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
Expand Down

0 comments on commit e86c5b2

Please sign in to comment.