implemented api handlers
This commit is contained in:
parent
ab12dd67d4
commit
372915e24d
15 changed files with 302 additions and 73 deletions
|
|
@ -1,29 +1,43 @@
|
|||
package haproxy
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Config struct {
|
||||
LoadBalancers []LoadBalancer
|
||||
LoadBalancers []*LoadBalancer
|
||||
}
|
||||
|
||||
func NewConfig(m map[string][]byte) (c Config, err error) {
|
||||
for _, v := range m {
|
||||
lb := new(LoadBalancer)
|
||||
err = json.Unmarshal(v, lb)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
c.LoadBalancers = append(c.LoadBalancers, lb)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type LoadBalancer struct {
|
||||
Name string
|
||||
IP string
|
||||
Options Options
|
||||
Ports []Port
|
||||
Name string `json:"name"`
|
||||
IP string `json:"ip"`
|
||||
Options Options `json:"options"`
|
||||
Ports []Port `json:"ports"`
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Frontend []string
|
||||
Backend []string
|
||||
DefaultServer string
|
||||
Frontend []string `json:"frontend"`
|
||||
Backend []string `json:"backend"`
|
||||
DefaultServer string `json:"defaultServer"`
|
||||
}
|
||||
|
||||
type Port struct {
|
||||
Port int
|
||||
Servers []Server
|
||||
Port int `json:"port"`
|
||||
Servers []Server `json:"servers"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Name string
|
||||
IP string
|
||||
Port int
|
||||
Name string `json:"name"`
|
||||
IP string `json:"ip"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
|
@ -51,7 +52,11 @@ func (ha *HAProxyManager) checkConfig(ctx context.Context, lbs Config) (hash []b
|
|||
return
|
||||
}
|
||||
hash = h.Sum(nil)
|
||||
return hash, exec.CommandContext(ctx, "haproxy", "-c", "-f", tmp.Name()).Run()
|
||||
out, err := exec.CommandContext(ctx, "haproxy", "-c", "-f", tmp.Name()).CombinedOutput()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%s:%w", string(out), err)
|
||||
}
|
||||
return hash, err
|
||||
}
|
||||
|
||||
func (ha *HAProxyManager) UpdateConfig(ctx context.Context, lbs Config) error {
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ package haproxy
|
|||
|
||||
const haproxyConfigTemplate = `global
|
||||
daemon
|
||||
chroot /var/lib/haproxy
|
||||
#chroot /var/lib/haproxy
|
||||
chroot /usr/share/haproxy
|
||||
pidfile /run/haproxy.pid
|
||||
user haproxy
|
||||
group haproxy
|
||||
maxconn 5000
|
||||
maxconn 20000
|
||||
defaults
|
||||
mode tcp
|
||||
log global
|
||||
|
|
@ -15,29 +17,37 @@ defaults
|
|||
timeout client 5000ms
|
||||
timeout tunnel 3600s
|
||||
timeout check 10s
|
||||
frontend stats
|
||||
mode http
|
||||
bind 127.0.0.1:8404
|
||||
stats enable
|
||||
stats uri /stats
|
||||
stats refresh 10s
|
||||
stats admin if LOCALHOST
|
||||
{{ range $item := .LoadBalancers -}}
|
||||
{{ range $port := $item.Ports -}}
|
||||
frontend fr_{{ $item.Name }}_{{ $port.Port }}
|
||||
bind {{ $item.IP }}:{{ $port.Port }}
|
||||
{{ range $opt := $item.Options.Frontend -}}
|
||||
{{- range $opt := $item.Options.Frontend }}
|
||||
option {{ $opt }}
|
||||
{{ end -}}
|
||||
{{- 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 -}}
|
||||
{{- range $opt := $item.Options.Backend }}
|
||||
option {{ $opt }}
|
||||
{{ end -}}
|
||||
{{- 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 -}}
|
||||
{{- range $srv := $port.Servers }}
|
||||
server {{ $srv.Name }} {{ $srv.IP }}:{{ $srv.Port }} check
|
||||
{{ end -}}
|
||||
{{- end }}
|
||||
{{ end -}}
|
||||
{{ end -}}`
|
||||
{{ end -}}
|
||||
`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue