initial commit

This commit is contained in:
ston1th 2020-11-22 12:59:12 +01:00
commit 7715fcf373
37 changed files with 2957 additions and 0 deletions

67
pkg/haproxy/haproxy.go Normal file
View file

@ -0,0 +1,67 @@
package haproxy
import (
"context"
"io/ioutil"
"os"
"sync"
"text/template"
)
type ServiceManager interface {
Reload()
}
type HAProxyManager struct {
sync.Mutex
configFile string
serviceManager ServiceManager
template *template.Template
}
func NewHAProxyManager(configFile string) (*HAProxyManager, error) {
t, err := template.New("haproxy").Parse(haproxyTemplate)
if err != nil {
return nil, err
}
return &HAProxyManager{
configFile: configFile,
serviceManager: NewSystemdManager("haproxy"),
template: t,
}, nil
}
func (ha *HAProxyManager) checkConfig(ctx context.Context, configFile string) error {
return exec.CommandContext(ctx, "haproxy", "-c", "-f", configFile).Run()
}
func (ha *HAProxyManager) UpdateConfig(ctx context.Context, lbs []config.LoadBalancer) error {
ha.Lock()
defer ha.Unlock()
tmp, err := ioutil.TempFile(os.TempDir(), "haproxy_")
if err != nil {
return err
}
defer tmp.Close()
defer os.Remove(tmp.Name())
err = ha.template.Execute(tmp, lbs)
if err != nil {
return err
}
err = ha.checkConfig(ctx, tmp.Name())
if err != nil {
return err
}
file, err := os.OpenFile(ha.configFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
err = ha.template.Execute(file, lbs)
if err != nil {
file.Close()
return err
}
file.Close()
return ha.serviceManager.Reload()
}

20
pkg/haproxy/systemd.go Normal file
View file

@ -0,0 +1,20 @@
package haproxy
import (
"context"
"os/exec"
)
const systemctl = "systemctl"
type SystemdManager struct {
service string
}
func NewSystemdManager(service string) *SystemdManager {
return &SystemdManager{service}
}
func (sm *SystemdManager) Reload(ctx context.Context) error {
return exec.CommandContext(ctx, systemctl, "reload", sm.service).Run()
}

43
pkg/haproxy/template.go Normal file
View file

@ -0,0 +1,43 @@
package haproxy
const haproxyConfigTemplate = `global
daemon
chroot /var/lib/haproxy
user haproxy
group haproxy
maxconn 5000
defaults
mode tcp
log global
retries 2
timeout connect 3000ms
timeout server 5000ms
timeout client 5000ms
timeout tunnel 3600s
timeout check 10s
{{ range $item := . -}}
{{ range $port := $item.Ports -}}
frontend fr_{{ $item.Name }}_{{ $port.Port }}
bind {{ $item.IP }}:{{ $port.Port }}
{{ range $opt := $item.Options.Frontend -}}
option {{ $opt }}
{{ end -}}
# option tcplog
# option splice-request
default_backend ba_{{ $item.Name }}_{{ $port.Port }}
backend ba_{{ $item.Name }}_{{ $port.Port }}
balance roundrobin
{{ range $opt := $item.Options.Backend -}}
option {{ $opt }}
{{ end -}}
# option splice-response
# option httpchk GET /
default-server inter 3s downinter 10s fall 2 rise 2 on-marked-down shutdown-sessions {{ $item.Options.DefaultServer }}
# send-proxy-v2
# check-send-proxy
# port <healthCheckNodePort>
{{ range $srv := $port.Servers -}}
server {{ $srv.Name }} {{ $srv.IP }}:{{ $srv.Port }} check
{{ end -}}
{{ end -}}
{{ end -}}`