2015-01-06 23:48:46 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2015-02-23 02:24:10 +00:00
|
|
|
"fmt"
|
2015-01-06 23:48:46 +00:00
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2017-03-27 07:15:42 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2015-01-06 23:48:46 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2017-03-27 07:15:42 +00:00
|
|
|
dc string
|
|
|
|
nodeID types.NodeID
|
|
|
|
nodeName string
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
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 != "" {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
nodeMap[nodeID] = m.Name
|
|
|
|
}
|
|
|
|
|
2015-01-06 23:48:46 +00:00
|
|
|
ok, dc := isConsulNode(*m)
|
|
|
|
if ok {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, parts := metadata.IsConsulServer(*m)
|
2015-01-06 23:48:46 +00:00
|
|
|
if ok && parts.Datacenter != 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, parts.Datacenter)
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
}
|
|
|
|
|
2015-02-23 02:24:10 +00:00
|
|
|
func (md *wanMergeDelegate) NotifyMerge(members []*serf.Member) error {
|
2015-01-06 23:48:46 +00:00
|
|
|
for _, m := range members {
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, _ := 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
|
|
|
}
|
|
|
|
}
|
2015-02-23 02:24:10 +00:00
|
|
|
return nil
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|