segments: oss changes for enterprise network area changes (#7786)

OSS code changes for network segments
This commit is contained in:
Hans Hasselberg 2020-05-05 21:41:19 +02:00 committed by GitHub
commit 5d2b10e862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 68 deletions

View File

@ -24,7 +24,7 @@ func (s *Server) FloodNotify() {
// Flood is a long-running goroutine that floods servers from the LAN to the // Flood is a long-running goroutine that floods servers from the LAN to the
// given global Serf instance, such as the WAN. This will exit once either of // given global Serf instance, such as the WAN. This will exit once either of
// the Serf instances are shut down. // the Serf instances are shut down.
func (s *Server) Flood(addrFn router.FloodAddrFn, portFn router.FloodPortFn, global *serf.Serf) { func (s *Server) Flood(addrFn router.FloodAddrFn, dstSerf *serf.Serf) {
s.floodLock.Lock() s.floodLock.Lock()
floodCh := make(chan struct{}) floodCh := make(chan struct{})
s.floodCh = append(s.floodCh, floodCh) s.floodCh = append(s.floodCh, floodCh)
@ -50,17 +50,15 @@ func (s *Server) Flood(addrFn router.FloodAddrFn, portFn router.FloodPortFn, glo
case <-s.serfLAN.ShutdownCh(): case <-s.serfLAN.ShutdownCh():
return return
case <-global.ShutdownCh(): case <-dstSerf.ShutdownCh():
return return
case <-ticker.C: case <-ticker.C:
goto FLOOD router.FloodJoins(s.logger, addrFn, s.config.Datacenter, s.serfLAN, dstSerf)
case <-floodCh: case <-floodCh:
goto FLOOD router.FloodJoins(s.logger, addrFn, s.config.Datacenter, s.serfLAN, dstSerf)
} }
FLOOD:
router.FloodJoins(s.logger, addrFn, portFn, s.config.Datacenter, s.serfLAN, global)
} }
} }

View File

@ -558,13 +558,17 @@ func NewServerLogger(config *Config, logger hclog.InterceptLogger, tokens *token
go router.HandleSerfEvents(s.logger, s.router, types.AreaWAN, s.serfWAN.ShutdownCh(), s.eventChWAN) go router.HandleSerfEvents(s.logger, s.router, types.AreaWAN, s.serfWAN.ShutdownCh(), s.eventChWAN)
// Fire up the LAN <-> WAN join flooder. // Fire up the LAN <-> WAN join flooder.
portFn := func(s *metadata.Server) (int, bool) { addrFn := func(s *metadata.Server) (string, error) {
if s.WanJoinPort > 0 { if s.WanJoinPort == 0 {
return s.WanJoinPort, true return "", fmt.Errorf("no wan join port for server: %s", s.Addr.String())
} }
return 0, false addr, _, err := net.SplitHostPort(s.Addr.String())
if err != nil {
return "", err
} }
go s.Flood(nil, portFn, s.serfWAN) return fmt.Sprintf("%s:%d", addr, s.WanJoinPort), nil
}
go s.Flood(addrFn, s.serfWAN)
} }
// Start enterprise specific functionality // Start enterprise specific functionality

View File

@ -49,11 +49,6 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string, w
conf.Tags["role"] = "consul" conf.Tags["role"] = "consul"
conf.Tags["dc"] = s.config.Datacenter conf.Tags["dc"] = s.config.Datacenter
conf.Tags["segment"] = segment conf.Tags["segment"] = segment
if segment == "" {
for _, s := range s.config.Segments {
conf.Tags["sl_"+s.Name] = net.JoinHostPort(s.Advertise, fmt.Sprintf("%d", s.Port))
}
}
conf.Tags["id"] = string(s.config.NodeID) conf.Tags["id"] = string(s.config.NodeID)
conf.Tags["vsn"] = fmt.Sprintf("%d", s.config.ProtocolVersion) conf.Tags["vsn"] = fmt.Sprintf("%d", s.config.ProtocolVersion)
conf.Tags["vsn_min"] = fmt.Sprintf("%d", ProtocolVersionMin) conf.Tags["vsn_min"] = fmt.Sprintf("%d", ProtocolVersionMin)

View File

