Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advertise_addrs. #1004

Merged
merged 1 commit into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ func (a *Agent) consulConfig() *consul.Config {
Port: a.config.Ports.Server,
}
}
if a.config.AdvertiseAddrs.SerfLan != nil {
base.SerfLANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddrs.SerfLan.IP.String()
base.SerfLANConfig.MemberlistConfig.AdvertisePort = a.config.AdvertiseAddrs.SerfLan.Port
}
if a.config.AdvertiseAddrs.SerfWan != nil {
base.SerfWANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddrs.SerfWan.IP.String()
base.SerfWANConfig.MemberlistConfig.AdvertisePort = a.config.AdvertiseAddrs.SerfWan.Port
}
if a.config.AdvertiseAddrs.RPC != nil {
base.RPCAdvertise = a.config.AdvertiseAddrs.RPC
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of printing a message for both of the above and allowing Consul to start even with a bad advertise address specified in its config, can we store the *net.TCPAddrs in the AdvertiseAddrs struct and parse the address while we are decoding? This would also make a config merge pretty easy for these fields. We do a similar thing with parsing time values.

if a.config.Bootstrap {
base.Bootstrap = true
}
Expand Down
32 changes: 32 additions & 0 deletions command/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -134,6 +135,37 @@ func TestAgent_RPCPing(t *testing.T) {
}
}

func TestAgent_CheckAdvertiseAddrsSettings(t *testing.T) {
c := nextConfig()
c.AdvertiseAddrs.SerfLan, _ = net.ResolveTCPAddr("tcp", "127.0.0.42:1233")
c.AdvertiseAddrs.SerfWan, _ = net.ResolveTCPAddr("tcp", "127.0.0.43:1234")
c.AdvertiseAddrs.RPC, _ = net.ResolveTCPAddr("tcp", "127.0.0.44:1235")
dir, agent := makeAgent(t, c)
defer os.RemoveAll(dir)
defer agent.Shutdown()

serfLanAddr := agent.consulConfig().SerfLANConfig.MemberlistConfig.AdvertiseAddr
if serfLanAddr != "127.0.0.42" {
t.Fatalf("SerfLan is not properly set to '127.0.0.42': %s", serfLanAddr)
}
serfLanPort := agent.consulConfig().SerfLANConfig.MemberlistConfig.AdvertisePort
if serfLanPort != 1233 {
t.Fatalf("SerfLan is not properly set to '1233': %s", serfLanPort)
}
serfWanAddr := agent.consulConfig().SerfWANConfig.MemberlistConfig.AdvertiseAddr
if serfWanAddr != "127.0.0.43" {
t.Fatalf("SerfWan is not properly set to '127.0.0.43': %s", serfWanAddr)
}
serfWanPort := agent.consulConfig().SerfWANConfig.MemberlistConfig.AdvertisePort
if serfWanPort != 1234 {
t.Fatalf("SerfWan is not properly set to '1234': %s", serfWanPort)
}
rpc := agent.consulConfig().RPCAdvertise
if rpc != c.AdvertiseAddrs.RPC {
t.Fatalf("RPC is not properly set to %v: %s", c.AdvertiseAddrs.RPC, rpc)
}
}

