Compare commits

...

11 commits

Author SHA1 Message Date
65de2adc47 Update main.go
All checks were successful
Build Go Binary / build (push) Successful in 32s
2025-08-16 07:53:49 +02:00
150373a819 delete log
All checks were successful
Build Go Binary / build (push) Successful in 38s
2025-08-15 14:31:49 +02:00
91fbebd25e Update README_GO.md
All checks were successful
Build Go Binary / build (push) Successful in 32s
2025-08-08 19:03:08 +02:00
f889527cc7 Update README_GO.md
All checks were successful
Build Go Binary / build (push) Successful in 34s
2025-08-08 19:02:06 +02:00
1ba52cb8fd Update main.go
All checks were successful
Build Go Binary / build (push) Successful in 39s
2025-08-08 11:57:49 +02:00
c42d85a1d5 Update README_GO.md
All checks were successful
Build Go Binary / build (push) Successful in 30s
2025-08-06 17:35:22 +02:00
923261a527 Update main.go
Some checks failed
Build Go Binary / build (push) Has been cancelled
2025-08-06 17:34:55 +02:00
f797d909cf Update README_GO.md
Some checks failed
Build Go Binary / build (push) Failing after 27s
2025-08-06 17:30:19 +02:00
d010200de7 Update main.go
Some checks failed
Build Go Binary / build (push) Has been cancelled
2025-08-06 17:29:55 +02:00
5e645198a7 Update README_GO.md
All checks were successful
Build Go Binary / build (push) Successful in 30s
2025-08-06 17:27:01 +02:00
27ab196460 Update main.go
All checks were successful
Build Go Binary / build (push) Successful in 41s
2025-08-06 17:16:38 +02:00
2 changed files with 49 additions and 28 deletions

View file

@ -21,14 +21,17 @@ backend {{ .Name }}
cookie {{ .CookieName }} {{ .CookieFlags }} cookie {{ .CookieName }} {{ .CookieFlags }}
{{- end }} {{- end }}
{{- if .HealthCheck }}
option httpchk GET {{ .HealthCheck }}
http-check expect status 200
{{- end }}
{{- $backend := . }}
{{- range .Servers }} {{- 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 }} {{- end }}
{{- end }}
``` ```
@ -45,8 +48,7 @@ Environment=KUBERNETES_HOST=https://10.0.20.7:6443
Environment=KUBERNETES_TOKEN=eyJhbGciOi... Environment=KUBERNETES_TOKEN=eyJhbGciOi...
Environment=KUBERNETES_VERIFYSSL=false Environment=KUBERNETES_VERIFYSSL=false
Environment=HAPROXY_TEMPLATE=/etc/haproxy/haproxy.tmpl Environment=HAPROXY_TEMPLATE=/etc/haproxy/haproxy.tmpl
ExecStart=/path/to/haproxy-generator > /etc/haproxy/haproxy.cfg ExecStart=/bin/bash /etc/haproxy/haproxy-generator.sh
ExecStartPost=/bin/systemctl restart haproxy
``` ```
@ -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 # Service Annotations
| Annotation-Key | Beschreibung | Typ | Beispielwert | | Annotation-Key | Beschreibung | Typ | Beispielwert |

21
main.go
View file

@ -33,7 +33,7 @@ type Backend struct {
Mode string Mode string
CookieName string CookieName string
CookieFlags string CookieFlags string
HealthCheck bool HealthCheck string
ServerOptions string ServerOptions string
Servers []BackendServer Servers []BackendServer
} }
@ -188,15 +188,12 @@ func main() {
key := svc.Metadata.Namespace + "/" + svc.Metadata.Name key := svc.Metadata.Namespace + "/" + svc.Metadata.Name
ep, found := endpointMap[key] ep, found := endpointMap[key]
if !found || len(ep.Subsets) == 0 {
continue
}
b := Backend{ b := Backend{
Name: "SRV_" + strings.ReplaceAll(svc.Metadata.Name, " ", "-"), Name: "SRV_" + strings.ReplaceAll(svc.Metadata.Name, " ", "-"),
Balance: "leastconn", Balance: "leastconn",
Mode: "tcp", Mode: "tcp",
HealthCheck: true, HealthCheck: "",
} }
if val, ok := ann["haproxy/mode"]; ok && val != "" { if val, ok := ann["haproxy/mode"]; ok && val != "" {
@ -208,8 +205,8 @@ func main() {
if val, ok := ann["haproxy/cookie-flags"]; ok && val != "" { if val, ok := ann["haproxy/cookie-flags"]; ok && val != "" {
b.CookieFlags = val b.CookieFlags = val
} }
if val, ok := ann["haproxy/health-check"]; ok && strings.ToLower(val) == "false" { if val, ok := ann["haproxy/health-check"]; ok && val != "" {
b.HealthCheck = false b.HealthCheck = val
} }
if val, ok := ann["haproxy/server-options"]; ok && val != "" { if val, ok := ann["haproxy/server-options"]; ok && val != "" {
b.ServerOptions = val b.ServerOptions = val
@ -223,7 +220,8 @@ func main() {
} }
servers := []BackendServer{} servers := []BackendServer{}
serverIndex := 1
if found && len(ep.Subsets) > 0 {
for _, subset := range ep.Subsets { for _, subset := range ep.Subsets {
for _, addr := range subset.Addresses { for _, addr := range subset.Addresses {
for _, port := range subset.Ports { for _, port := range subset.Ports {
@ -231,8 +229,7 @@ func main() {
continue continue
} }
cookie := hashString(fmt.Sprintf("%s-%s-%d", svc.Metadata.Name, addr.IP, port.Port)) cookie := hashString(fmt.Sprintf("%s-%s-%d", svc.Metadata.Name, addr.IP, port.Port))
serverName := fmt.Sprintf("%s_%d", svc.Metadata.Name, serverIndex) serverName := fmt.Sprintf("%s_%d", svc.Metadata.Name, cookie)
serverIndex++
servers = append(servers, BackendServer{ servers = append(servers, BackendServer{
Name: serverName, Name: serverName,
Address: addr.IP, Address: addr.IP,
@ -242,13 +239,17 @@ func main() {
} }
} }
} }
}
b.Servers = servers b.Servers = servers
backends = append(backends, b) backends = append(backends, b)
} }
tmplAbsPath, err := filepath.Abs(templatePath) tmplAbsPath, err := filepath.Abs(templatePath)
if err != nil { if err != nil {
log.Fatalf("Failed to get absolute path: %v", err) log.Fatalf("Failed to get absolute path: %v", err)
} }
tmpl, err := getTemplate(tmplAbsPath) tmpl, err := getTemplate(tmplAbsPath)
if err != nil { if err != nil {
log.Fatalf("Failed to parse template: %v", err) log.Fatalf("Failed to parse template: %v", err)