agent: Simplifying configs

pull/36/head
Armon Dadgar 2014-04-11 15:46:55 -07:00
parent 922014530f
commit 8dd1e51bae
8 changed files with 90 additions and 77 deletions

View File

@ -67,16 +67,13 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
return nil, fmt.Errorf("Must configure a DataDir")
}
// Ensure the Bind Addr is sane
if _, err := net.ResolveTCPAddr("tcp", config.BindAddr); err != nil {
return nil, fmt.Errorf("Bad server address: %v", err)
}
// Try to get an advertise address
if config.AdvertiseAddr != "" {
if ip := net.ParseIP(config.AdvertiseAddr); ip == nil {
return nil, fmt.Errorf("Failed to parse advertise address: %v", config.AdvertiseAddr)
}
} else if config.BindAddr != "0.0.0.0" && config.BindAddr != "" {
config.AdvertiseAddr = config.BindAddr
} else {
ip, err := consul.GetPrivateIP()
if err != nil {
@ -159,7 +156,7 @@ func (a *Agent) consulConfig() *consul.Config {
base.SerfWANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr
base.RPCAdvertise = &net.TCPAddr{
IP: net.ParseIP(a.config.AdvertiseAddr),
Port: base.RPCAddr.Port,
Port: a.config.Ports.RPC,
}
}
if a.config.Bootstrap {

View File

@ -84,7 +84,7 @@ func TestHTTPAgentMembers(t *testing.T) {
t.Fatalf("bad members: %v", obj)
}
if int(val[0].Port) != srv.agent.config.SerfLanPort {
if int(val[0].Port) != srv.agent.config.Ports.SerfLan {
t.Fatalf("not lan: %v", obj)
}
}
@ -109,7 +109,7 @@ func TestHTTPAgentMembers_WAN(t *testing.T) {
t.Fatalf("bad members: %v", obj)
}
if int(val[0].Port) != srv.agent.config.SerfWanPort {
if int(val[0].Port) != srv.agent.config.Ports.SerfWan {
t.Fatalf("not wan: %v", obj)
}
}
@ -124,7 +124,7 @@ func TestHTTPAgentJoin(t *testing.T) {
defer os.RemoveAll(dir2)
defer a2.Shutdown()
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.SerfLanPort)
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan)
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s", addr), nil)
if err != nil {
t.Fatalf("err: %v", err)
@ -153,7 +153,7 @@ func TestHTTPAgentJoin_WAN(t *testing.T) {
defer os.RemoveAll(dir2)
defer a2.Shutdown()
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.SerfWanPort)
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfWan)
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/agent/join/%s?wan=true", addr), nil)
if err != nil {
t.Fatalf("err: %v", err)
@ -183,7 +183,7 @@ func TestHTTPAgentForceLeave(t *testing.T) {
defer a2.Shutdown()
// Join first
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.SerfLanPort)
addr := fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan)
_, err := srv.agent.JoinLAN([]string{addr})
if err != nil {
t.Fatalf("err: %v", err)

View File

@ -15,21 +15,21 @@ import (
var offset uint64
func nextConfig() *Config {
idx := atomic.AddUint64(&offset, 1)
idx := int(atomic.AddUint64(&offset, 1))
conf := DefaultConfig()
conf.AdvertiseAddr = "127.0.0.1"
conf.Bootstrap = true
conf.Datacenter = "dc1"
conf.NodeName = fmt.Sprintf("Node %d", idx)
conf.DNSAddr = fmt.Sprintf("127.0.0.1:%d", 18600+idx)
conf.HTTPAddr = fmt.Sprintf("127.0.0.1:%d", 18500+idx)
conf.RPCAddr = fmt.Sprintf("127.0.0.1:%d", 18400+idx)
conf.SerfBindAddr = "127.0.0.1"
conf.SerfLanPort = int(18200 + idx)
conf.SerfWanPort = int(18300 + idx)
conf.BindAddr = "127.0.0.1"
conf.Ports.DNS = 18600 + idx
conf.Ports.HTTP = 18500 + idx
conf.Ports.RPC = 18400 + idx
conf.Ports.SerfLan = 18200 + idx
conf.Ports.SerfWan = 18300 + idx
conf.Ports.Server = 18100 + idx
conf.Server = true
conf.ServerAddr = fmt.Sprintf("127.0.0.1:%d", 18100+idx)
cons := consul.DefaultConfig()
conf.ConsulConfig = cons

View File

@ -23,7 +23,7 @@ type PortConfig struct {
HTTP int // HTTP API
RPC int // CLI RPC
SerfLan int `mapstructure:"serf_lan"` // LAN gossip (Client + Server)
SerfWan int `mapstructure:"serv_wan"` // WAN gossip (Server onlyg)
SerfWan int `mapstructure:"serf_wan"` // WAN gossip (Server onlyg)
Server int // Server internal RPC
}
@ -145,6 +145,7 @@ func DefaultConfig() *Config {
Domain: "consul.",
LogLevel: "INFO",
ClientAddr: "127.0.0.1",
BindAddr: "0.0.0.0",
Ports: PortConfig{
DNS: 8600,
HTTP: 8500,

View File

@ -94,13 +94,13 @@ func TestDecodeConfig(t *testing.T) {
}
// DNS setup
input = `{"dns_addr": "127.0.0.1:8500", "recursor": "8.8.8.8", "domain": "foobar"}`
input = `{"ports": {"dns": 8500}, "recursor": "8.8.8.8", "domain": "foobar"}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.DNSAddr != "127.0.0.1:8500" {
if config.Ports.DNS != 8500 {
t.Fatalf("bad: %#v", config)
}
@ -113,51 +113,55 @@ func TestDecodeConfig(t *testing.T) {
}
// RPC configs
input = `{"http_addr": "127.0.0.1:1234", "rpc_addr": "127.0.0.1:8100"}`
input = `{"ports": {"http": 1234, "rpc": 8100}, "client_addr": "0.0.0.0"}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.HTTPAddr != "127.0.0.1:1234" {
if config.ClientAddr != "0.0.0.0" {
t.Fatalf("bad: %#v", config)
}
if config.RPCAddr != "127.0.0.1:8100" {
if config.Ports.HTTP != 1234 {
t.Fatalf("bad: %#v", config)
}
if config.Ports.RPC != 8100 {
t.Fatalf("bad: %#v", config)
}
// Serf configs
input = `{"serf_bind_addr": "127.0.0.2", "serf_lan_port": 1000, "serf_wan_port": 2000}`
input = `{"ports": {"serf_lan": 1000, "serf_wan": 2000}}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.SerfBindAddr != "127.0.0.2" {
if config.Ports.SerfLan != 1000 {
t.Fatalf("bad: %#v", config)
}
if config.SerfLanPort != 1000 {
t.Fatalf("bad: %#v", config)
}
if config.SerfWanPort != 2000 {
if config.Ports.SerfWan != 2000 {
t.Fatalf("bad: %#v", config)
}
// Server addrs
input = `{"server_addr": "127.0.0.1:8000", "advertise_addr": "127.0.0.1:8000"}`
input = `{"ports": {"server": 8000}, "bind_addr": "127.0.0.2", "advertise_addr": "127.0.0.3"}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.ServerAddr != "127.0.0.1:8000" {
if config.BindAddr != "127.0.0.2" {
t.Fatalf("bad: %#v", config)
}
if config.AdvertiseAddr != "127.0.0.1:8000" {
if config.AdvertiseAddr != "127.0.0.3" {
t.Fatalf("bad: %#v", config)
}
if config.Ports.Server != 8000 {
t.Fatalf("bad: %#v", config)
}
@ -312,18 +316,13 @@ func TestMergeConfig(t *testing.T) {
Bootstrap: false,
Datacenter: "dc1",
DataDir: "/tmp/foo",
DNSAddr: "127.0.0.1:1000",
DNSRecursor: "127.0.0.1:1001",
Domain: "basic",
HTTPAddr: "",
LogLevel: "debug",
NodeName: "foo",
RPCAddr: "",
SerfBindAddr: "127.0.0.1",
SerfLanPort: 1000,
SerfWanPort: 2000,
ServerAddr: "127.0.0.1:8000",
AdvertiseAddr: "127.0.0.1:8000",
ClientAddr: "127.0.0.1",
BindAddr: "127.0.0.1",
AdvertiseAddr: "127.0.0.1",
Server: false,
LeaveOnTerm: false,
SkipLeaveOnInt: false,
@ -331,21 +330,24 @@ func TestMergeConfig(t *testing.T) {
}
b := &Config{
Bootstrap: true,
Datacenter: "dc2",
DataDir: "/tmp/bar",
DNSAddr: "127.0.0.2:1000",
DNSRecursor: "127.0.0.2:1001",
Domain: "other",
HTTPAddr: "127.0.0.1:12345",
LogLevel: "info",
NodeName: "baz",
RPCAddr: "127.0.0.1:9999",
SerfBindAddr: "127.0.0.2",
SerfLanPort: 3000,
SerfWanPort: 4000,
ServerAddr: "127.0.0.2:8000",
AdvertiseAddr: "127.0.0.2:8000",
Bootstrap: true,
Datacenter: "dc2",
DataDir: "/tmp/bar",
DNSRecursor: "127.0.0.2:1001",
Domain: "other",
LogLevel: "info",
NodeName: "baz",
ClientAddr: "127.0.0.1",
BindAddr: "127.0.0.1",
AdvertiseAddr: "127.0.0.1",
Ports: PortConfig{
DNS: 1,
HTTP: 2,
RPC: 3,
SerfLan: 4,
SerfWan: 5,
Server: 6,
},
Server: true,
LeaveOnTerm: true,
SkipLeaveOnInt: true,

View File

@ -12,9 +12,10 @@ import (
func makeDNSServer(t *testing.T) (string, *DNSServer) {
conf := nextConfig()
addr, _ := conf.ClientListener(conf.Ports.DNS)
dir, agent := makeAgent(t, conf)
server, err := NewDNSServer(agent, agent.logOutput, conf.Domain,
conf.DNSAddr, "8.8.8.8:53")
addr.String(), "8.8.8.8:53")
if err != nil {
t.Fatalf("err: %v", err)
}
@ -40,7 +41,8 @@ func TestDNS_IsAlive(t *testing.T) {
m.SetQuestion("_test.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}
@ -81,7 +83,8 @@ func TestDNS_NodeLookup(t *testing.T) {
m.SetQuestion("foo.node.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}
@ -103,7 +106,7 @@ func TestDNS_NodeLookup(t *testing.T) {
m.SetQuestion("foo.node.dc1.consul.", dns.TypeANY)
c = new(dns.Client)
in, _, err = c.Exchange(m, srv.agent.config.DNSAddr)
in, _, err = c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
}
@ -144,7 +147,8 @@ func TestDNS_NodeLookup_AAAA(t *testing.T) {
m.SetQuestion("bar.node.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}
@ -185,7 +189,8 @@ func TestDNS_NodeLookup_CNAME(t *testing.T) {
m.SetQuestion("google.node.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}
@ -232,7 +237,8 @@ func TestDNS_ServiceLookup(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeSRV)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}
@ -322,7 +328,8 @@ func TestDNS_ServiceLookup_Dedup(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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 +405,8 @@ func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeSRV)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}
@ -454,7 +462,8 @@ func TestDNS_Recurse(t *testing.T) {
c := new(dns.Client)
c.Net = "tcp"
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}
@ -519,7 +528,8 @@ func TestDNS_ServiceLookup_FilterCritical(t *testing.T) {
m.SetQuestion("db.service.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}
@ -557,12 +567,13 @@ 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)
for i := 0; i < 5; i++ {
m := new(dns.Msg)
m.SetQuestion("web.service.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
in, _, err := c.Exchange(m, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
}
@ -620,7 +631,8 @@ func TestDNS_ServiceLookup_CNAME(t *testing.T) {
m.SetQuestion("search.service.consul.", dns.TypeANY)
c := new(dns.Client)
in, _, err := c.Exchange(m, srv.agent.config.DNSAddr)
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)
}

View File

@ -15,7 +15,8 @@ import (
func makeHTTPServer(t *testing.T) (string, *HTTPServer) {
conf := nextConfig()
dir, agent := makeAgent(t, conf)
server, err := NewHTTPServer(agent, true, agent.logOutput, conf.HTTPAddr)
addr, _ := agent.config.ClientListener(agent.config.Ports.HTTP)
server, err := NewHTTPServer(agent, true, agent.logOutput, addr.String())
if err != nil {
t.Fatalf("err: %v", err)
}

View File

@ -61,7 +61,7 @@ func TestRPCClientForceLeave(t *testing.T) {
defer p2.Close()
testutil.Yield()
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.SerfLanPort)
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfLan)
if _, err := p1.agent.JoinLAN([]string{s2Addr}); err != nil {
t.Fatalf("err: %s", err)
}
@ -97,7 +97,7 @@ func TestRPCClientJoinLAN(t *testing.T) {
defer p2.Close()
testutil.Yield()
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.SerfLanPort)
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfLan)
n, err := p1.client.Join([]string{s2Addr}, false)
if err != nil {
t.Fatalf("err: %s", err)
@ -115,7 +115,7 @@ func TestRPCClientJoinWAN(t *testing.T) {
defer p2.Close()
testutil.Yield()
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.SerfWanPort)
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfWan)
n, err := p1.client.Join([]string{s2Addr}, true)
if err != nil {
t.Fatalf("err: %s", err)
@ -142,7 +142,7 @@ func TestRPCClientLANMembers(t *testing.T) {
t.Fatalf("bad: %#v", mem)
}
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.SerfLanPort)
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfLan)
_, err = p1.client.Join([]string{s2Addr}, false)
if err != nil {
t.Fatalf("err: %s", err)
@ -176,7 +176,7 @@ func TestRPCClientWANMembers(t *testing.T) {
t.Fatalf("bad: %#v", mem)
}
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.SerfWanPort)
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfWan)
_, err = p1.client.Join([]string{s2Addr}, true)
if err != nil {
t.Fatalf("err: %s", err)