From e86c5b2627981f40243ff04c074624e6296c344c Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 2 Sep 2014 12:47:40 -0700 Subject: [PATCH] agent: Address overrides. Fixes #301 and #253 --- command/agent/command.go | 10 ++++---- command/agent/config.go | 16 +++++++++---- command/agent/dns_test.go | 47 +++++++++++++++++++------------------- command/agent/http_test.go | 7 +++--- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/command/agent/command.go b/command/agent/command.go index 78837e06cdfd..8d4c0b0ba0b4 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -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 @@ -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 @@ -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 @@ -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)) } @@ -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)) } diff --git a/command/agent/config.go b/command/agent/config.go index ebe5805c82fb..f2d55cf66eaf 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -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 } diff --git a/command/agent/dns_test.go b/command/agent/dns_test.go index 0107cca72534..b28fc7db1deb 100644 --- a/command/agent/dns_test.go +++ b/command/agent/dns_test.go @@ -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) { @@ -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") @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 7d680536ac14..21e59b8f4d83 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -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" @@ -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) { @@ -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)