/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package pvecloud import ( "context" "strings" apiclient "git.giftfish.de/ston1th/haproxy-lb/pkg/api/client" "git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/client" "git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/schema" v1 "k8s.io/api/core/v1" ) type loadbalancer struct { client *client.Client } func newLoadbalancer(client *client.Client) *loadbalancer { return &loadbalancer{client} } func name(service *v1.Service) string { return service.Namespace + "_" + service.Name } func fullname(clusterName string, service *v1.Service) string { return clusterName + "/" + name(service) } func lbStatus(ip string) *v1.LoadBalancerStatus { return &v1.LoadBalancerStatus{ Ingress: []v1.LoadBalancerIngress{ {IP: ip}, }, } } func (lb *loadbalancer) GetLoadBalancer(ctx context.Context, clusterName string, service *v1.Service) (status *v1.LoadBalancerStatus, exists bool, err error) { l, err := lb.client.GetLoadBalancer(ctx, clusterName, name(service)) if err != nil { return } exists = true status = lbStatus(l.IP) return } func (lb *loadbalancer) GetLoadBalancerName(ctx context.Context, clusterName string, service *v1.Service) string { return fullname(clusterName, service) } const ( FrontendHAProxyLBOptions = "frontend.haproxy-lb/options" BackendHAProxyLBOptions = "backend.haproxy-lb/options" BackendHAProxyLBProxyProtocol = "backend.haproxy-lb/proxy-protocol" BackendHAProxyLBCheckProxyProtocol = "backend.haproxy-lb/check-proxy-protocol" BackendHAProxyLBDefaultServer = "backend.haproxy-lb/default-server" ) func (lb *loadbalancer) EnsureLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) (status *v1.LoadBalancerStatus, err error) { /*TODO service annotations port https://pkg.go.dev/k8s.io/api/core/v1#NodeStatus node.Status.ExternalTrafficPolicy ? v1.ServiceExternalTrafficPolicyTypeLocal v1.ServiceExternalTrafficPolicyTypeCluster */ var slb schema.LoadBalancer if fo, ok := service.Annotations[FrontendHAProxyLBOptions]; ok { slb.Options.Frontend = strings.Split(fo, ",") } if bo, ok := service.Annotations[BackendHAProxyLBOptions]; ok { slb.Options.Backend = strings.Split(bo, ",") } if lbpp, ok := service.Annotations[BackendHAProxyLBProxyProtocol]; ok { pp, err := schema.ParseProxyProtocol(lbpp) if err != nil { return nil, err } slb.Options.ProxyProtocol = pp } if cpp, ok := service.Annotations[BackendHAProxyLBCheckProxyProtocol]; ok { slb.Options.CheckProxyProtocol = cpp == "true" } if df, ok := service.Annotations[BackendHAProxyLBDefaultServer]; ok { slb.Options.DefaultServer = df } for _, port := range service.Spec.Ports { sp := schema.Port{Port: int(port.Port)} for _, node := range nodes { ip := "" for _, a := range node.Status.Addresses { if a.Type == v1.NodeInternalIP { ip = a.Address break } else if a.Type == v1.NodeHostName { ip = a.Address break } else if a.Type == v1.NodeExternalIP { ip = a.Address break } } if ip == "" { continue } sp.Servers = append(sp.Servers, schema.Server{ Name: node.Name, IP: ip, Port: int(port.NodePort), }) } slb.Ports = append(slb.Ports, sp) } n := name(service) err = lb.client.CreateLoadBalancer(ctx, clusterName, n, slb) if err != nil { return } l, err := lb.client.GetLoadBalancer(ctx, clusterName, n) if err != nil { return } status = lbStatus(l.IP) return } func (lb *loadbalancer) UpdateLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) error { _, err := lb.EnsureLoadBalancer(ctx, clusterName, service, nodes) return err } func (lb *loadbalancer) EnsureLoadBalancerDeleted(ctx context.Context, clusterName string, service *v1.Service) error { err := lb.client.DeleteLoadBalancer(ctx, clusterName, name(service)) if err == apiclient.ErrNotFound { return nil } return err }