added raft over tls

This commit is contained in:
ston1th 2020-11-15 00:30:35 +01:00
commit 585baea183
16 changed files with 563 additions and 300 deletions

View file

@ -2,8 +2,15 @@ package config
import (
"errors"
"gopkg.in/yaml.v2"
"fmt"
"os"
"gopkg.in/yaml.v2"
)
const (
RaftDir = "raft"
RaftPort = 7001
)
func ParseFile(file string) (cfg *Config, err error) {
@ -16,33 +23,99 @@ func ParseFile(file string) (cfg *Config, err error) {
}
cfg = new(Config)
err = yaml.NewDecoder(f).Decode(cfg)
if err != nil {
return
}
err = Validate(cfg)
return
}
func Validate(c *Config) error {
if len(c.VirtualIPs) == 0 {
return errors.New("missing virtualIPs config")
}
raft := c.Cluster.Raft
if raft != nil {
if raft.Dir == "" {
raft.Dir = RaftDir
}
if raft.Level == "" {
raft.Level = "off"
}
if raft.ID == "" {
return errors.New("missing raft.id config")
}
if raft.Address == "" {
return errors.New("missing raft.address config")
}
if len(raft.Peers) < 2 {
return errors.New("minimum number of remote peers: 2")
}
for i, v := range raft.Peers {
if v.ID == "" {
return fmt.Errorf("missing raft.peers[%d].id config", i)
}
if v.Address == "" {
return fmt.Errorf("missing raft.peers[%d].address config", i)
}
}
}
etcd := c.Cluster.Etcd
if etcd != nil {
}
if raft == nil && etcd == nil {
return errors.New("missing cluster config: raft or etcd")
}
if raft != nil && etcd != nil {
return errors.New("only one cluster config allowed: raft or etcd")
}
return nil
}
type Config struct {
RaftDir string `yaml:"raftDir"`
VirtualIPs []string `yaml:"virtualIPs"`
Leader bool `yaml:"leader"`
AddressRange []string `yaml:"addressRange"`
// LocalPeer is the configuration of this host
LocalPeer RaftPeer `yaml:"localPeer"`
// Peers are all of the peers within the RAFT cluster
RemotePeers []RaftPeer `yaml:"remotePeers"`
// Interface is the network interface to bind to (default: First Adapter)
Interface string `yaml:"interface,omitempty"`
IPLabel string `yaml:"label,omitempty"`
LeaderHook string `yaml:"leaderHook,omitempty"`
FollowerHook string `yaml:"followerHook,omitempty"`
Cluster Cluster `yaml:"cluster"`
}
type Cluster struct {
Raft *Raft `yaml:"raft,omitempty"`
Etcd *Etcd `yaml:"etcd,omitempty"`
}
type Raft struct {
Dir string `yaml:"dir,omitempty"`
ID string `yaml:"id"`
Address string `yaml:"address"`
Level string `yaml:"level"`
Peers []RaftPeer `yaml:"peers"`
TLS *TLS `yaml:"tls,omitempty"`
}
type TLS struct {
Cert string `yaml:"cert"`
Key string `yaml:"key"`
CA string `yaml:"ca,omitempty"`
Insecure bool `yaml:"insecure,omitempty"`
}
// RaftPeer details the configuration of all cluster peers
type RaftPeer struct {
ID string `yaml:"id"`
// IP Address of a peer instance
ID string `yaml:"id"`
Address string `yaml:"address"`
// Listening port of this peer instance
Port int `yaml:"port"`
}
type Etcd struct {
Endpoints []string `yaml:"endpoints"`
Prefix string `yaml:"prefix"`
ClusterName string `yaml:"clusterName"`
}