2015-01-06 23:48:46 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2015-02-23 02:24:10 +00:00
|
|
|
"fmt"
|
2021-12-06 19:18:02 +00:00
|
|
|
"sync"
|
2015-01-06 23:48:46 +00:00
|
|
|
|
2017-09-06 05:57:29 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2015-01-06 23:48:46 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2021-08-19 20:09:42 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
|
|
|
"github.com/hashicorp/consul/types"
|
2015-01-06 23:48:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// lanMergeDelegate is used to handle a cluster merge on the LAN gossip
|
|
|
|
// ring. We check that the peers are in the same datacenter and abort the
|
|
|
|
// merge if there is a mis-match.
|
|
|
|
type lanMergeDelegate struct {
|
2021-11-15 15:51:14 +00:00
|
|
|
dc string
|
|
|
|
nodeID types.NodeID
|
|
|
|
nodeName string
|
|
|
|
segment string
|
2021-08-19 20:09:42 +00:00
|
|
|
server bool
|
|
|
|
partition string
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:57:29 +00:00
|
|
|
// uniqueIDMinVersion is the lowest version where we insist that nodes
|
|
|
|
// have a unique ID.
|
|
|
|
var uniqueIDMinVersion = version.Must(version.NewVersion("0.8.5"))
|
|
|
|
|
2015-02-23 02:24:10 +00:00
|
|
|
func (md *lanMergeDelegate) NotifyMerge(members []*serf.Member) error {
|
2017-03-27 07:15:42 +00:00
|
|
|
nodeMap := make(map[types.NodeID]string)
|
2015-01-06 23:48:46 +00:00
|
|
|
for _, m := range members {
|
2017-03-27 07:15:42 +00:00
|
|
|
if rawID, ok := m.Tags["id"]; ok && rawID != "" {
|
2020-05-04 16:39:08 +00:00
|
|
|
if m.Status == serf.StatusLeft {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-27 07:15:42 +00:00
|
|
|
nodeID := types.NodeID(rawID)
|
|
|
|
|
|
|
|
// See if there's another node that conflicts with us.
|
|
|
|
if (nodeID == md.nodeID) && (m.Name != md.nodeName) {
|
|
|
|
return fmt.Errorf("Member '%s' has conflicting node ID '%s' with this agent's ID",
|
|
|
|
m.Name, nodeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if there are any two nodes that conflict with each
|
|
|
|
// other. This lets us only do joins into a hygienic
|
|
|
|
// cluster now that node IDs are critical for operation.
|
|
|
|
if other, ok := nodeMap[nodeID]; ok {
|
|
|
|
return fmt.Errorf("Member '%s' has conflicting node ID '%s' with member '%s'",
|
|
|
|
m.Name, nodeID, other)
|
|
|
|
}
|
2017-09-06 05:57:29 +00:00
|
|
|
|
2017-09-06 20:23:19 +00:00
|
|
|
// Only map nodes with a version that's >= than when
|
2017-09-06 05:57:29 +00:00
|
|
|
// we made host-based IDs opt-in, which helps prevent
|
|
|
|
// chaos when upgrading older clusters. See #3070 for
|
|
|
|
// more details.
|
|
|
|
if ver, err := metadata.Build(m); err == nil {
|
|
|
|
if ver.Compare(uniqueIDMinVersion) >= 0 {
|
|
|
|
nodeMap[nodeID] = m.Name
|
|
|
|
}
|
|
|
|
}
|
2017-03-27 07:15:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 00:05:40 +00:00
|
|
|
if ok, dc := isConsulNode(*m); ok {
|
2015-01-06 23:48:46 +00:00
|
|
|
if dc != md.dc {
|
2015-02-23 02:24:10 +00:00
|
|
|
return fmt.Errorf("Member '%s' part of wrong datacenter '%s'",
|
2015-01-06 23:48:46 +00:00
|
|
|
m.Name, dc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 00:05:40 +00:00
|
|
|
if ok, parts := metadata.IsConsulServer(*m); ok {
|
|
|
|
if parts.Datacenter != md.dc {
|
|
|
|
return fmt.Errorf("Member '%s' part of wrong datacenter '%s'",
|
|
|
|
m.Name, parts.Datacenter)
|
|
|
|
}
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
if err := md.enterpriseNotifyMergeMember(m); err != nil {
|
|
|
|
return err
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
2015-02-23 02:24:10 +00:00
|
|
|
return nil
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wanMergeDelegate is used to handle a cluster merge on the WAN gossip
|
|
|
|
// ring. We check that the peers are server nodes and abort the merge
|
|
|
|
// otherwise.
|
|
|
|
type wanMergeDelegate struct {
|
2021-12-06 19:18:02 +00:00
|
|
|
localDatacenter string
|
|
|
|
|
|
|
|
federationDisabledLock sync.Mutex
|
|
|
|
federationDisabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWANFederationDisabled selectively disables the wan pool from accepting
|
|
|
|
// non-local members. If the toggle changed the current value it returns true.
|
|
|
|
func (md *wanMergeDelegate) SetWANFederationDisabled(disabled bool) bool {
|
|
|
|
md.federationDisabledLock.Lock()
|
|
|
|
prior := md.federationDisabled
|
|
|
|
md.federationDisabled = disabled
|
|
|
|
md.federationDisabledLock.Unlock()
|
|
|
|
|
|
|
|
return prior != disabled
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 02:24:10 +00:00
|
|
|
func (md *wanMergeDelegate) NotifyMerge(members []*serf.Member) error {
|
2021-12-06 19:18:02 +00:00
|
|
|
// Deliberately hold this lock during the entire merge so calls to
|
|
|
|
// SetWANFederationDisabled returning immediately imply that the flag takes
|
|
|
|
// effect for all future merges.
|
|
|
|
md.federationDisabledLock.Lock()
|
|
|
|
defer md.federationDisabledLock.Unlock()
|
|
|
|
|
2015-01-06 23:48:46 +00:00
|
|
|
for _, m := range members {
|
2021-12-06 19:18:02 +00:00
|
|
|
ok, srv := metadata.IsConsulServer(*m)
|
2015-01-06 23:48:46 +00:00
|
|
|
if !ok {
|
2015-02-23 02:24:10 +00:00
|
|
|
return fmt.Errorf("Member '%s' is not a server", m.Name)
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
2021-12-06 19:18:02 +00:00
|
|
|
|
|
|
|
if md.federationDisabled {
|
|
|
|
if srv.Datacenter != md.localDatacenter {
|
|
|
|
return fmt.Errorf("Member '%s' part of wrong datacenter '%s'; WAN federation is disabled", m.Name, srv.Datacenter)
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
2015-02-23 02:24:10 +00:00
|
|
|
return nil
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|