added netbox allocator
This commit is contained in:
parent
fef86deac8
commit
cef74df3e8
10 changed files with 195 additions and 53 deletions
|
|
@ -13,6 +13,7 @@ import (
|
|||
"git.giftfish.de/ston1th/haproxy-lb/pkg/db"
|
||||
|
||||
"git.giftfish.de/ston1th/netalloc"
|
||||
"git.giftfish.de/ston1th/netalloc/netbox"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/gorilla/mux"
|
||||
|
|
@ -25,7 +26,6 @@ type Server struct {
|
|||
log logr.Logger
|
||||
|
||||
Data *types.ContextData
|
||||
cidr string
|
||||
|
||||
init chan struct{}
|
||||
stop chan struct{}
|
||||
|
|
@ -33,6 +33,9 @@ type Server struct {
|
|||
|
||||
tlsConfig *tls.Config
|
||||
listen net.Listener
|
||||
|
||||
prefix string
|
||||
store config.Store
|
||||
}
|
||||
|
||||
type notFoundHandler struct {
|
||||
|
|
@ -48,17 +51,19 @@ func NewServer(c *config.Config, log logr.Logger) (*Server, error) {
|
|||
s := &Server{
|
||||
mux: mux.NewRouter(),
|
||||
log: log,
|
||||
cidr: c.VIP.Prefix,
|
||||
Data: &types.ContextData{},
|
||||
|
||||
init: make(chan struct{}),
|
||||
stop: make(chan struct{}),
|
||||
stopKeyReset: make(chan struct{}),
|
||||
}
|
||||
s.Data.Auth = types.NewAuth(c.Server.BasicAuth)
|
||||
|
||||
if c.Server.TLS != nil {
|
||||
cert, err := tls.LoadX509KeyPair(c.Server.TLS.Cert, c.Server.TLS.Key)
|
||||
prefix: c.VIP.Prefix,
|
||||
store: c.VIP.Store,
|
||||
}
|
||||
s.Data.Auth = types.NewAuth(c.APIServer.BasicAuth)
|
||||
|
||||
if c.APIServer.TLS != nil {
|
||||
cert, err := tls.LoadX509KeyPair(c.APIServer.TLS.Cert, c.APIServer.TLS.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -83,13 +88,13 @@ func NewServer(c *config.Config, log logr.Logger) (*Server, error) {
|
|||
PreferServerCipherSuites: true,
|
||||
Certificates: []tls.Certificate{cert},
|
||||
}
|
||||
l, err := tls.Listen("tcp", c.Server.Listen, s.tlsConfig)
|
||||
l, err := tls.Listen("tcp", c.APIServer.Listen, s.tlsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.listen = l
|
||||
} else {
|
||||
l, err := net.Listen("tcp", c.Server.Listen)
|
||||
l, err := net.Listen("tcp", c.APIServer.Listen)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -107,15 +112,32 @@ func NewServer(c *config.Config, log logr.Logger) (*Server, error) {
|
|||
func (s *Server) UpdateDB(db *db.DB, leader bool) error {
|
||||
s.Data.DB = db
|
||||
s.Data.Leader = leader
|
||||
if s.Data.Net == nil {
|
||||
na, err := netalloc.NewGenericAlloc(db, s.cidr)
|
||||
if err != nil {
|
||||
return err
|
||||
if s.store.Local != nil {
|
||||
if s.Data.Net == nil {
|
||||
na, err := netalloc.NewGenericAlloc(db, s.prefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.Data.Net = na
|
||||
close(s.init)
|
||||
} else {
|
||||
s.Data.Net.SetStore(db)
|
||||
}
|
||||
s.Data.Net = na
|
||||
close(s.init)
|
||||
} else {
|
||||
s.Data.Net.SetStore(db)
|
||||
if s.Data.Net == nil {
|
||||
nb := s.store.Netbox
|
||||
na, err := netbox.NewAllocator(
|
||||
netbox.WithEndpoint(nb.Endpoint),
|
||||
netbox.WithToken(nb.Token),
|
||||
netbox.WithPrefix(s.prefix),
|
||||
netbox.WithInsecureClient(nb.Insecure),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.Data.Net = na
|
||||
close(s.init)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,14 @@ import (
|
|||
)
|
||||
|
||||
func NewLBController(cfg *config.Config, srv *api.Server, log logr.Logger) (callbacks cluster.Callbacks, err error) {
|
||||
var fo *vip.Network
|
||||
if cfg.Failover != nil {
|
||||
fo, err = vip.NewNetwork(cfg.Failover.Interface)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
n, err := vip.NewNetworkWithLabel(cfg.VIP.Interface, cfg.VIP.Label)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -57,6 +65,9 @@ func NewLBController(cfg *config.Config, srv *api.Server, log logr.Logger) (call
|
|||
return
|
||||
}
|
||||
log.Info("leading", "id", cc.ID())
|
||||
if cfg.Failover != nil {
|
||||
addIPs(log, fo, []string{cfg.Failover.IP})
|
||||
}
|
||||
ips, err := db.GetDeletedIPs()
|
||||
if err != nil && err != cluster.ErrPrefixNotFound {
|
||||
log.Error(err, "error reading deleted ips list")
|
||||
|
|
@ -150,6 +161,9 @@ func NewLBController(cfg *config.Config, srv *api.Server, log logr.Logger) (call
|
|||
}
|
||||
},
|
||||
Cleanup: func(ctx context.Context, cc cluster.CallbackContext) {
|
||||
if cfg.Failover != nil {
|
||||
deleteIPs(log, fo, fo.GetIPs())
|
||||
}
|
||||
deleteIPs(log, n, n.GetIPs())
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,11 +48,11 @@ func (c *Cluster) Start(raftcfg *config.Config) error {
|
|||
transport = raft.NewNetworkTransportWithLogger(s, pool, timeout, hcl)
|
||||
} else {
|
||||
log.V(1).Info("setup", "transport", "tcp")
|
||||
address, err := net.ResolveTCPAddr("tcp", cfg.Address)
|
||||
address, err := net.ResolveTCPAddr("tcp", cfg.Listen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
transport, err = raft.NewTCPTransportWithLogger(cfg.Address, address, pool, timeout, hcl)
|
||||
transport, err = raft.NewTCPTransportWithLogger(cfg.Listen, address, pool, timeout, hcl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -85,10 +85,10 @@ func (c *Cluster) Start(raftcfg *config.Config) error {
|
|||
// Add Local Peer
|
||||
configuration.Servers = append(configuration.Servers, raft.Server{
|
||||
ID: raft.ServerID(cfg.ID),
|
||||
Address: raft.ServerAddress(cfg.Address),
|
||||
Address: raft.ServerAddress(cfg.Listen),
|
||||
})
|
||||
for _, p := range cfg.Peers {
|
||||
if cfg.Address != p.Address {
|
||||
if cfg.Listen != p.Address {
|
||||
configuration.Servers = append(configuration.Servers, raft.Server{
|
||||
ID: raft.ServerID(p.ID),
|
||||
Address: raft.ServerAddress(p.Address)})
|
||||
|
|
@ -113,7 +113,7 @@ func (c *Cluster) Start(raftcfg *config.Config) error {
|
|||
leading := false
|
||||
following := false
|
||||
for {
|
||||
if !leading && cfg.Address == string(c.r.Leader()) {
|
||||
if !leading && cfg.Listen == string(c.r.Leader()) {
|
||||
leading = true
|
||||
go c.leader(ctx)
|
||||
} else if !following && !leading {
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ func NewStream(cfg *config.Raft) (s *stream, err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
l, err := tls.Listen("tcp", cfg.Address, &tls.Config{
|
||||
l, err := tls.Listen("tcp", cfg.Listen, &tls.Config{
|
||||
MinVersion: tls.VersionTLS13,
|
||||
CipherSuites: ciphers,
|
||||
CurvePreferences: curves,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue