Skip to content
Snippets Groups Projects
Commit 45e4e373 authored by kaiyou's avatar kaiyou
Browse files

Use a pflag type for node role

parent a90bdcbb
No related branches found
No related tags found
No related merge requests found
...@@ -64,5 +64,5 @@ func init() { ...@@ -64,5 +64,5 @@ func init() {
rootCmd.Flags().IntVar(&config.Node.Port, "discovery-port", 7123, "TCP port used for discovering the cluster") rootCmd.Flags().IntVar(&config.Node.Port, "discovery-port", 7123, "TCP port used for discovering the cluster")
rootCmd.Flags().StringVar(&config.Node.Name, "name", "", "Hepto node name") rootCmd.Flags().StringVar(&config.Node.Name, "name", "", "Hepto node name")
rootCmd.Flags().StringSliceVar(&config.Node.Anchors, "anchors", []string{}, "List of cluster anchors") rootCmd.Flags().StringSliceVar(&config.Node.Anchors, "anchors", []string{}, "List of cluster anchors")
rootCmd.Flags().StringVar(&config.Node.Role, "role", "node", "Node role inside the cluster") rootCmd.Flags().Var(&config.Node.Role, "role", "Node role inside the cluster")
} }
...@@ -3,11 +3,14 @@ ...@@ -3,11 +3,14 @@
package cluster package cluster
import ( import (
"context"
"net" "net"
"forge.tedomum.net/acides/hepto/hepto/pkg/sml" "forge.tedomum.net/acides/hepto/hepto/pkg/sml"
"forge.tedomum.net/acides/hepto/hepto/pkg/wg" "forge.tedomum.net/acides/hepto/hepto/pkg/wg"
"forge.tedomum.net/acides/hepto/hepto/pkg/wrappers"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"go.etcd.io/etcd/server/v3/embed"
) )
type Cluster struct { type Cluster struct {
...@@ -35,7 +38,7 @@ func New(settings *ClusterSettings, node *NodeSettings) (*Cluster, error) { ...@@ -35,7 +38,7 @@ func New(settings *ClusterSettings, node *NodeSettings) (*Cluster, error) {
} }
c.vpn = vpn c.vpn = vpn
// Initialize cluster PKI and local keys // Initialize cluster PKI and local keys
if node.Role == "master" { if node.Role == Master {
pki, err := NewClusterPKI("pki") pki, err := NewClusterPKI("pki")
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -51,7 +54,7 @@ func New(settings *ClusterSettings, node *NodeSettings) (*Cluster, error) { ...@@ -51,7 +54,7 @@ func New(settings *ClusterSettings, node *NodeSettings) (*Cluster, error) {
c.certs = certs c.certs = certs
// Initialize node meta // Initialize node meta
c.ml.Meta.VpnKey = vpn.PubKey.String() c.ml.Meta.VpnKey = vpn.PubKey.String()
c.ml.Meta.Role = node.Role c.ml.Meta.Role = string(node.Role)
// Initialize cluster state // Initialize cluster state
c.ml.State.PKI = c.pki c.ml.State.PKI = c.pki
c.ml.State.Certificates = make(map[string]*NodeCerts) c.ml.State.Certificates = make(map[string]*NodeCerts)
...@@ -71,10 +74,11 @@ func (c *Cluster) Run() error { ...@@ -71,10 +74,11 @@ func (c *Cluster) Run() error {
for { for {
select { select {
case <-events: case <-events:
if c.node.Role == "master" { if c.node.Role == Master {
c.handlePKI() c.handlePKI()
} }
c.updateVPN() c.updateVPN()
c.updateServices()
case <-instrUpdates: case <-instrUpdates:
c.networking.MTU = instr.MinMTU() c.networking.MTU = instr.MinMTU()
c.updateVPN() c.updateVPN()
...@@ -107,3 +111,15 @@ func (c *Cluster) updateVPN() { ...@@ -107,3 +111,15 @@ func (c *Cluster) updateVPN() {
logrus.Debugf("updating VPN mesh, %d peers, MTU %d", len(peers), c.networking.MTU) logrus.Debugf("updating VPN mesh, %d peers, MTU %d", len(peers), c.networking.MTU)
c.vpn.Update(peers, c.networking.MTU) c.vpn.Update(peers, c.networking.MTU)
} }
func (c *Cluster) updateServices() {
ctx := context.Background()
if c.node.Role == Master {
etcdConfig := embed.NewConfig()
etcdConfig.Dir = "/etcd"
_, err := wrappers.ETCd(ctx, etcdConfig)
if err != nil {
logrus.Fatal(err)
}
}
}
package cluster package cluster
import ( import (
"errors"
"net" "net"
"forge.tedomum.net/acides/hepto/hepto/pkg/types" "forge.tedomum.net/acides/hepto/hepto/pkg/types"
...@@ -16,8 +17,8 @@ type ClusterSettings struct { ...@@ -16,8 +17,8 @@ type ClusterSettings struct {
type NodeSettings struct { type NodeSettings struct {
// Node name, must be unique inside a cluster // Node name, must be unique inside a cluster
Name string Name string
// Node role, TODO: switch to proper types // Node role
Role string Role NodeRole
// Port for initial memberlist negotiations // Port for initial memberlist negotiations
Port int Port int
// Public IPv6 address for the node // Public IPv6 address for the node
...@@ -26,6 +27,30 @@ type NodeSettings struct { ...@@ -26,6 +27,30 @@ type NodeSettings struct {
Anchors []string Anchors []string
} }
type NodeRole string
const (
Master NodeRole = "master"
Node = "node"
)
func (r *NodeRole) String() string {
return string(*r)
}
func (r *NodeRole) Type() string {
return "NodeRole"
}
func (r *NodeRole) Set(v string) error {
cast := (NodeRole)(v)
if cast == Master || cast == Node {
*r = (NodeRole)(cast)
return nil
}
return errors.New("wrong node type")
}
type ClusterNetworking struct { type ClusterNetworking struct {
NodeNet types.Address NodeNet types.Address
NodeAddress types.Address NodeAddress types.Address
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment