This commit is contained in:
parent
be43cb93b4
commit
edef83ffd3
1 changed files with 15 additions and 20 deletions
33
main.go
33
main.go
|
@ -38,7 +38,6 @@ type Backend struct {
|
||||||
Servers []BackendServer
|
Servers []BackendServer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func getEnv(key, fallback string) string {
|
func getEnv(key, fallback string) string {
|
||||||
if val, ok := os.LookupEnv(key); ok && val != "" {
|
if val, ok := os.LookupEnv(key); ok && val != "" {
|
||||||
return val
|
return val
|
||||||
|
@ -102,7 +101,6 @@ func main() {
|
||||||
k8sToken := getEnv("KUBERNETES_TOKEN", "")
|
k8sToken := getEnv("KUBERNETES_TOKEN", "")
|
||||||
verifySSLStr := getEnv("KUBERNETES_VERIFYSSL", "false")
|
verifySSLStr := getEnv("KUBERNETES_VERIFYSSL", "false")
|
||||||
templatePath := getEnv("HAPROXY_TEMPLATE", "haproxy.tmpl")
|
templatePath := getEnv("HAPROXY_TEMPLATE", "haproxy.tmpl")
|
||||||
annotationCheckEnabled := os.Getenv("KUBERNETES_ANNOTATION_HAPROXY") != ""
|
|
||||||
|
|
||||||
if k8sHost == "" || k8sToken == "" {
|
if k8sHost == "" || k8sToken == "" {
|
||||||
log.Fatal("KUBERNETES_HOST and KUBERNETES_TOKEN must be set")
|
log.Fatal("KUBERNETES_HOST and KUBERNETES_TOKEN must be set")
|
||||||
|
@ -139,16 +137,13 @@ func main() {
|
||||||
type EndpointSubsetAddress struct {
|
type EndpointSubsetAddress struct {
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EndpointSubsetPort struct {
|
type EndpointSubsetPort struct {
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EndpointSubset struct {
|
type EndpointSubset struct {
|
||||||
Addresses []EndpointSubsetAddress `json:"addresses"`
|
Addresses []EndpointSubsetAddress `json:"addresses"`
|
||||||
Ports []EndpointSubsetPort `json:"ports"`
|
Ports []EndpointSubsetPort `json:"ports"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Endpoint struct {
|
type Endpoint struct {
|
||||||
Metadata struct {
|
Metadata struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@ -186,12 +181,10 @@ func main() {
|
||||||
backends := []Backend{}
|
backends := []Backend{}
|
||||||
|
|
||||||
for _, svc := range services {
|
for _, svc := range services {
|
||||||
if annotationCheckEnabled {
|
ann := svc.Metadata.Annotations
|
||||||
val, ok := svc.Metadata.Annotations["haproxy/enabled"]
|
if val, ok := ann["haproxy/enabled"]; !ok || val != "true" {
|
||||||
if !ok || val != "true" {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
key := svc.Metadata.Namespace + "/" + svc.Metadata.Name
|
key := svc.Metadata.Namespace + "/" + svc.Metadata.Name
|
||||||
ep, found := endpointMap[key]
|
ep, found := endpointMap[key]
|
||||||
|
@ -199,21 +192,17 @@ 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
|
Mode: "tcp",
|
||||||
CookieName: "",
|
|
||||||
CookieFlags: "",
|
|
||||||
HealthCheck: true,
|
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 != "" {
|
||||||
|
@ -222,20 +211,28 @@ func main() {
|
||||||
if val, ok := ann["haproxy/health-check"]; ok && strings.ToLower(val) == "false" {
|
if val, ok := ann["haproxy/health-check"]; ok && strings.ToLower(val) == "false" {
|
||||||
b.HealthCheck = false
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
targetPort := 0
|
||||||
|
if val, ok := ann["haproxy/port"]; ok && val != "" {
|
||||||
|
if p, err := strconv.Atoi(val); err == nil {
|
||||||
|
targetPort = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
servers := []BackendServer{}
|
servers := []BackendServer{}
|
||||||
serverIndex := 1
|
serverIndex := 1
|
||||||
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 {
|
||||||
|
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, serverIndex)
|
||||||
serverIndex++
|
serverIndex++
|
||||||
|
|
||||||
servers = append(servers, BackendServer{
|
servers = append(servers, BackendServer{
|
||||||
Name: serverName,
|
Name: serverName,
|
||||||
Address: addr.IP,
|
Address: addr.IP,
|
||||||
|
@ -248,7 +245,6 @@ 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)
|
||||||
|
@ -257,7 +253,6 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to parse template: %v", err)
|
log.Fatalf("Failed to parse template: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tmpl.Execute(os.Stdout, map[string]interface{}{
|
err = tmpl.Execute(os.Stdout, map[string]interface{}{
|
||||||
"backends": backends,
|
"backends": backends,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue