From 97525f4b105c573495b10aeac0a1a28fb9489e6b Mon Sep 17 00:00:00 2001 From: kaiyou Date: Wed, 6 May 2020 19:14:33 +0200 Subject: [PATCH] Stop using wireguard in cluster.go The wireguard is mostly used to compute metadata. Metadata is now computed by main.go and encoded in node.go, the cluster only receives a function generating the binary metadata. --- cluster.go | 17 +++++++++++------ main.go | 12 +++++++++++- node.go | 7 ++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cluster.go b/cluster.go index 86bd976..0e1c428 100644 --- a/cluster.go +++ b/cluster.go @@ -27,14 +27,14 @@ type ClusterState struct { type cluster struct { localName string // used to avoid LocalNode(); should not change ml *memberlist.Memberlist - wg *wgState + getMeta func(int) []byte state *ClusterState events chan memberlist.NodeEvent } const statePath = "/var/lib/wesher/state.json" -func newCluster(config *config, wg *wgState) (*cluster, error) { +func newCluster(config *config, getMeta func(int) []byte) (*cluster, error) { clusterKey := config.ClusterKey state := &ClusterState{} @@ -70,7 +70,7 @@ func newCluster(config *config, wg *wgState) (*cluster, error) { cluster := cluster{ localName: ml.LocalNode().Name, ml: ml, - wg: wg, + getMeta: getMeta, // The big channel buffer is a work-around for https://github.com/hashicorp/memberlist/issues/23 // More than this many simultaneous events will deadlock cluster.members() events: make(chan memberlist.NodeEvent, 100), @@ -80,9 +80,6 @@ func newCluster(config *config, wg *wgState) (*cluster, error) { mlConfig.Events = &memberlist.ChannelEventDelegate{Ch: cluster.events} mlConfig.Delegate = &cluster - wg.assignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.localName) - - ml.UpdateNode(1 * time.Second) // we currently do not update after creation return &cluster, nil } @@ -90,6 +87,10 @@ func (c *cluster) NotifyConflict(node, other *memberlist.Node) { logrus.Errorf("node name conflict detected: %s", other.Name) } +func (c *cluster) NodeMeta(limit int) []byte { + return c.getMeta(limit) +} + // none of these are used func (c *cluster) NotifyMsg([]byte) {} func (c *cluster) GetBroadcasts(overhead, limit int) [][]byte { return nil } @@ -117,6 +118,10 @@ func (c *cluster) leave() { c.ml.Shutdown() //nolint: errcheck } +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) { changes := make(chan []node) errc := make(chan error, 1) diff --git a/main.go b/main.go index 922ef5f..572d407 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main // import "github.com/costela/wesher" import ( "fmt" + "net" "os" "os/signal" "syscall" @@ -34,10 +35,19 @@ func main() { logrus.WithError(err).Fatal("could not instantiate wireguard controller") } - cluster, err := newCluster(config, wg) + getMeta := func(limit int) []byte { + return encodeNodeMeta(nodeMeta{ + OverlayAddr: wg.OverlayAddr, + PubKey: wg.PubKey.String(), + }, limit) + } + + cluster, err := newCluster(config, getMeta) if err != nil { logrus.WithError(err).Fatal("could not create cluster") } + wg.assignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.localName) + cluster.update() nodec, errc := cluster.members() // avoid deadlocks by starting before join if err := backoff.RetryNotify( diff --git a/node.go b/node.go index 16a4da4..8fc75a7 100644 --- a/node.go +++ b/node.go @@ -26,12 +26,9 @@ func (n *node) String() string { return n.Addr.String() } -func (c *cluster) NodeMeta(limit int) []byte { +func encodeNodeMeta(nm nodeMeta, limit int) []byte { buf := &bytes.Buffer{} - if err := gob.NewEncoder(buf).Encode(nodeMeta{ - OverlayAddr: c.wg.OverlayAddr, - PubKey: c.wg.PubKey.String(), - }); err != nil { + if err := gob.NewEncoder(buf).Encode(nm); err != nil { logrus.Errorf("could not encode local state: %s", err) return nil }