Add configuration option to specify a separate address for advertising on the wan

This commit is contained in:
pepov 2015-03-21 10:14:03 +01:00
parent f5d3e5653f
commit e66301ab99
3 changed files with 38 additions and 1 deletions

View file

@ -127,6 +127,15 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
config.AdvertiseAddr = ip.String()
}
// Try to get an advertise address for the wan
if config.AdvertiseAddrWan != "" {
if ip := net.ParseIP(config.AdvertiseAddrWan); ip == nil {
return nil, fmt.Errorf("Failed to parse advertise address for wan: %v", config.AdvertiseAddrWan)
}
} else {
config.AdvertiseAddrWan = config.AdvertiseAddr
}
agent := &Agent{
config: config,
logger: log.New(logOutput, "", log.LstdFlags),
@ -225,7 +234,11 @@ func (a *Agent) consulConfig() *consul.Config {
}
if a.config.AdvertiseAddr != "" {
base.SerfLANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr
base.SerfWANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr
if a.config.AdvertiseAddrWan != "" {
base.SerfWANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddrWan
} else {
base.SerfWANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr
}
base.RPCAdvertise = &net.TCPAddr{
IP: net.ParseIP(a.config.AdvertiseAddr),
Port: a.config.Ports.Server,

View file

@ -142,6 +142,10 @@ type Config struct {
// and Consul RPC IP. If not specified, bind address is used.
AdvertiseAddr string `mapstructure:"advertise_addr"`
// 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"`
// Port configurations
Ports PortConfig
@ -799,6 +803,9 @@ func MergeConfig(a, b *Config) *Config {
if b.AdvertiseAddr != "" {
result.AdvertiseAddr = b.AdvertiseAddr
}
if b.AdvertiseAddrWan != "" {
result.AdvertiseAddrWan = b.AdvertiseAddrWan
}
if b.Server == true {
result.Server = b.Server
}

View file

@ -190,10 +190,27 @@ func TestDecodeConfig(t *testing.T) {
t.Fatalf("bad: %#v", config)
}
if config.AdvertiseAddrWan != "" {
t.Fatalf("bad: %#v", config)
}
if config.Ports.Server != 8000 {
t.Fatalf("bad: %#v", config)
}
// Advertise address for wan
input = `{"advertise_addr_wan": "127.0.0.5"}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.AdvertiseAddr != "" {
t.Fatalf("bad: %#v", config)
}
if config.AdvertiseAddrWan != "127.0.0.5" {
t.Fatalf("bad: %#v", config)
}
// leave_on_terminate
input = `{"leave_on_terminate": true}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))