@ -145,7 +145,7 @@ func testServerConfig(t *testing.T) (string, *Config) {
config.ServerHealthInterval = 50 * time.Millisecond config.ServerHealthInterval = 50 * time.Millisecond
config.AutopilotInterval = 100 * time.Millisecond config.AutopilotInterval = 100 * time.Millisecond
config.Build = "1.4.0" config.Build = "1.7.2"
config.CoordinateUpdatePeriod = 100 * time.Millisecond config.CoordinateUpdatePeriod = 100 * time.Millisecond
config.LeaveDrainTime = 1 * time.Millisecond config.LeaveDrainTime = 1 * time.Millisecond

View File

@ -2,7 +2,6 @@ package router
import ( import (
"fmt" "fmt"
"net"
"strings" "strings"
"github.com/hashicorp/consul/agent/metadata" "github.com/hashicorp/consul/agent/metadata"
@ -10,29 +9,25 @@ import (
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
) )
// FloodAddrFn gets the address to use for a given server when flood-joining. This // FloodAddrPortFn gets the address and port to use for a given server when
// will return false if it doesn't have one. // flood-joining. This will return false if it doesn't have one.
type FloodAddrFn func(*metadata.Server) (string, bool) type FloodAddrFn func(*metadata.Server) (string, error)
// FloodPortFn gets the port to use for a given server when flood-joining. This // FloodJoins attempts to make sure all Consul servers in the src Serf
// will return false if it doesn't have one. // instance are joined in the dst Serf instance. It assumes names in the
type FloodPortFn func(*metadata.Server) (int, bool) // src area are of the form <node> and those in the dst area are of the
// FloodJoins attempts to make sure all Consul servers in the local Serf
// instance are joined in the global Serf instance. It assumes names in the
// local area are of the form <node> and those in the global area are of the
// form <node>.<dc> as is done for WAN and general network areas in Consul // form <node>.<dc> as is done for WAN and general network areas in Consul
// Enterprise. // Enterprise.
func FloodJoins(logger hclog.Logger, addrFn FloodAddrFn, portFn FloodPortFn, func FloodJoins(logger hclog.Logger, addrFn FloodAddrFn,
localDatacenter string, localSerf *serf.Serf, globalSerf *serf.Serf) { localDatacenter string, srcSerf *serf.Serf, dstSerf *serf.Serf) {
// Names in the global Serf have the datacenter suffixed. // Names in the dst Serf have the datacenter suffixed.
suffix := fmt.Sprintf(".%s", localDatacenter) suffix := fmt.Sprintf(".%s", localDatacenter)
// Index the global side so we can do one pass through the local side // Index the dst side so we can do one pass through the src side
// with cheap lookups. // with cheap lookups.
index := make(map[string]*metadata.Server) index := make(map[string]*metadata.Server)
for _, m := range globalSerf.Members() { for _, m := range dstSerf.Members() {
ok, server := metadata.IsConsulServer(m) ok, server := metadata.IsConsulServer(m)
if !ok { if !ok {
continue continue
@ -42,12 +37,12 @@ func FloodJoins(logger hclog.Logger, addrFn FloodAddrFn, portFn FloodPortFn,
continue continue
} }
localName := strings.TrimSuffix(server.Name, suffix) srcName := strings.TrimSuffix(server.Name, suffix)
index[localName] = server index[srcName] = server
} }
// Now run through the local side and look for joins. // Now run through the src side and look for joins.
for _, m := range localSerf.Members() { for _, m := range srcSerf.Members() {
if m.Status != serf.StatusAlive { if m.Status != serf.StatusAlive {
continue continue
} }
@ -61,51 +56,28 @@ func FloodJoins(logger hclog.Logger, addrFn FloodAddrFn, portFn FloodPortFn,
continue continue
} }
// We can't use the port number from the local Serf, so we just addr, err := addrFn(server)
// get the host part.
addr, _, err := net.SplitHostPort(server.Addr.String())
if err != nil { if err != nil {
logger.Debug("Failed to flood-join server (bad address)", logger.Debug("Failed to flood-join server", "server",
"server", server.Name, server.Name, "address", server.Addr.String(),
"address", server.Addr.String(),
"error", err, "error", err,
) )
} continue
if addrFn != nil {
if a, ok := addrFn(server); ok {
addr = a
}
} }
// Let the callback see if it can get the port number, otherwise dstServerName := fmt.Sprintf("%s.%s", server.Name, server.Datacenter)
// leave it blank to behave as if we just supplied an address.
if port, ok := portFn(server); ok {
addr = net.JoinHostPort(addr, fmt.Sprintf("%d", port))
} else {
// If we have an IPv6 address, we should add brackets,
// single globalSerf.Join expects that.
if ip := net.ParseIP(addr); ip != nil {
if ip.To4() == nil {
addr = fmt.Sprintf("[%s]", addr)
}
} else {
logger.Debug("Failed to parse IP", "ip", addr)
}
}
globalServerName := fmt.Sprintf("%s.%s", server.Name, server.Datacenter)
// Do the join! // Do the join!
n, err := globalSerf.Join([]string{globalServerName + "/" + addr}, true) n, err := dstSerf.Join([]string{dstServerName + "/" + addr}, true)
if err != nil { if err != nil {
logger.Debug("Failed to flood-join server at address", logger.Debug("Failed to flood-join server at address",
"server", globalServerName, "server", dstServerName,
"address", addr, "address", addr,
"error", err, "error", err,
) )
} else if n > 0 { } else if n > 0 {
logger.Debug("Successfully performed flood-join for server at address", logger.Debug("Successfully performed flood-join for server at address",
"server", globalServerName, "server", dstServerName,
"address", addr, "address", addr,
) )
} }