Merge pull request #1204 from hashicorp/fix-server-address

Use Bind Addr as server's IP while registering with consul in the absence of other configuration.
This commit is contained in:
Diptanu Choudhury
2016-05-24 22:30:35 -07:00
2 changed files with 20 additions and 1 deletions

View File

@@ -159,9 +159,15 @@ func (a *Agent) serverConfig() (*nomad.Config, error) {
if port := a.config.Ports.Serf; port != 0 {
conf.SerfConfig.MemberlistConfig.BindPort = port
}
a.serverHTTPAddr = fmt.Sprintf("%v:%v", a.config.Addresses.HTTP, a.config.Ports.HTTP)
if a.config.AdvertiseAddrs.HTTP != "" {
a.serverHTTPAddr = a.config.AdvertiseAddrs.HTTP
} else if a.config.Addresses.HTTP != "" {
a.serverHTTPAddr = fmt.Sprintf("%v:%v", a.config.Addresses.HTTP, a.config.Ports.HTTP)
} else if a.config.BindAddr != "" {
a.serverHTTPAddr = fmt.Sprintf("%v:%v", a.config.BindAddr, a.config.Ports.HTTP)
} else {
a.serverHTTPAddr = fmt.Sprintf("%v:%v", "127.0.0.1", a.config.Ports.HTTP)
}
if gcThreshold := a.config.Server.NodeGCThreshold; gcThreshold != "" {

View File

@@ -100,6 +100,7 @@ func TestAgent_ServerConfig(t *testing.T) {
t.Fatalf("expected rpc address error, got: %#v", err)
}
conf.AdvertiseAddrs.RPC = "127.0.0.1:4001"
conf.AdvertiseAddrs.HTTP = "10.10.11.1:4005"
// Parses the advertise addrs correctly
out, err := a.serverConfig()
@@ -117,6 +118,9 @@ func TestAgent_ServerConfig(t *testing.T) {
if addr := out.RPCAdvertise; addr.IP.String() != "127.0.0.1" || addr.Port != 4001 {
t.Fatalf("bad rpc advertise addr: %#v", addr)
}
if addr := a.serverHTTPAddr; addr != "10.10.11.1:4005" {
t.Fatalf("expect 10.11.11.1:4005, got: %v", addr)
}
// Sets up the ports properly
conf.Ports.RPC = 4003
@@ -137,6 +141,8 @@ func TestAgent_ServerConfig(t *testing.T) {
conf.BindAddr = "127.0.0.3"
conf.Addresses.RPC = "127.0.0.2"
conf.Addresses.Serf = "127.0.0.2"
conf.Addresses.HTTP = "127.0.0.2"
conf.AdvertiseAddrs.HTTP = ""
out, err = a.serverConfig()
if err != nil {
@@ -148,6 +154,9 @@ func TestAgent_ServerConfig(t *testing.T) {
if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.2" {
t.Fatalf("expect 127.0.0.2, got: %s", addr)
}
if addr := a.serverHTTPAddr; addr != "127.0.0.2:4646" {
t.Fatalf("expect 127.0.0.3:4646, got: %s", addr)
}
conf.Server.NodeGCThreshold = "42g"
out, err = a.serverConfig()
@@ -174,6 +183,7 @@ func TestAgent_ServerConfig(t *testing.T) {
// Defaults to the global bind addr
conf.Addresses.RPC = ""
conf.Addresses.Serf = ""
conf.Addresses.HTTP = ""
out, err = a.serverConfig()
if err != nil {
t.Fatalf("err: %s", err)
@@ -184,6 +194,9 @@ func TestAgent_ServerConfig(t *testing.T) {
if addr := out.SerfConfig.MemberlistConfig.BindAddr; addr != "127.0.0.3" {
t.Fatalf("expect 127.0.0.3, got: %s", addr)
}
if addr := a.serverHTTPAddr; addr != "127.0.0.3:4646" {
t.Fatalf("expect 127.0.0.3:4646, got: %s", addr)
}
// Properly handles the bootstrap flags
conf.Server.BootstrapExpect = 1