Move node related stuff to node.go
The cornerstone for exchanging data is the node structure and associated metadata. Moving it to a separate file (maybe later a separate module) will help decoupling.
This commit is contained in:
parent
5772640cf9
commit
0d93439d0d
43
cluster.go
43
cluster.go
|
@ -1,10 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/gob"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -98,37 +96,6 @@ func (c *cluster) GetBroadcasts(overhead, limit int) [][]byte { return nil }
|
||||||
func (c *cluster) LocalState(join bool) []byte { return nil }
|
func (c *cluster) LocalState(join bool) []byte { return nil }
|
||||||
func (c *cluster) MergeRemoteState(buf []byte, join bool) {}
|
func (c *cluster) MergeRemoteState(buf []byte, join bool) {}
|
||||||
|
|
||||||
type nodeMeta struct {
|
|
||||||
OverlayAddr net.IPNet
|
|
||||||
PubKey string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cluster) 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 {
|
|
||||||
logrus.Errorf("could not encode local state: %s", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if buf.Len() > limit {
|
|
||||||
logrus.Errorf("could not fit node metadata into %d bytes", limit)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return buf.Bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeNodeMeta(b []byte) (nodeMeta, error) {
|
|
||||||
// TODO: we blindly trust the info we get from the peers; We should be more defensive to limit the damage a leaked
|
|
||||||
// PSK can cause.
|
|
||||||
nm := nodeMeta{}
|
|
||||||
if err := gob.NewDecoder(bytes.NewReader(b)).Decode(&nm); err != nil {
|
|
||||||
return nm, errors.Wrap(err, "could not decode node meta")
|
|
||||||
}
|
|
||||||
return nm, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cluster) join(addrs []string) error {
|
func (c *cluster) join(addrs []string) error {
|
||||||
if len(addrs) == 0 {
|
if len(addrs) == 0 {
|
||||||
for _, n := range c.state.Nodes {
|
for _, n := range c.state.Nodes {
|
||||||
|
@ -197,16 +164,6 @@ func (c *cluster) members() (<-chan []node, <-chan error) {
|
||||||
return changes, errc
|
return changes, errc
|
||||||
}
|
}
|
||||||
|
|
||||||
type node struct {
|
|
||||||
Name string
|
|
||||||
Addr net.IP
|
|
||||||
nodeMeta
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *node) String() string {
|
|
||||||
return n.Addr.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func computeClusterKey(state *ClusterState, clusterKey []byte) ([]byte, error) {
|
func computeClusterKey(state *ClusterState, clusterKey []byte) ([]byte, error) {
|
||||||
if len(clusterKey) == 0 {
|
if len(clusterKey) == 0 {
|
||||||
clusterKey = state.ClusterKey
|
clusterKey = state.ClusterKey
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// nodeMeta holds metadata sent over the cluster
|
||||||
|
type nodeMeta struct {
|
||||||
|
OverlayAddr net.IPNet
|
||||||
|
PubKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Node holds the memberlist node structure
|
||||||
|
type node struct {
|
||||||
|
Name string
|
||||||
|
Addr net.IP
|
||||||
|
nodeMeta
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) String() string {
|
||||||
|
return n.Addr.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cluster) 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 {
|
||||||
|
logrus.Errorf("could not encode local state: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if buf.Len() > limit {
|
||||||
|
logrus.Errorf("could not fit node metadata into %d bytes", limit)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return buf.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeNodeMeta(b []byte) (nodeMeta, error) {
|
||||||
|
// TODO: we blindly trust the info we get from the peers; We should be more defensive to limit the damage a leaked
|
||||||
|
// PSK can cause.
|
||||||
|
nm := nodeMeta{}
|
||||||
|
if err := gob.NewDecoder(bytes.NewReader(b)).Decode(&nm); err != nil {
|
||||||
|
return nm, errors.Wrap(err, "could not decode node meta")
|
||||||
|
}
|
||||||
|
return nm, nil
|
||||||
|
}
|
Loading…
Reference in New Issue