Update main.go
All checks were successful
Build Go Binary / build (push) Successful in 43s

This commit is contained in:
holzi1005 2025-08-06 12:42:03 +02:00
parent 144fbdd18b
commit b15afb8c06

42
main.go
View file

@ -28,9 +28,13 @@ type BackendServer struct {
} }
type Backend struct { type Backend struct {
Name string Name string
Balance string Balance string
Servers []BackendServer Mode string
CookieName string
CookieFlags string
HealthCheck bool
Servers []BackendServer
} }
func getEnv(key, fallback string) string { func getEnv(key, fallback string) string {
@ -118,8 +122,8 @@ func main() {
type Service struct { type Service struct {
Metadata struct { Metadata struct {
Name string `json:"name"` Name string `json:"name"`
Namespace string `json:"namespace"` Namespace string `json:"namespace"`
Annotations map[string]string `json:"annotations"` Annotations map[string]string `json:"annotations"`
} `json:"metadata"` } `json:"metadata"`
Spec struct { Spec struct {
@ -180,10 +184,6 @@ func main() {
backends := []Backend{} backends := []Backend{}
for _, svc := range services { for _, svc := range services {
if svc.Spec.Type != "ClusterIP" {
continue
}
if annotationCheckEnabled { if annotationCheckEnabled {
val, ok := svc.Metadata.Annotations["haproxy/enabled"] val, ok := svc.Metadata.Annotations["haproxy/enabled"]
if !ok || val != "true" { if !ok || val != "true" {
@ -197,9 +197,28 @@ func main() {
continue continue
} }
ann := svc.Metadata.Annotations
b := Backend{ b := Backend{
Name: "SRV_" + strings.ReplaceAll(svc.Metadata.Name, " ", "-"), Name: "SRV_" + strings.ReplaceAll(svc.Metadata.Name, " ", "-"),
Balance: "leastconn", Balance: "leastconn",
Mode: "tcp", // default
CookieName: "",
CookieFlags: "",
HealthCheck: true,
}
if val, ok := ann["haproxy/mode"]; ok && val != "" {
b.Mode = val
}
if val, ok := ann["haproxy/cookie-name"]; ok && val != "" {
b.CookieName = val
}
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
} }
servers := []BackendServer{} servers := []BackendServer{}
@ -221,7 +240,6 @@ func main() {
} }
} }
b.Servers = servers b.Servers = servers
backends = append(backends, b) backends = append(backends, b)
} }