From a235986a28a30710dd357f0196565e0c439f0f4f Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Wed, 3 May 2017 22:59:06 +0200 Subject: [PATCH] Do not modify config after creation Make sure the RPCAdvertise address is always set so that the configuration does not have to be modified after creation. --- command/agent/agent.go | 5 +++++ consul/server.go | 24 ++++++------------------ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index f391ca0049..cebd973a2f 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -437,6 +437,11 @@ func (a *Agent) consulConfig() *consul.Config { base.AutopilotConfig.DisableUpgradeMigration = *a.config.Autopilot.DisableUpgradeMigration } + // make sure the advertise address is always set + if base.RPCAdvertise == nil { + base.RPCAdvertise = base.RPCAddr + } + // Format the build string revision := a.config.Revision if len(revision) > 8 { diff --git a/consul/server.go b/consul/server.go index 42c149ba6f..d9185f92e2 100644 --- a/consul/server.go +++ b/consul/server.go @@ -598,34 +598,22 @@ func (s *Server) setupRPC(tlsWrap tlsutil.DCWrapper) error { s.rpcServer.Register(s.endpoints.Status) s.rpcServer.Register(s.endpoints.Txn) - list, err := net.ListenTCP("tcp", s.config.RPCAddr) + ln, err := net.ListenTCP("tcp", s.config.RPCAddr) if err != nil { return err } - s.rpcListener = list - - var advertise net.Addr - if s.config.RPCAdvertise != nil { - advertise = s.config.RPCAdvertise - } else { - advertise = s.rpcListener.Addr() - } + s.rpcListener = ln // Verify that we have a usable advertise address - addr, ok := advertise.(*net.TCPAddr) - if !ok { - list.Close() - return fmt.Errorf("RPC advertise address is not a TCP Address: %v", addr) - } - if addr.IP.IsUnspecified() { - list.Close() - return fmt.Errorf("RPC advertise address is not advertisable: %v", addr) + if s.config.RPCAdvertise.IP.IsUnspecified() { + ln.Close() + return fmt.Errorf("RPC advertise address is not advertisable: %v", s.config.RPCAdvertise) } // Provide a DC specific wrapper. Raft replication is only // ever done in the same datacenter, so we can provide it as a constant. wrapper := tlsutil.SpecificDC(s.config.Datacenter, tlsWrap) - s.raftLayer = NewRaftLayer(advertise, wrapper) + s.raftLayer = NewRaftLayer(s.config.RPCAdvertise, wrapper) return nil }