2019-04-05 10:22:04 +00:00
|
|
|
package rancher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/label"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/provider"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/provider/constraints"
|
2019-04-05 10:22:04 +00:00
|
|
|
)
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
func (p *Provider) buildConfiguration(ctx context.Context, services []rancherData) *dynamic.Configuration {
|
|
|
|
configurations := make(map[string]*dynamic.Configuration)
|
2019-04-05 10:22:04 +00:00
|
|
|
|
|
|
|
for _, service := range services {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx).With().Str("service", service.Name).Logger()
|
|
|
|
ctxService := logger.WithContext(ctx)
|
2019-04-05 10:22:04 +00:00
|
|
|
|
|
|
|
if !p.keepService(ctx, service) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
confFromLabel, err := label.DecodeConfiguration(service.Labels)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2019-04-05 10:22:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
var tcpOrUDP bool
|
2019-04-05 10:22:04 +00:00
|
|
|
if len(confFromLabel.TCP.Routers) > 0 || len(confFromLabel.TCP.Services) > 0 {
|
2020-02-20 21:24:05 +00:00
|
|
|
tcpOrUDP = true
|
|
|
|
|
2019-04-05 10:22:04 +00:00
|
|
|
err := p.buildTCPServiceConfiguration(ctxService, service, confFromLabel.TCP)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2019-04-05 10:22:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
provider.BuildTCPRouterConfiguration(ctxService, confFromLabel.TCP)
|
2020-02-20 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(confFromLabel.UDP.Routers) > 0 || len(confFromLabel.UDP.Services) > 0 {
|
|
|
|
tcpOrUDP = true
|
|
|
|
|
|
|
|
err := p.buildUDPServiceConfiguration(ctxService, service, confFromLabel.UDP)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2019-04-05 10:22:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-02-20 21:24:05 +00:00
|
|
|
provider.BuildUDPRouterConfiguration(ctxService, confFromLabel.UDP)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tcpOrUDP && len(confFromLabel.HTTP.Routers) == 0 &&
|
|
|
|
len(confFromLabel.HTTP.Middlewares) == 0 &&
|
|
|
|
len(confFromLabel.HTTP.Services) == 0 {
|
|
|
|
configurations[service.Name] = confFromLabel
|
|
|
|
continue
|
2019-04-05 10:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = p.buildServiceConfiguration(ctx, service, confFromLabel.HTTP)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2019-04-05 10:22:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
model := struct {
|
|
|
|
Name string
|
|
|
|
Labels map[string]string
|
|
|
|
}{
|
|
|
|
Name: service.Name,
|
|
|
|
Labels: service.Labels,
|
|
|
|
}
|
|
|
|
|
|
|
|
provider.BuildRouterConfiguration(ctx, confFromLabel.HTTP, service.Name, p.defaultRuleTpl, model)
|
|
|
|
|
|
|
|
configurations[service.Name] = confFromLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider.Merge(ctx, configurations)
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
func (p *Provider) buildTCPServiceConfiguration(ctx context.Context, service rancherData, configuration *dynamic.TCPConfiguration) error {
|
2019-04-05 10:22:04 +00:00
|
|
|
serviceName := service.Name
|
|
|
|
|
|
|
|
if len(configuration.Services) == 0 {
|
2019-07-10 07:26:04 +00:00
|
|
|
configuration.Services = make(map[string]*dynamic.TCPService)
|
2019-09-13 18:00:06 +00:00
|
|
|
lb := &dynamic.TCPServersLoadBalancer{}
|
2019-09-13 15:46:04 +00:00
|
|
|
lb.SetDefaults()
|
2019-07-10 07:26:04 +00:00
|
|
|
configuration.Services[serviceName] = &dynamic.TCPService{
|
2019-04-05 10:22:04 +00:00
|
|
|
LoadBalancer: lb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, confService := range configuration.Services {
|
|
|
|
err := p.addServerTCP(ctx, service, confService.LoadBalancer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
func (p *Provider) buildUDPServiceConfiguration(ctx context.Context, service rancherData, configuration *dynamic.UDPConfiguration) error {
|
|
|
|
serviceName := service.Name
|
|
|
|
|
|
|
|
if len(configuration.Services) == 0 {
|
|
|
|
configuration.Services = make(map[string]*dynamic.UDPService)
|
|
|
|
lb := &dynamic.UDPServersLoadBalancer{}
|
|
|
|
|
|
|
|
configuration.Services[serviceName] = &dynamic.UDPService{
|
|
|
|
LoadBalancer: lb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, confService := range configuration.Services {
|
|
|
|
err := p.addServerUDP(ctx, service, confService.LoadBalancer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
func (p *Provider) buildServiceConfiguration(ctx context.Context, service rancherData, configuration *dynamic.HTTPConfiguration) error {
|
2019-04-05 10:22:04 +00:00
|
|
|
serviceName := service.Name
|
|
|
|
|
|
|
|
if len(configuration.Services) == 0 {
|
2019-07-10 07:26:04 +00:00
|
|
|
configuration.Services = make(map[string]*dynamic.Service)
|
2019-08-26 08:30:05 +00:00
|
|
|
lb := &dynamic.ServersLoadBalancer{}
|
2019-04-05 10:22:04 +00:00
|
|
|
lb.SetDefaults()
|
2019-07-10 07:26:04 +00:00
|
|
|
configuration.Services[serviceName] = &dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
LoadBalancer: lb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, confService := range configuration.Services {
|
|
|
|
err := p.addServers(ctx, service, confService.LoadBalancer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) keepService(ctx context.Context, service rancherData) bool {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx)
|
2019-04-05 10:22:04 +00:00
|
|
|
|
|
|
|
if !service.ExtraConf.Enable {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Filtering disabled service")
|
2019-04-05 10:22:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:16:05 +00:00
|
|
|
matches, err := constraints.MatchLabels(service.Labels, p.Constraints)
|
2019-06-21 07:24:04 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Error matching constraint expression")
|
2019-06-21 07:24:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !matches {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msgf("Service pruned by constraint expression: %q", p.Constraints)
|
2019-04-05 10:22:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.EnableServiceHealthFilter {
|
|
|
|
if service.Health != "" && service.Health != healthy && service.Health != updatingHealthy {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msgf("Filtering service %s with healthState of %s", service.Name, service.Health)
|
2019-04-05 10:22:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if service.State != "" && service.State != active && service.State != updatingActive && service.State != upgraded && service.State != upgrading {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msgf("Filtering service %s with state of %s", service.Name, service.State)
|
2019-04-05 10:22:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-13 18:00:06 +00:00
|
|
|
func (p *Provider) addServerTCP(ctx context.Context, service rancherData, loadBalancer *dynamic.TCPServersLoadBalancer) error {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Ctx(ctx).Debug().Msgf("Trying to add servers for service %s", service.Name)
|
2019-04-05 10:22:04 +00:00
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if loadBalancer == nil {
|
|
|
|
return errors.New("load-balancer is not defined")
|
2019-04-05 10:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(loadBalancer.Servers) == 0 {
|
2022-09-21 12:54:08 +00:00
|
|
|
loadBalancer.Servers = []dynamic.TCPServer{{}}
|
2019-04-05 10:22:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
port := loadBalancer.Servers[0].Port
|
|
|
|
loadBalancer.Servers[0].Port = ""
|
|
|
|
|
|
|
|
if port == "" {
|
|
|
|
port = getServicePort(service)
|
2019-04-05 10:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if port == "" {
|
|
|
|
return errors.New("port is missing")
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
var servers []dynamic.TCPServer
|
2019-04-05 10:22:04 +00:00
|
|
|
for _, containerIP := range service.Containers {
|
2019-07-10 07:26:04 +00:00
|
|
|
servers = append(servers, dynamic.TCPServer{
|
2019-04-05 10:22:04 +00:00
|
|
|
Address: net.JoinHostPort(containerIP, port),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
loadBalancer.Servers = servers
|
2022-09-21 12:54:08 +00:00
|
|
|
|
2019-04-05 10:22:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
func (p *Provider) addServerUDP(ctx context.Context, service rancherData, loadBalancer *dynamic.UDPServersLoadBalancer) error {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Ctx(ctx).Debug().Msgf("Trying to add servers for service %s", service.Name)
|
2020-02-20 21:24:05 +00:00
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if loadBalancer == nil {
|
|
|
|
return errors.New("load-balancer is not defined")
|
2020-02-20 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(loadBalancer.Servers) == 0 {
|
2022-09-21 12:54:08 +00:00
|
|
|
loadBalancer.Servers = []dynamic.UDPServer{{}}
|
2020-02-20 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
port := loadBalancer.Servers[0].Port
|
|
|
|
loadBalancer.Servers[0].Port = ""
|
|
|
|
|
|
|
|
if port == "" {
|
|
|
|
port = getServicePort(service)
|
2020-02-20 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if port == "" {
|
|
|
|
return errors.New("port is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
var servers []dynamic.UDPServer
|
|
|
|
for _, containerIP := range service.Containers {
|
|
|
|
servers = append(servers, dynamic.UDPServer{
|
|
|
|
Address: net.JoinHostPort(containerIP, port),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
loadBalancer.Servers = servers
|
2022-09-21 12:54:08 +00:00
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:30:05 +00:00
|
|
|
func (p *Provider) addServers(ctx context.Context, service rancherData, loadBalancer *dynamic.ServersLoadBalancer) error {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Ctx(ctx).Debug().Msgf("Trying to add servers for service %s", service.Name)
|
2019-04-05 10:22:04 +00:00
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if loadBalancer == nil {
|
|
|
|
return errors.New("load-balancer is not defined")
|
|
|
|
}
|
2019-04-05 10:22:04 +00:00
|
|
|
|
|
|
|
if len(loadBalancer.Servers) == 0 {
|
2019-07-10 07:26:04 +00:00
|
|
|
server := dynamic.Server{}
|
2019-04-05 10:22:04 +00:00
|
|
|
server.SetDefaults()
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
loadBalancer.Servers = []dynamic.Server{server}
|
2019-04-05 10:22:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
port := loadBalancer.Servers[0].Port
|
|
|
|
loadBalancer.Servers[0].Port = ""
|
|
|
|
|
|
|
|
if port == "" {
|
|
|
|
port = getServicePort(service)
|
2019-04-05 10:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if port == "" {
|
|
|
|
return errors.New("port is missing")
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
var servers []dynamic.Server
|
2019-04-05 10:22:04 +00:00
|
|
|
for _, containerIP := range service.Containers {
|
2019-07-10 07:26:04 +00:00
|
|
|
servers = append(servers, dynamic.Server{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: fmt.Sprintf("%s://%s", loadBalancer.Servers[0].Scheme, net.JoinHostPort(containerIP, port)),
|
2019-04-05 10:22:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
loadBalancer.Servers = servers
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
return nil
|
2019-04-05 10:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getServicePort(data rancherData) string {
|
|
|
|
rawPort := strings.Split(data.Port, "/")[0]
|
|
|
|
hostPort := strings.Split(rawPort, ":")
|
|
|
|
|
|
|
|
if len(hostPort) >= 2 {
|
|
|
|
return hostPort[1]
|
|
|
|
}
|
|
|
|
if len(hostPort) > 0 && hostPort[0] != "" {
|
|
|
|
return hostPort[0]
|
|
|
|
}
|
|
|
|
return rawPort
|
|
|
|
}
|