2020-02-26 11:28:05 +00:00
|
|
|
package crd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-12-04 19:56:04 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2020-02-26 11:28:05 +00:00
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
2023-04-17 08:56:36 +00:00
|
|
|
traefikv1alpha1 "github.com/traefik/traefik/v3/pkg/provider/kubernetes/crd/traefikio/v1alpha1"
|
2020-02-26 11:28:05 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *Provider) loadIngressRouteUDPConfiguration(ctx context.Context, client Client) *dynamic.UDPConfiguration {
|
|
|
|
conf := &dynamic.UDPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.UDPRouter{},
|
|
|
|
Services: map[string]*dynamic.UDPService{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ingressRouteUDP := range client.GetIngressRouteUDPs() {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx).With().Str("ingress", ingressRouteUDP.Name).Str("namespace", ingressRouteUDP.Namespace).Logger()
|
2020-02-26 11:28:05 +00:00
|
|
|
|
|
|
|
if !shouldProcessIngress(p.IngressClass, ingressRouteUDP.Annotations[annotationKubernetesIngressClass]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ingressName := ingressRouteUDP.Name
|
|
|
|
if len(ingressName) == 0 {
|
|
|
|
ingressName = ingressRouteUDP.GenerateName
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, route := range ingressRouteUDP.Spec.Routes {
|
|
|
|
key := fmt.Sprintf("%s-%d", ingressName, i)
|
|
|
|
serviceName := makeID(ingressRouteUDP.Namespace, key)
|
|
|
|
|
|
|
|
for _, service := range route.Services {
|
2020-12-10 13:58:04 +00:00
|
|
|
balancerServerUDP, err := p.createLoadBalancerServerUDP(client, ingressRouteUDP.Namespace, service)
|
2020-02-26 11:28:05 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().
|
|
|
|
Str("serviceName", service.Name).
|
|
|
|
Stringer("servicePort", &service.Port).
|
|
|
|
Err(err).
|
|
|
|
Msg("Cannot create service")
|
2020-02-26 11:28:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is only one service defined, we skip the creation of the load balancer of services,
|
|
|
|
// i.e. the service on top is directly a load balancer of servers.
|
|
|
|
if len(route.Services) == 1 {
|
|
|
|
conf.Services[serviceName] = balancerServerUDP
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-01-15 14:54:04 +00:00
|
|
|
serviceKey := fmt.Sprintf("%s-%s-%s", serviceName, service.Name, &service.Port)
|
2020-02-26 11:28:05 +00:00
|
|
|
conf.Services[serviceKey] = balancerServerUDP
|
|
|
|
|
|
|
|
srv := dynamic.UDPWRRService{Name: serviceKey}
|
|
|
|
srv.SetDefaults()
|
|
|
|
if service.Weight != nil {
|
|
|
|
srv.Weight = service.Weight
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Services[serviceName] == nil {
|
|
|
|
conf.Services[serviceName] = &dynamic.UDPService{Weighted: &dynamic.UDPWeightedRoundRobin{}}
|
|
|
|
}
|
|
|
|
conf.Services[serviceName].Weighted.Services = append(conf.Services[serviceName].Weighted.Services, srv)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Routers[serviceName] = &dynamic.UDPRouter{
|
|
|
|
EntryPoints: ingressRouteUDP.Spec.EntryPoints,
|
|
|
|
Service: serviceName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (p *Provider) createLoadBalancerServerUDP(client Client, parentNamespace string, service traefikv1alpha1.ServiceUDP) (*dynamic.UDPService, error) {
|
2020-12-10 13:58:04 +00:00
|
|
|
ns := parentNamespace
|
2020-02-26 11:28:05 +00:00
|
|
|
if len(service.Namespace) > 0 {
|
2020-12-10 13:58:04 +00:00
|
|
|
if !isNamespaceAllowed(p.AllowCrossNamespace, parentNamespace, service.Namespace) {
|
|
|
|
return nil, fmt.Errorf("udp service %s/%s is not in the parent resource namespace %s", service.Namespace, service.Name, ns)
|
|
|
|
}
|
|
|
|
|
2020-02-26 11:28:05 +00:00
|
|
|
ns = service.Namespace
|
|
|
|
}
|
|
|
|
|
2021-07-13 10:54:09 +00:00
|
|
|
servers, err := p.loadUDPServers(client, ns, service)
|
2020-02-26 11:28:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
udpService := &dynamic.UDPService{
|
|
|
|
LoadBalancer: &dynamic.UDPServersLoadBalancer{
|
|
|
|
Servers: servers,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return udpService, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (p *Provider) loadUDPServers(client Client, namespace string, svc traefikv1alpha1.ServiceUDP) ([]dynamic.UDPServer, error) {
|
2020-02-26 11:28:05 +00:00
|
|
|
service, exists, err := client.GetService(namespace, svc.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.New("service not found")
|
|
|
|
}
|
|
|
|
|
2021-07-13 10:54:09 +00:00
|
|
|
if service.Spec.Type == corev1.ServiceTypeExternalName && !p.AllowExternalNameServices {
|
|
|
|
return nil, fmt.Errorf("externalName services not allowed: %s/%s", namespace, svc.Name)
|
|
|
|
}
|
|
|
|
|
2021-01-19 08:30:05 +00:00
|
|
|
svcPort, err := getServicePort(service, svc.Port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-26 11:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var servers []dynamic.UDPServer
|
2024-02-27 09:54:04 +00:00
|
|
|
if service.Spec.Type == corev1.ServiceTypeNodePort && svc.NodePortLB {
|
2024-08-01 13:50:04 +00:00
|
|
|
if p.DisableClusterScopeResources {
|
|
|
|
return nil, errors.New("nodes lookup is disabled")
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:54:04 +00:00
|
|
|
nodes, nodesExists, nodesErr := client.GetNodes()
|
|
|
|
if nodesErr != nil {
|
|
|
|
return nil, nodesErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nodesExists || len(nodes) == 0 {
|
|
|
|
return nil, fmt.Errorf("nodes not found for NodePort service %s/%s", svc.Namespace, svc.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
for _, addr := range node.Status.Addresses {
|
|
|
|
if addr.Type == corev1.NodeInternalIP {
|
|
|
|
servers = append(servers, dynamic.UDPServer{
|
|
|
|
Address: net.JoinHostPort(addr.Address, strconv.Itoa(int(svcPort.NodePort))),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(servers) == 0 {
|
|
|
|
return nil, fmt.Errorf("no servers were generated for service %s/%s", svc.Namespace, svc.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return servers, nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 11:28:05 +00:00
|
|
|
if service.Spec.Type == corev1.ServiceTypeExternalName {
|
|
|
|
servers = append(servers, dynamic.UDPServer{
|
2021-01-19 08:30:05 +00:00
|
|
|
Address: net.JoinHostPort(service.Spec.ExternalName, strconv.Itoa(int(svcPort.Port))),
|
2020-02-26 11:28:05 +00:00
|
|
|
})
|
|
|
|
} else {
|
2024-04-29 10:20:04 +00:00
|
|
|
nativeLB := p.NativeLBByDefault
|
|
|
|
if svc.NativeLB != nil {
|
|
|
|
nativeLB = *svc.NativeLB
|
|
|
|
}
|
|
|
|
if nativeLB {
|
|
|
|
address, err := getNativeServiceAddress(*service, *svcPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting native Kubernetes Service address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []dynamic.UDPServer{{Address: address}}, nil
|
|
|
|
}
|
|
|
|
|
2024-06-21 12:56:03 +00:00
|
|
|
endpointSlices, err := client.GetEndpointSlicesForService(namespace, svc.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting endpointslices: %w", err)
|
2020-02-26 11:28:05 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 12:56:03 +00:00
|
|
|
addresses := map[string]struct{}{}
|
|
|
|
for _, endpointSlice := range endpointSlices {
|
|
|
|
var port int32
|
|
|
|
for _, p := range endpointSlice.Ports {
|
|
|
|
if svcPort.Name == *p.Name {
|
|
|
|
port = *p.Port
|
2020-02-26 11:28:05 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if port == 0 {
|
2024-06-21 12:56:03 +00:00
|
|
|
continue
|
2020-02-26 11:28:05 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 12:56:03 +00:00
|
|
|
for _, endpoint := range endpointSlice.Endpoints {
|
|
|
|
if endpoint.Conditions.Ready == nil || !*endpoint.Conditions.Ready {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range endpoint.Addresses {
|
|
|
|
if _, ok := addresses[address]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses[address] = struct{}{}
|
|
|
|
servers = append(servers, dynamic.UDPServer{
|
|
|
|
Address: net.JoinHostPort(address, strconv.Itoa(int(port))),
|
|
|
|
})
|
|
|
|
}
|
2020-02-26 11:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-21 12:56:03 +00:00
|
|
|
if len(servers) == 0 && !p.AllowEmptyServices {
|
|
|
|
return nil, fmt.Errorf("no servers found for %s/%s", namespace, svc.Name)
|
|
|
|
}
|
|
|
|
|
2020-02-26 11:28:05 +00:00
|
|
|
return servers, nil
|
|
|
|
}
|