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.
This commit is contained in:
kaiyou 2020-05-06 19:14:33 +02:00 committed by Leo Antunes
parent 0d93439d0d
commit 97525f4b10
3 changed files with 24 additions and 12 deletions

View File

@ -27,14 +27,14 @@ type ClusterState struct {
type cluster struct { type cluster struct {
localName string // used to avoid LocalNode(); should not change localName string // used to avoid LocalNode(); should not change
ml *memberlist.Memberlist ml *memberlist.Memberlist
wg *wgState getMeta func(int) []byte
state *ClusterState state *ClusterState
events chan memberlist.NodeEvent events chan memberlist.NodeEvent
} }
const statePath = "/var/lib/wesher/state.json" 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 clusterKey := config.ClusterKey
state := &ClusterState{} state := &ClusterState{}
@ -70,7 +70,7 @@ func newCluster(config *config, wg *wgState) (*cluster, error) {
cluster := cluster{ cluster := cluster{
localName: ml.LocalNode().Name, localName: ml.LocalNode().Name,
ml: ml, ml: ml,
wg: wg, getMeta: getMeta,
// The big channel buffer is a work-around for https://github.com/hashicorp/memberlist/issues/23 // 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() // More than this many simultaneous events will deadlock cluster.members()
events: make(chan memberlist.NodeEvent, 100), 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.Events = &memberlist.ChannelEventDelegate{Ch: cluster.events}
mlConfig.Delegate = &cluster 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 return &cluster, nil
} }
@ -90,6 +87,10 @@ func (c *cluster) NotifyConflict(node, other *memberlist.Node) {
logrus.Errorf("node name conflict detected: %s", other.Name) 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 // none of these are used
func (c *cluster) NotifyMsg([]byte) {} func (c *cluster) NotifyMsg([]byte) {}
func (c *cluster) GetBroadcasts(overhead, limit int) [][]byte { return nil } func (c *cluster) GetBroadcasts(overhead, limit int) [][]byte { return nil }
@ -117,6 +118,10 @@ func (c *cluster) leave() {
c.ml.Shutdown() //nolint: errcheck 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) { func (c *cluster) members() (<-chan []node, <-chan error) {
changes := make(chan []node) changes := make(chan []node)
errc := make(chan error, 1) errc := make(chan error, 1)

12
main.go
View File

@ -2,6 +2,7 @@ package main // import "github.com/costela/wesher"
import ( import (
"fmt" "fmt"
"net"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@ -34,10 +35,19 @@ func main() {
logrus.WithError(err).Fatal("could not instantiate wireguard controller") 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 { if err != nil {
logrus.WithError(err).Fatal("could not create cluster") 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 nodec, errc := cluster.members() // avoid deadlocks by starting before join
if err := backoff.RetryNotify( if err := backoff.RetryNotify(

View File

@ -26,12 +26,9 @@ func (n *node) String() string {
return n.Addr.String() return n.Addr.String()
} }
func (c *cluster) NodeMeta(limit int) []byte { func encodeNodeMeta(nm nodeMeta, limit int) []byte {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
if err := gob.NewEncoder(buf).Encode(nodeMeta{ if err := gob.NewEncoder(buf).Encode(nm); err != nil {
OverlayAddr: c.wg.OverlayAddr,
PubKey: c.wg.PubKey.String(),
}); err != nil {
logrus.Errorf("could not encode local state: %s", err) logrus.Errorf("could not encode local state: %s", err)
return nil return nil
} }