Parse metadata outside of cluster.go

Move the calls for metadata decoding from the cluster
membership management loop to the main loop. This task
was not directly related to the cluster, and was adding
complexity, including the need for multierr structures.
This commit is contained in:
kaiyou 2020-05-06 19:27:21 +02:00 committed by Leo Antunes
parent 366f906d5d
commit 0e799d6074
4 changed files with 17 additions and 22 deletions

View File

@ -15,7 +15,6 @@ import (
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.uber.org/multierr"
)
// ClusterState keeps track of information needed to rejoin the cluster
@ -120,9 +119,8 @@ func (c *cluster) update() {
c.ml.UpdateNode(1 * time.Second) // we currently do not update after creation
}
func (c *cluster) members() (<-chan []node, <-chan error) {
func (c *cluster) members() <-chan []node {
changes := make(chan []node)
errc := make(chan error, 1)
go func() {
for {
event := <-c.events
@ -140,31 +138,22 @@ func (c *cluster) members() (<-chan []node, <-chan error) {
}
nodes := make([]node, 0)
var errs error
for _, n := range c.ml.Members() {
if n.Name == c.localName {
continue
}
meta, err := decodeNodeMeta(n.Meta)
if err != nil {
errs = multierr.Append(errs, err)
continue
}
nodes = append(nodes, node{
Name: n.Name,
Addr: n.Addr,
nodeMeta: meta,
Name: n.Name,
Addr: n.Addr,
Meta: n.Meta,
})
}
c.state.Nodes = nodes
changes <- nodes
if errs != nil {
errc <- errs
}
c.saveState()
}
}()
return changes, errc
return changes
}
func computeClusterKey(state *ClusterState, clusterKey []byte) ([]byte, error) {

1
go.mod
View File

@ -16,7 +16,6 @@ require (
github.com/stevenroose/gonfig v0.1.5
github.com/stretchr/testify v1.4.0 // indirect
github.com/vishvananda/netlink v1.1.0
go.uber.org/multierr v1.5.0
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200324154536-ceff61240acf
)

16
main.go
View File

@ -49,7 +49,7 @@ func main() {
wg.assignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.localName)
cluster.update()
nodec, errc := cluster.members() // avoid deadlocks by starting before join
nodec := cluster.members() // avoid deadlocks by starting before join
if err := backoff.RetryNotify(
func() error { return cluster.join(config.Join) },
backoff.NewExponentialBackOff(),
@ -65,9 +65,17 @@ func main() {
logrus.Debug("waiting for cluster events")
for {
select {
case nodes := <-nodec:
case rawNodes := <-nodec:
logrus.Info("cluster members:\n")
for _, node := range nodes {
nodes := make([]node, 0)
for _, node := range rawNodes {
meta, err := decodeNodeMeta(node.Meta)
if err != nil {
logrus.Warnf("\t addr: %s, could not decode metadata", node.Addr)
continue
}
node.nodeMeta = meta
nodes = append(nodes, node)
logrus.Infof("\taddr: %s, overlay: %s, pubkey: %s", node.Addr, node.OverlayAddr, node.PubKey)
}
if err := wg.setUpInterface(nodes); err != nil {
@ -79,8 +87,6 @@ func main() {
logrus.WithError(err).Error("could not write hosts entries")
}
}
case errs := <-errc:
logrus.WithError(errs).Error("could not receive node info")
case <-incomingSigs:
logrus.Info("terminating...")
cluster.leave()

View File

@ -19,6 +19,7 @@ type nodeMeta struct {
type node struct {
Name string
Addr net.IP
Meta []byte
nodeMeta
}