Compare commits
11 commits
Author | SHA1 | Date | |
---|---|---|---|
65de2adc47 | |||
150373a819 | |||
91fbebd25e | |||
f889527cc7 | |||
1ba52cb8fd | |||
c42d85a1d5 | |||
923261a527 | |||
f797d909cf | |||
d010200de7 | |||
5e645198a7 | |||
27ab196460 |
2 changed files with 49 additions and 28 deletions
32
README_GO.md
32
README_GO.md
|
@ -21,14 +21,17 @@ backend {{ .Name }}
|
|||
cookie {{ .CookieName }} {{ .CookieFlags }}
|
||||
{{- end }}
|
||||
|
||||
{{- if .HealthCheck }}
|
||||
option httpchk GET {{ .HealthCheck }}
|
||||
http-check expect status 200
|
||||
{{- end }}
|
||||
|
||||
{{- $backend := . }}
|
||||
{{- range .Servers }}
|
||||
server {{ .Name }} {{ .Address }}:{{ .Port }}{{ if $.HealthCheck }} check{{ end }}{{ if $.CookieName }} cookie {{ .Cookie }}{{ end }} {{ $.ServerOptions }}
|
||||
server {{ .Name }} {{ .Address }}:{{ .Port }}{{ if $backend.HealthCheck }} check{{ end }}{{ if $backend.CookieName }} cookie {{ .Cookie }}{{ end }}{{ if $backend.ServerOptions }} {{ $backend.ServerOptions }}{{ end }}
|
||||
{{- end }}
|
||||
|
||||
{{- end }}
|
||||
|
||||
{{- end }}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
@ -45,8 +48,7 @@ Environment=KUBERNETES_HOST=https://10.0.20.7:6443
|
|||
Environment=KUBERNETES_TOKEN=eyJhbGciOi...
|
||||
Environment=KUBERNETES_VERIFYSSL=false
|
||||
Environment=HAPROXY_TEMPLATE=/etc/haproxy/haproxy.tmpl
|
||||
ExecStart=/path/to/haproxy-generator > /etc/haproxy/haproxy.cfg
|
||||
ExecStartPost=/bin/systemctl restart haproxy
|
||||
ExecStart=/bin/bash /etc/haproxy/haproxy-generator.sh
|
||||
|
||||
```
|
||||
|
||||
|
@ -66,6 +68,24 @@ WantedBy=timers.target
|
|||
|
||||
```
|
||||
|
||||
# Reload Script for post start
|
||||
|
||||
Copy the script to /etc/haproxy/haproxy-generator.sh
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
/usr/local/bin/haproxy-generator > /etc/haproxy/haproxy.cfg.new
|
||||
DIFF=$(diff /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.new)
|
||||
/sbin/haproxy -f /etc/haproxy/haproxy.cfg.new -c
|
||||
VALID=$?
|
||||
|
||||
if [ "$DIFF" != "" ] && [ $VALID -eq 0 ]
|
||||
then
|
||||
mv /etc/haproxy/haproxy.cfg.new /etc/haproxy/haproxy.cfg
|
||||
/usr/sbin/service haproxy restart
|
||||
fi
|
||||
```
|
||||
|
||||
# Service Annotations
|
||||
|
||||
| Annotation-Key | Beschreibung | Typ | Beispielwert |
|
||||
|
|
45
main.go
45
main.go
|
@ -33,7 +33,7 @@ type Backend struct {
|
|||
Mode string
|
||||
CookieName string
|
||||
CookieFlags string
|
||||
HealthCheck bool
|
||||
HealthCheck string
|
||||
ServerOptions string
|
||||
Servers []BackendServer
|
||||
}
|
||||
|
@ -188,15 +188,12 @@ func main() {
|
|||
|
||||
key := svc.Metadata.Namespace + "/" + svc.Metadata.Name
|
||||
ep, found := endpointMap[key]
|
||||
if !found || len(ep.Subsets) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
b := Backend{
|
||||
Name: "SRV_" + strings.ReplaceAll(svc.Metadata.Name, " ", "-"),
|
||||
Balance: "leastconn",
|
||||
Mode: "tcp",
|
||||
HealthCheck: true,
|
||||
HealthCheck: "",
|
||||
}
|
||||
|
||||
if val, ok := ann["haproxy/mode"]; ok && val != "" {
|
||||
|
@ -208,8 +205,8 @@ func main() {
|
|||
if val, ok := ann["haproxy/cookie-flags"]; ok && val != "" {
|
||||
b.CookieFlags = val
|
||||
}
|
||||
if val, ok := ann["haproxy/health-check"]; ok && strings.ToLower(val) == "false" {
|
||||
b.HealthCheck = false
|
||||
if val, ok := ann["haproxy/health-check"]; ok && val != "" {
|
||||
b.HealthCheck = val
|
||||
}
|
||||
if val, ok := ann["haproxy/server-options"]; ok && val != "" {
|
||||
b.ServerOptions = val
|
||||
|
@ -223,32 +220,36 @@ func main() {
|
|||
}
|
||||
|
||||
servers := []BackendServer{}
|
||||
serverIndex := 1
|
||||
for _, subset := range ep.Subsets {
|
||||
for _, addr := range subset.Addresses {
|
||||
for _, port := range subset.Ports {
|
||||
if targetPort != 0 && port.Port != targetPort {
|
||||
continue
|
||||
|
||||
if found && len(ep.Subsets) > 0 {
|
||||
for _, subset := range ep.Subsets {
|
||||
for _, addr := range subset.Addresses {
|
||||
for _, port := range subset.Ports {
|
||||
if targetPort != 0 && port.Port != targetPort {
|
||||
continue
|
||||
}
|
||||
cookie := hashString(fmt.Sprintf("%s-%s-%d", svc.Metadata.Name, addr.IP, port.Port))
|
||||
serverName := fmt.Sprintf("%s_%d", svc.Metadata.Name, cookie)
|
||||
servers = append(servers, BackendServer{
|
||||
Name: serverName,
|
||||
Address: addr.IP,
|
||||
Port: port.Port,
|
||||
Cookie: cookie,
|
||||
})
|
||||
}
|
||||
cookie := hashString(fmt.Sprintf("%s-%s-%d", svc.Metadata.Name, addr.IP, port.Port))
|
||||
serverName := fmt.Sprintf("%s_%d", svc.Metadata.Name, serverIndex)
|
||||
serverIndex++
|
||||
servers = append(servers, BackendServer{
|
||||
Name: serverName,
|
||||
Address: addr.IP,
|
||||
Port: port.Port,
|
||||
Cookie: cookie,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b.Servers = servers
|
||||
backends = append(backends, b)
|
||||
}
|
||||
|
||||
tmplAbsPath, err := filepath.Abs(templatePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get absolute path: %v", err)
|
||||
}
|
||||
|
||||
tmpl, err := getTemplate(tmplAbsPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse template: %v", err)
|
||||
|
|
Loading…
Add table
Reference in a new issue