Compare commits
No commits in common. "master" and "0.0.13" have entirely different histories.
2 changed files with 38 additions and 59 deletions
50
README_GO.md
50
README_GO.md
|
@ -21,17 +21,14 @@ 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 $backend.HealthCheck }} check{{ end }}{{ if $backend.CookieName }} cookie {{ .Cookie }}{{ end }}{{ if $backend.ServerOptions }} {{ $backend.ServerOptions }}{{ end }}
|
server {{ .Name }} {{ .Address }}:{{ .Port }}{{ if $.HealthCheck }} check{{ end }}{{ if $.CookieName }} cookie {{ .Cookie }}{{ end }} {{ $.ServerOptions }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +45,8 @@ 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=/bin/bash /etc/haproxy/haproxy-generator.sh
|
ExecStart=/path/to/haproxy-generator > /etc/haproxy/haproxy.cfg
|
||||||
|
ExecStartPost=/bin/systemctl restart haproxy
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -68,32 +66,14 @@ 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 |
|
||||||
|--------------------------------|-----------------------------------------------------------------------------|---------|---------------------------|
|
|--------------------------------|-----------------------------------------------------------------------------|---------|----------------------------|
|
||||||
| `haproxy/enabled` | **Aktiviert** die Aufnahme des Services in die HAProxy-Konfiguration | `bool` | `"true"` |
|
| `haproxy/enabled` | **Aktiviert** die Aufnahme des Services in die HAProxy-Konfiguration | `bool` | `"true"` |
|
||||||
| `haproxy/mode` | Betriebsmodus des Backends (`tcp` oder `http`) | `string`| `"tcp"` / `"http"` |
|
| `haproxy/mode` | Betriebsmodus des Backends (`tcp` oder `http`) | `string`| `"tcp"` / `"http"` |
|
||||||
| `haproxy/cookie-name` | Name des Cookies zur Session-Persistenz | `string`| `"SRVCOOKIE"` |
|
| `haproxy/cookie` | Name des Cookies zur Session-Persistenz | `string`| `"SRVCOOKIE"` |
|
||||||
| `haproxy/cookie-flags` | Zusätzliche Cookie-Optionen für HAProxy | `string`| `"insert indirect nocache"`|
|
| `haproxy/cookie-flags` | Zusätzliche Cookie-Optionen für HAProxy | `string`| `"insert indirect nocache"` |
|
||||||
| `haproxy/health-check` | Deaktiviert Health-Checks, wenn `"false"` gesetzt wird | `bool` | `"false"` |
|
| `haproxy/health-check` | Deaktiviert Health-Checks, wenn `"false"` gesetzt wird | `bool` | `"false"` |
|
||||||
| `haproxy/server-options` | Zusätzliche Optionen für die einzelnen `server`-Zeilen in HAProxy | `string`| `"ssl verify none"` |
|
| `haproxy/server-options` | Zusätzliche Optionen für die einzelnen `server`-Zeilen in HAProxy | `string`| `"ssl verify none"` |
|
||||||
| `haproxy/port` | Nutze nur **diesen Port** des Services, wenn mehrere Ports definiert sind | `int` |`"8080"` |
|
| `haproxy/port` | Nutze nur **diesen Port** des Services, wenn mehrere Ports definiert sind | `int` | `"8080"` |
|
47
main.go
47
main.go
|
@ -33,7 +33,7 @@ type Backend struct {
|
||||||
Mode string
|
Mode string
|
||||||
CookieName string
|
CookieName string
|
||||||
CookieFlags string
|
CookieFlags string
|
||||||
HealthCheck string
|
HealthCheck bool
|
||||||
ServerOptions string
|
ServerOptions string
|
||||||
Servers []BackendServer
|
Servers []BackendServer
|
||||||
}
|
}
|
||||||
|
@ -188,25 +188,28 @@ 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: "",
|
HealthCheck: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if val, ok := ann["haproxy/mode"]; ok && val != "" {
|
if val, ok := ann["haproxy/mode"]; ok && val != "" {
|
||||||
b.Mode = val
|
b.Mode = val
|
||||||
}
|
}
|
||||||
if val, ok := ann["haproxy/cookie-name"]; ok && val != "" {
|
if val, ok := ann["haproxy/cookie"]; ok && val != "" {
|
||||||
b.CookieName = val
|
b.CookieName = val
|
||||||
}
|
}
|
||||||
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 && val != "" {
|
if val, ok := ann["haproxy/health-check"]; ok && strings.ToLower(val) == "false" {
|
||||||
b.HealthCheck = val
|
b.HealthCheck = false
|
||||||
}
|
}
|
||||||
if val, ok := ann["haproxy/server-options"]; ok && val != "" {
|
if val, ok := ann["haproxy/server-options"]; ok && val != "" {
|
||||||
b.ServerOptions = val
|
b.ServerOptions = val
|
||||||
|
@ -220,36 +223,32 @@ 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 {
|
if targetPort != 0 && port.Port != targetPort {
|
||||||
if targetPort != 0 && port.Port != targetPort {
|
continue
|
||||||
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
|
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)
|
||||||
|
|
Loading…
Add table
Reference in a new issue