traefik/pkg/provider/consulcatalog/config.go

327 lines
8.2 KiB
Go
Raw Normal View History

2019-10-15 15:34:08 +00:00
package consulcatalog
import (
"context"
"errors"
"fmt"
"hash/fnv"
2019-10-15 15:34:08 +00:00
"net"
"sort"
"strings"
2019-10-15 15:34:08 +00:00
"github.com/hashicorp/consul/api"
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"
"github.com/traefik/traefik/v3/pkg/config/label"
"github.com/traefik/traefik/v3/pkg/logs"
"github.com/traefik/traefik/v3/pkg/provider"
"github.com/traefik/traefik/v3/pkg/provider/constraints"
2019-10-15 15:34:08 +00:00
)
func (p *Provider) buildConfiguration(ctx context.Context, items []itemData, certInfo *connectCert) *dynamic.Configuration {
2019-10-15 15:34:08 +00:00
configurations := make(map[string]*dynamic.Configuration)
for _, item := range items {
svcName := provider.Normalize(item.Node + "-" + item.Name + "-" + item.ID)
2022-11-21 17:36:05 +00:00
logger := log.Ctx(ctx).With().Str(logs.ServiceName, svcName).Logger()
ctxSvc := logger.WithContext(ctx)
2019-10-15 15:34:08 +00:00
if !p.keepContainer(ctxSvc, item) {
continue
}
confFromLabel, err := label.DecodeConfiguration(item.Labels)
if err != nil {
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Send()
2019-10-15 15:34:08 +00:00
continue
}
2020-02-20 21:24:05 +00:00
var tcpOrUDP bool
2019-10-15 15:34:08 +00:00
if len(confFromLabel.TCP.Routers) > 0 || len(confFromLabel.TCP.Services) > 0 {
2020-02-20 21:24:05 +00:00
tcpOrUDP = true
if item.ExtraConf.ConsulCatalog.Connect {
if confFromLabel.TCP.ServersTransports == nil {
confFromLabel.TCP.ServersTransports = make(map[string]*dynamic.TCPServersTransport)
}
serversTransportKey := itemServersTransportKey(item)
if confFromLabel.TCP.ServersTransports[serversTransportKey] == nil {
confFromLabel.TCP.ServersTransports[serversTransportKey] = certInfo.tcpServersTransport(item)
}
}
if err := p.buildTCPServiceConfiguration(item, confFromLabel.TCP); err != nil {
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Send()
2019-10-15 15:34:08 +00:00
continue
}
2019-10-15 15:34:08 +00:00
provider.BuildTCPRouterConfiguration(ctxSvc, confFromLabel.TCP)
2020-02-20 21:24:05 +00:00
}
2019-10-15 15:34:08 +00:00
2020-02-20 21:24:05 +00:00
if len(confFromLabel.UDP.Routers) > 0 || len(confFromLabel.UDP.Services) > 0 {
tcpOrUDP = true
if err := p.buildUDPServiceConfiguration(item, confFromLabel.UDP); err != nil {
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Send()
2019-10-15 15:34:08 +00:00
continue
}
2020-02-20 21:24:05 +00:00
provider.BuildUDPRouterConfiguration(ctxSvc, confFromLabel.UDP)
}
if tcpOrUDP && len(confFromLabel.HTTP.Routers) == 0 &&
len(confFromLabel.HTTP.Middlewares) == 0 &&
len(confFromLabel.HTTP.Services) == 0 {
configurations[svcName] = confFromLabel
continue
2019-10-15 15:34:08 +00:00
}
if item.ExtraConf.ConsulCatalog.Connect {
if confFromLabel.HTTP.ServersTransports == nil {
confFromLabel.HTTP.ServersTransports = make(map[string]*dynamic.ServersTransport)
}
serversTransportKey := itemServersTransportKey(item)
if confFromLabel.HTTP.ServersTransports[serversTransportKey] == nil {
confFromLabel.HTTP.ServersTransports[serversTransportKey] = certInfo.serversTransport(item)
}
}
if err = p.buildServiceConfiguration(item, confFromLabel.HTTP); err != nil {
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Send()
2019-10-15 15:34:08 +00:00
continue
}
model := struct {
Name string
Labels map[string]string
}{
Name: item.Name,
Labels: item.Labels,
}
provider.BuildRouterConfiguration(ctx, confFromLabel.HTTP, getName(item), p.defaultRuleTpl, model)
2019-10-15 15:34:08 +00:00
configurations[svcName] = confFromLabel
}
return provider.Merge(ctx, configurations)
}
func (p *Provider) keepContainer(ctx context.Context, item itemData) bool {
2022-11-21 17:36:05 +00:00
logger := log.Ctx(ctx)
2019-10-15 15:34:08 +00:00
if !item.ExtraConf.Enable {
2022-11-21 17:36:05 +00:00
logger.Debug().Msg("Filtering disabled item")
2019-10-15 15:34:08 +00:00
return false
}
if !p.ConnectAware && item.ExtraConf.ConsulCatalog.Connect {
2022-11-21 17:36:05 +00:00
logger.Debug().Msg("Filtering out Connect aware item, Connect support is not enabled")
return false
}
2019-11-29 16:16:05 +00:00
matches, err := constraints.MatchTags(item.Tags, p.Constraints)
2019-10-15 15:34:08 +00:00
if err != nil {
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Msg("Error matching constraint expressions")
2019-10-15 15:34:08 +00:00
return false
}
if !matches {
2022-11-21 17:36:05 +00:00
logger.Debug().Msgf("Container pruned by constraint expressions: %q", p.Constraints)
2019-10-15 15:34:08 +00:00
return false
}
if item.Status != api.HealthPassing && item.Status != api.HealthWarning {
2022-11-21 17:36:05 +00:00
logger.Debug().Msg("Filtering unhealthy or starting item")
2019-10-15 15:34:08 +00:00
return false
}
return true
}
func (p *Provider) buildTCPServiceConfiguration(item itemData, configuration *dynamic.TCPConfiguration) error {
2019-10-15 15:34:08 +00:00
if len(configuration.Services) == 0 {
configuration.Services = map[string]*dynamic.TCPService{
getName(item): {
LoadBalancer: new(dynamic.TCPServersLoadBalancer),
},
2019-10-15 15:34:08 +00:00
}
}
for name, service := range configuration.Services {
if err := p.addServerTCP(item, service.LoadBalancer); err != nil {
2022-08-31 06:24:08 +00:00
return fmt.Errorf("%s: %w", name, err)
2019-10-15 15:34:08 +00:00
}
}
return nil
}
func (p *Provider) buildUDPServiceConfiguration(item itemData, configuration *dynamic.UDPConfiguration) error {
2020-02-20 21:24:05 +00:00
if len(configuration.Services) == 0 {
configuration.Services = make(map[string]*dynamic.UDPService)
lb := &dynamic.UDPServersLoadBalancer{}
configuration.Services[getName(item)] = &dynamic.UDPService{
2020-02-20 21:24:05 +00:00
LoadBalancer: lb,
}
}
for name, service := range configuration.Services {
if err := p.addServerUDP(item, service.LoadBalancer); err != nil {
2022-08-31 06:24:08 +00:00
return fmt.Errorf("%s: %w", name, err)
2020-02-20 21:24:05 +00:00
}
}
return nil
}
func (p *Provider) buildServiceConfiguration(item itemData, configuration *dynamic.HTTPConfiguration) error {
2019-10-15 15:34:08 +00:00
if len(configuration.Services) == 0 {
configuration.Services = make(map[string]*dynamic.Service)
lb := &dynamic.ServersLoadBalancer{}
lb.SetDefaults()
configuration.Services[getName(item)] = &dynamic.Service{
2019-10-15 15:34:08 +00:00
LoadBalancer: lb,
}
}
for name, service := range configuration.Services {
if err := p.addServer(item, service.LoadBalancer); err != nil {
2022-08-31 06:24:08 +00:00
return fmt.Errorf("%s: %w", name, err)
2019-10-15 15:34:08 +00:00
}
}
return nil
}
func (p *Provider) addServerTCP(item itemData, loadBalancer *dynamic.TCPServersLoadBalancer) error {
2019-10-15 15:34:08 +00:00
if loadBalancer == nil {
return errors.New("load-balancer is not defined")
}
if len(loadBalancer.Servers) == 0 {
loadBalancer.Servers = []dynamic.TCPServer{{}}
}
if item.Address == "" {
return errors.New("address is missing")
2019-10-15 15:34:08 +00:00
}
port := loadBalancer.Servers[0].Port
loadBalancer.Servers[0].Port = ""
2019-10-15 15:34:08 +00:00
2020-02-20 21:24:05 +00:00
if port == "" {
port = item.Port
2020-02-20 21:24:05 +00:00
}
if port == "" {
return errors.New("port is missing")
2020-02-20 21:24:05 +00:00
}
if item.Address == "" {
return errors.New("address is missing")
}
if item.ExtraConf.ConsulCatalog.Connect {
loadBalancer.ServersTransport = itemServersTransportKey(item)
loadBalancer.Servers[0].TLS = true
}
2020-02-20 21:24:05 +00:00
loadBalancer.Servers[0].Address = net.JoinHostPort(item.Address, port)
2020-02-20 21:24:05 +00:00
return nil
}
func (p *Provider) addServerUDP(item itemData, loadBalancer *dynamic.UDPServersLoadBalancer) error {
2020-02-20 21:24:05 +00:00
if loadBalancer == nil {
return errors.New("load-balancer is not defined")
}
if len(loadBalancer.Servers) == 0 {
loadBalancer.Servers = []dynamic.UDPServer{{}}
}
if item.Address == "" {
return errors.New("address is missing")
2020-02-20 21:24:05 +00:00
}
port := loadBalancer.Servers[0].Port
loadBalancer.Servers[0].Port = ""
2019-10-15 15:34:08 +00:00
if port == "" {
port = item.Port
2019-10-15 15:34:08 +00:00
}
if port == "" {
return errors.New("port is missing")
}
2019-10-15 15:34:08 +00:00
loadBalancer.Servers[0].Address = net.JoinHostPort(item.Address, port)
2019-10-15 15:34:08 +00:00
return nil
}
func (p *Provider) addServer(item itemData, loadBalancer *dynamic.ServersLoadBalancer) error {
2019-10-15 15:34:08 +00:00
if loadBalancer == nil {
return errors.New("load-balancer is not defined")
}
if len(loadBalancer.Servers) == 0 {
server := dynamic.Server{}
server.SetDefaults()
loadBalancer.Servers = []dynamic.Server{server}
}
if item.Address == "" {
return errors.New("address is missing")
2019-10-15 15:34:08 +00:00
}
port := loadBalancer.Servers[0].Port
loadBalancer.Servers[0].Port = ""
2019-10-15 15:34:08 +00:00
if port == "" {
port = item.Port
2019-10-15 15:34:08 +00:00
}
if port == "" {
return errors.New("port is missing")
}
scheme := loadBalancer.Servers[0].Scheme
2019-10-15 15:34:08 +00:00
loadBalancer.Servers[0].Scheme = ""
if item.ExtraConf.ConsulCatalog.Connect {
loadBalancer.ServersTransport = itemServersTransportKey(item)
scheme = "https"
}
loadBalancer.Servers[0].URL = fmt.Sprintf("%s://%s", scheme, net.JoinHostPort(item.Address, port))
2019-10-15 15:34:08 +00:00
return nil
}
func itemServersTransportKey(item itemData) string {
return provider.Normalize("tls-" + item.Namespace + "-" + item.Datacenter + "-" + item.Name)
}
func getName(i itemData) string {
if !i.ExtraConf.ConsulCatalog.Canary {
return provider.Normalize(i.Name)
}
tags := make([]string, len(i.Tags))
copy(tags, i.Tags)
sort.Strings(tags)
hasher := fnv.New64()
hasher.Write([]byte(strings.Join(tags, "")))
return provider.Normalize(fmt.Sprintf("%s-%d", i.Name, hasher.Sum64()))
}