migrated to external IP allocator

This commit is contained in:
ston1th 2021-09-26 20:20:43 +02:00
commit e2449b425e
15 changed files with 478 additions and 194 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
)
/*
@ -16,8 +17,15 @@ const Version = "v1"
type LoadBalancer struct {
Name string `json:"name,omitempty"`
IP string `json:"ip,omitempty"`
CIDR string `json:"cidr,omitempty"`
Options Options `json:"options,omitempty"`
Ports []Port `json:"ports,omitempty"`
Ports Ports `json:"ports,omitempty"`
}
func NewLoadBalancerFromBytes(b []byte) (lb *LoadBalancer, err error) {
lb = new(LoadBalancer)
err = json.Unmarshal(b, lb)
return
}
type Options struct {
@ -29,16 +37,83 @@ type Options struct {
}
type Port struct {
Port int `json:"port,omitempty"`
Servers []Server `json:"servers,omitempty"`
Port int `json:"port,omitempty"`
Servers Servers `json:"servers,omitempty"`
}
type Ports []Port
func (p Ports) Len() int { return len(p) }
func (p Ports) Less(i, j int) bool { return p[i].Port < p[j].Port }
func (p Ports) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type Server struct {
Name string `json:"name,omitempty"`
IP string `json:"ip,omitempty"`
Port int `json:"port,omitempty"`
}
type Servers []Server
func (s Servers) Len() int { return len(s) }
func (s Servers) Less(i, j int) bool { return s[i].Name < s[j].Name }
func (s Servers) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (o Servers) Equal(n Servers) bool {
if len(o) != len(n) {
return false
}
for i, v := range o {
if v.Name != n[i].Name ||
v.IP != n[i].IP ||
v.Port != n[i].Port {
return false
}
}
return true
}
func (o Servers) ContainsIP(ip string) bool {
return o.IndexOfIP(ip) != -1
}
func (o Servers) IndexOfIP(ip string) int {
for i, v := range o {
if v.IP == ip {
return i
}
}
return -1
}
func (o Servers) RemoveByIP(ip string) Servers {
index := o.IndexOfIP(ip)
if index == -1 {
return o
}
return append(o[:index], o[index+1:]...)
}
func (o Servers) ContainsName(name string) bool {
return o.IndexOfName(name) != -1
}
func (o Servers) IndexOfName(name string) int {
for i, v := range o {
if v.Name == name {
return i
}
}
return -1
}
func (o Servers) RemoveByName(name string) Servers {
index := o.IndexOfName(name)
if index == -1 {
return o
}
return append(o[:index], o[index+1:]...)
}
func (lb LoadBalancer) ValidateClient() error {
for i, p := range lb.Ports {
if p.Port < 0 || p.Port > 65535 {
@ -66,7 +141,18 @@ func (lb LoadBalancer) ValidateServer() error {
if lb.IP == "" {
return errors.New("LoadBalancer.IP can not be empty")
}
return lb.ValidateClient()
if lb.CIDR == "" {
return errors.New("LoadBalancer.CIDR can not be empty")
}
err := lb.ValidateClient()
if err != nil {
return err
}
sort.Sort(lb.Ports)
for _, p := range lb.Ports {
sort.Sort(p.Servers)
}
return nil
}
func (lb LoadBalancer) JSON() ([]byte, error) {