added netbox allocator

This commit is contained in:
ston1th 2021-10-12 21:45:19 +02:00
commit cef74df3e8
10 changed files with 195 additions and 53 deletions

View file

@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strings"
@ -13,7 +14,7 @@ import (
const (
RaftDir = "raft"
RaftPort = 7001
RaftListen = ":7001"
RaftLogLevel = "off"
EtcdLogLevel = "error"
@ -48,9 +49,19 @@ func Validate(c *Config) error {
if c.HAProxyService == "" {
c.HAProxyService = HAProxyService
}
if c.VIP.Prefix == "" {
return errors.New("missing server.prefix config")
failover := c.Failover
if failover != nil {
if failover.IP == "" {
return errors.New("missing failover.ip config")
}
_, _, err := net.ParseCIDR(failover.IP)
if err != nil {
return fmt.Errorf("failed to parse failover.ip: %w", err)
}
}
if c.LeaderHook != "" {
if !filepath.IsAbs(c.LeaderHook) {
return errors.New("leaderHook path must be absolute")
@ -61,6 +72,28 @@ func Validate(c *Config) error {
return errors.New("followerHook path must be absolute")
}
}
vip := c.VIP
if vip.Prefix == "" {
return errors.New("missing vip.prefix config")
}
local := vip.Store.Local
netbox := vip.Store.Netbox
if local == nil && netbox == nil {
return errors.New("missing vip.store config: local or netbox")
}
if local != nil && netbox != nil {
return errors.New("only one vip.store config allowed: local or netbox")
}
if netbox != nil {
if netbox.Endpoint == "" {
return errors.New("missing vip.store.netbox.endpoint config")
}
if netbox.Token == "" {
return errors.New("missing vip.store.netbox.token config")
}
}
raft := c.Cluster.Raft
if raft != nil {
if raft.LogLevel == "" {
@ -72,8 +105,8 @@ func Validate(c *Config) error {
if raft.ID == "" {
return errors.New("missing raft.id config")
}
if raft.Address == "" {
return errors.New("missing raft.address config")
if raft.Listen == "" {
return errors.New("missing raft.listen config")
}
if len(raft.Peers) < 2 {
return errors.New("minimum number of remote peers: 2")
@ -119,10 +152,10 @@ func Validate(c *Config) error {
if raft != nil && etcd != nil {
return errors.New("only one cluster config allowed: raft or etcd")
}
if len(c.Server.BasicAuth) == 0 {
if len(c.APIServer.BasicAuth) == 0 {
return errors.New("missing basic auth configuration for api users")
}
for _, u := range c.Server.BasicAuth {
for _, u := range c.APIServer.BasicAuth {
if u.User == "" {
return errors.New("basic auth username can not be empty")
}
@ -135,31 +168,37 @@ func Validate(c *Config) error {
}
type Config struct {
LeaderHook string `yaml:"leaderHook,omitempty"`
FollowerHook string `yaml:"followerHook,omitempty"`
HAProxyConfig string `yaml:"haproxyConfig,omitempty"`
HAProxyService string `yaml:"haproxyService,omitempty"`
VIP VIP `yaml:"vip"`
Cluster Cluster `yaml:"cluster"`
Server Server `yaml:"server"`
LeaderHook string `yaml:"leaderHook,omitempty"`
FollowerHook string `yaml:"followerHook,omitempty"`
HAProxyConfig string `yaml:"haproxyConfig,omitempty"`
HAProxyService string `yaml:"haproxyService,omitempty"`
Failover *Failover `yaml:"failover"`
VIP VIP `yaml:"vip"`
Cluster Cluster `yaml:"cluster"`
APIServer APIServer `yaml:"apiServer"`
}
type Server struct {
Listen string `yaml:"listen"`
TLS *TLS `yaml:"tls"`
BasicAuth []BasicAuth `yaml:"basicAuth"`
type Failover struct {
IP string `yaml:"ip"`
Interface string `yaml:"interface"`
}
type VIP struct {
Prefix string `yaml:"prefix"`
Store Store `yaml:"store"`
Interface string `yaml:"interface,omitempty"`
Label string `yaml:"label,omitempty"`
}
type BasicAuth struct {
User string `yaml:"user"`
Hash string `yaml:"hash"`
Prefix string `yaml:"prefix"`
type Store struct {
Local *struct{} `yaml:"local"`
Netbox *Netbox `yaml:"netbox"`
}
type Netbox struct {
Endpoint string `yaml:"endpoint"`
Token string `yaml:"token"`
Insecure bool `yaml:"insecure,omitempty"`
}
type Cluster struct {
@ -170,7 +209,7 @@ type Cluster struct {
type Raft struct {
Dir string `yaml:"dir,omitempty"`
ID string `yaml:"id"`
Address string `yaml:"address"`
Listen string `yaml:"listen"`
Peers []RaftPeer `yaml:"peers"`
TLS *TLS `yaml:"tls,omitempty"`
LogLevel string `yaml:"logLevel,omitempty"`
@ -191,10 +230,22 @@ type RaftPeer struct {
type Etcd struct {
Endpoints []string `yaml:"endpoints"`
Prefix string `yaml:"prefix"`
ClusterName string `yaml:"clusterName"`
Prefix string `yaml:"prefix,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
TLS *TLS `yaml:"tls,omitempty"`
LogLevel string `yaml:"logLevel,omitempty"`
}
type APIServer struct {
Listen string `yaml:"listen"`
TLS *TLS `yaml:"tls"`
BasicAuth []BasicAuth `yaml:"basicAuth"`
}
type BasicAuth struct {
User string `yaml:"user"`
Hash string `yaml:"hash"`
Prefix string `yaml:"prefix"`
}