Skip to content

Commit

Permalink
Merge pull request #1204 from hashicorp/fix-server-address
Browse files Browse the repository at this point in the history
Use Bind Addr as server's IP while registering with consul in the absence of other configuration.
  • Loading branch information
diptanu committed May 25, 2016
2 parents b6f1d4b + 9d8f6d0 commit 20f4042
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
13 changes: 13 additions & 0 deletions command/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 20f4042

Please sign in to comment.