From edef83ffd36dc3675e2774998a9513d1b47e7439 Mon Sep 17 00:00:00 2001 From: holzi1005 Date: Wed, 6 Aug 2025 15:09:22 +0200 Subject: [PATCH] Update main.go --- main.go | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index b3b499b..79049ce 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,6 @@ type Backend struct { Servers []BackendServer } - func getEnv(key, fallback string) string { if val, ok := os.LookupEnv(key); ok && val != "" { return val @@ -102,7 +101,6 @@ func main() { k8sToken := getEnv("KUBERNETES_TOKEN", "") verifySSLStr := getEnv("KUBERNETES_VERIFYSSL", "false") templatePath := getEnv("HAPROXY_TEMPLATE", "haproxy.tmpl") - annotationCheckEnabled := os.Getenv("KUBERNETES_ANNOTATION_HAPROXY") != "" if k8sHost == "" || k8sToken == "" { log.Fatal("KUBERNETES_HOST and KUBERNETES_TOKEN must be set") @@ -139,16 +137,13 @@ func main() { type EndpointSubsetAddress struct { IP string `json:"ip"` } - type EndpointSubsetPort struct { Port int `json:"port"` } - type EndpointSubset struct { Addresses []EndpointSubsetAddress `json:"addresses"` Ports []EndpointSubsetPort `json:"ports"` } - type Endpoint struct { Metadata struct { Name string `json:"name"` @@ -186,11 +181,9 @@ func main() { backends := []Backend{} for _, svc := range services { - if annotationCheckEnabled { - val, ok := svc.Metadata.Annotations["haproxy/enabled"] - if !ok || val != "true" { - continue - } + ann := svc.Metadata.Annotations + if val, ok := ann["haproxy/enabled"]; !ok || val != "true" { + continue } key := svc.Metadata.Namespace + "/" + svc.Metadata.Name @@ -199,21 +192,17 @@ func main() { continue } - ann := svc.Metadata.Annotations - b := Backend{ Name: "SRV_" + strings.ReplaceAll(svc.Metadata.Name, " ", "-"), Balance: "leastconn", - Mode: "tcp", // default - CookieName: "", - CookieFlags: "", + Mode: "tcp", HealthCheck: true, } if val, ok := ann["haproxy/mode"]; ok && val != "" { b.Mode = val } - if val, ok := ann["haproxy/cookie-name"]; ok && val != "" { + if val, ok := ann["haproxy/cookie"]; ok && val != "" { b.CookieName = val } if val, ok := ann["haproxy/cookie-flags"]; ok && val != "" { @@ -222,20 +211,28 @@ func main() { if val, ok := ann["haproxy/health-check"]; ok && strings.ToLower(val) == "false" { b.HealthCheck = false } - if val, ok := ann["haproxy/server-options"]; ok && val != "" { b.ServerOptions = val } + targetPort := 0 + if val, ok := ann["haproxy/port"]; ok && val != "" { + if p, err := strconv.Atoi(val); err == nil { + targetPort = p + } + } + 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 + } 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, @@ -248,7 +245,6 @@ func main() { b.Servers = servers backends = append(backends, b) } - tmplAbsPath, err := filepath.Abs(templatePath) if err != nil { log.Fatalf("Failed to get absolute path: %v", err) @@ -257,7 +253,6 @@ func main() { if err != nil { log.Fatalf("Failed to parse template: %v", err) } - err = tmpl.Execute(os.Stdout, map[string]interface{}{ "backends": backends, })