func TestAgent_AddService(t *testing.T) {
dir, agent := makeAgent(t, nextConfig())
defer os.RemoveAll(dir)
Expand Down
48 changes: 48 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ type AddressConfig struct {
RPC string // CLI RPC
}

type AdvertiseAddrsConfig struct {
SerfLan *net.TCPAddr `mapstructure:"-"`
SerfLanRaw string `mapstructure:"serf_lan"`
SerfWan *net.TCPAddr `mapstructure:"-"`
SerfWanRaw string `mapstructure:"serf_wan"`
RPC *net.TCPAddr `mapstructure:"-"`
RPCRaw string `mapstructure:"rpc"`
}

// DNSConfig is used to fine tune the DNS sub-system.
// It can be used to control cache values, and stale
// reads
Expand Down Expand Up @@ -142,6 +151,9 @@ type Config struct {
// and Consul RPC IP. If not specified, bind address is used.
AdvertiseAddr string `mapstructure:"advertise_addr"`

// AdvertiseAddrs configuration
AdvertiseAddrs AdvertiseAddrsConfig `mapstructure:"advertise_addrs"`

// AdvertiseAddrWan is the address we use for advertising our
// Serf WAN IP. If not specified, the general advertise address is used.
AdvertiseAddrWan string `mapstructure:"advertise_addr_wan"`
Expand Down Expand Up @@ -638,6 +650,30 @@ func DecodeConfig(r io.Reader) (*Config, error) {
result.SessionTTLMin = dur
}

if result.AdvertiseAddrs.SerfLanRaw != "" {
addr, err := net.ResolveTCPAddr("tcp", result.AdvertiseAddrs.SerfLanRaw)
if err != nil {
return nil, fmt.Errorf("AdvertiseAddrs.SerfLan is invalid: %v", err)
}
result.AdvertiseAddrs.SerfLan = addr
}

if result.AdvertiseAddrs.SerfWanRaw != "" {
addr, err := net.ResolveTCPAddr("tcp", result.AdvertiseAddrs.SerfWanRaw)
if err != nil {
return nil, fmt.Errorf("AdvertiseAddrs.SerfWan is invalid: %v", err)
}
result.AdvertiseAddrs.SerfWan = addr
}

if result.AdvertiseAddrs.RPCRaw != "" {
addr, err := net.ResolveTCPAddr("tcp", result.AdvertiseAddrs.RPCRaw)
if err != nil {
return nil, fmt.Errorf("AdvertiseAddrs.RPC is invalid: %v", err)
}
result.AdvertiseAddrs.RPC = addr
}

return &result, nil
}

Expand Down Expand Up @@ -819,6 +855,18 @@ func MergeConfig(a, b *Config) *Config {
if b.AdvertiseAddrWan != "" {
result.AdvertiseAddrWan = b.AdvertiseAddrWan
}
if b.AdvertiseAddrs.SerfLan != nil {
result.AdvertiseAddrs.SerfLan = b.AdvertiseAddrs.SerfLan
result.AdvertiseAddrs.SerfLanRaw = b.AdvertiseAddrs.SerfLanRaw
}
if b.AdvertiseAddrs.SerfWan != nil {
result.AdvertiseAddrs.SerfWan = b.AdvertiseAddrs.SerfWan
result.AdvertiseAddrs.SerfWanRaw = b.AdvertiseAddrs.SerfWanRaw
}
if b.AdvertiseAddrs.RPC != nil {
result.AdvertiseAddrs.RPC = b.AdvertiseAddrs.RPC
result.AdvertiseAddrs.RPCRaw = b.AdvertiseAddrs.RPCRaw
}
if b.Server == true {
result.Server = b.Server
}
Expand Down
48 changes: 48 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"io/ioutil"
"net"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -211,6 +212,45 @@ func TestDecodeConfig(t *testing.T) {
t.Fatalf("bad: %#v", config)
}

// Advertise addresses for serflan
input = `{"advertise_addrs": {"serf_lan": "127.0.0.5:1234"}}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.AdvertiseAddrs.SerfLanRaw != "127.0.0.5:1234" {
t.Fatalf("bad: %#v", config)
}
if config.AdvertiseAddrs.SerfLan.String() != "127.0.0.5:1234" {
t.Fatalf("bad: %#v", config)
}

// Advertise addresses for serfwan
input = `{"advertise_addrs": {"serf_wan": "127.0.0.5:1234"}}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.AdvertiseAddrs.SerfWanRaw != "127.0.0.5:1234" {
t.Fatalf("bad: %#v", config)
}
if config.AdvertiseAddrs.SerfWan.String() != "127.0.0.5:1234" {
t.Fatalf("bad: %#v", config)
}

// Advertise addresses for rpc
input = `{"advertise_addrs": {"rpc": "127.0.0.5:1234"}}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.AdvertiseAddrs.RPCRaw != "127.0.0.5:1234" {
t.Fatalf("bad: %#v", config)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In these tests we should also test that the *net.TCPAddr is populated correctly.

if config.AdvertiseAddrs.RPC.String() != "127.0.0.5:1234" {
t.Fatalf("bad: %#v", config)
}

// leave_on_terminate
input = `{"leave_on_terminate": true}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
Expand Down Expand Up @@ -1166,6 +1206,14 @@ func TestMergeConfig(t *testing.T) {
AtlasJoin: true,
SessionTTLMinRaw: "1000s",
SessionTTLMin: 1000 * time.Second,
AdvertiseAddrs: AdvertiseAddrsConfig{
SerfLan: &net.TCPAddr{},
SerfLanRaw: "127.0.0.5:1231",
SerfWan: &net.TCPAddr{},
SerfWanRaw: "127.0.0.5:1232",
RPC: &net.TCPAddr{},
RPCRaw: "127.0.0.5:1233",
},
}

c := MergeConfig(a, b)
Expand Down
10 changes: 10 additions & 0 deletions website/source/docs/agent/options.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ definitions support being updated during a reload.
* <a name="advertise_addr"></a><a href="#advertise_addr">`advertise_addr`</a> Equivalent to
the [`-advertise` command-line flag](#_advertise).

* <a name="advertise_addrs"></a><a href="#advertise_addrs">`advertise_addrs`</a> Allows to set
the advertised addresses for SerfLan, SerfWan and RPC together with the port. This gives
you more control than (#_advertise) or (#_advertise-wan) while it serves the same purpose.
These settings might override (#_advertise) and (#_advertise-wan).
<br><br>
This is a nested setting that allows the following keys:
* `serf_lan` - The SerfLan address. Accepts values in the form of "host:port" like "10.23.31.101:8301".
* `serf_wan` - The SerfWan address. Accepts values in the form of "host:port" like "10.23.31.101:8302".
* `rpc` - The RPC address. Accepts values in the form of "host:port" like "10.23.31.101:8400".

* <a name="advertise_addr_wan"></a><a href="#advertise_addr_wan">`advertise_addr_wan`</a> Equivalent to
the [`-advertise-wan` command-line flag](#_advertise-wan).

Expand Down