2022-06-10 16:32:08 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-08-01 15:52:08 +00:00
|
|
|
"hash/fnv"
|
2022-06-10 16:32:08 +00:00
|
|
|
"net"
|
2022-08-01 15:52:08 +00:00
|
|
|
"sort"
|
2022-06-10 16:32:08 +00:00
|
|
|
"strconv"
|
2022-08-01 15:52:08 +00:00
|
|
|
"strings"
|
2022-06-10 16:32:08 +00:00
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-06-10 16:32:08 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/label"
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/logs"
|
2022-06-10 16:32:08 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/provider"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/provider/constraints"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *Provider) buildConfig(ctx context.Context, items []item) *dynamic.Configuration {
|
|
|
|
configurations := make(map[string]*dynamic.Configuration)
|
|
|
|
|
|
|
|
for _, i := range items {
|
|
|
|
svcName := provider.Normalize(i.Node + "-" + i.Name + "-" + i.ID)
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx).With().Str(logs.ServiceName, svcName).Logger()
|
|
|
|
ctxSvc := logger.WithContext(ctx)
|
2022-06-10 16:32:08 +00:00
|
|
|
|
|
|
|
if !p.keepItem(ctxSvc, i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := tagsToLabels(i.Tags, p.Prefix)
|
|
|
|
|
|
|
|
config, err := label.DecodeConfiguration(labels)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Failed to decode configuration")
|
2022-06-10 16:32:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var tcpOrUDP bool
|
|
|
|
|
|
|
|
if len(config.TCP.Routers) > 0 || len(config.TCP.Services) > 0 {
|
|
|
|
tcpOrUDP = true
|
|
|
|
if err := p.buildTCPConfig(i, config.TCP); err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Failed to build TCP service configuration")
|
2022-06-10 16:32:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
provider.BuildTCPRouterConfiguration(ctxSvc, config.TCP)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.UDP.Routers) > 0 || len(config.UDP.Services) > 0 {
|
|
|
|
tcpOrUDP = true
|
|
|
|
if err := p.buildUDPConfig(i, config.UDP); err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Failed to build UDP service configuration")
|
2022-06-10 16:32:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
provider.BuildUDPRouterConfiguration(ctxSvc, config.UDP)
|
|
|
|
}
|
|
|
|
|
|
|
|
// tcp/udp, skip configuring http service
|
|
|
|
if tcpOrUDP && len(config.HTTP.Routers) == 0 &&
|
|
|
|
len(config.HTTP.Middlewares) == 0 &&
|
|
|
|
len(config.HTTP.Services) == 0 {
|
|
|
|
configurations[svcName] = config
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure http service
|
|
|
|
if err := p.buildServiceConfig(i, config.HTTP); err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Failed to build HTTP service configuration")
|
2022-06-10 16:32:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
model := struct {
|
|
|
|
Name string
|
|
|
|
Labels map[string]string
|
|
|
|
}{
|
|
|
|
Name: i.Name,
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
|
2022-08-01 15:52:08 +00:00
|
|
|
provider.BuildRouterConfiguration(ctx, config.HTTP, getName(i), p.defaultRuleTpl, model)
|
2022-06-10 16:32:08 +00:00
|
|
|
configurations[svcName] = config
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider.Merge(ctx, configurations)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) buildTCPConfig(i item, configuration *dynamic.TCPConfiguration) error {
|
|
|
|
if len(configuration.Services) == 0 {
|
2022-12-09 08:58:05 +00:00
|
|
|
configuration.Services = map[string]*dynamic.TCPService{
|
|
|
|
getName(i): {
|
|
|
|
LoadBalancer: new(dynamic.TCPServersLoadBalancer),
|
|
|
|
},
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range configuration.Services {
|
|
|
|
if err := p.addServerTCP(i, service.LoadBalancer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) buildUDPConfig(i item, configuration *dynamic.UDPConfiguration) error {
|
|
|
|
if len(configuration.Services) == 0 {
|
|
|
|
configuration.Services = make(map[string]*dynamic.UDPService)
|
|
|
|
|
2022-08-01 15:52:08 +00:00
|
|
|
configuration.Services[getName(i)] = &dynamic.UDPService{
|
2022-06-10 16:32:08 +00:00
|
|
|
LoadBalancer: new(dynamic.UDPServersLoadBalancer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range configuration.Services {
|
|
|
|
if err := p.addServerUDP(i, service.LoadBalancer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) buildServiceConfig(i item, configuration *dynamic.HTTPConfiguration) error {
|
|
|
|
if len(configuration.Services) == 0 {
|
|
|
|
configuration.Services = make(map[string]*dynamic.Service)
|
|
|
|
|
|
|
|
lb := new(dynamic.ServersLoadBalancer)
|
|
|
|
lb.SetDefaults()
|
|
|
|
|
2022-08-01 15:52:08 +00:00
|
|
|
configuration.Services[getName(i)] = &dynamic.Service{
|
2022-06-10 16:32:08 +00:00
|
|
|
LoadBalancer: lb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range configuration.Services {
|
|
|
|
if err := p.addServer(i, service.LoadBalancer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check whether it is mandatory to filter again.
|
|
|
|
func (p *Provider) keepItem(ctx context.Context, i item) bool {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx)
|
2022-06-10 16:32:08 +00:00
|
|
|
|
|
|
|
if !i.ExtraConf.Enable {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Filtering disabled item")
|
2022-06-10 16:32:08 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
matches, err := constraints.MatchTags(i.Tags, p.Constraints)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Error matching constraint expressions")
|
2022-06-10 16:32:08 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !matches {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msgf("Filtering out item due to constraints: %q", p.Constraints)
|
2022-06-10 16:32:08 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: filter on health when that information exists (nomad 1.4+)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) addServerTCP(i item, lb *dynamic.TCPServersLoadBalancer) error {
|
|
|
|
if lb == nil {
|
|
|
|
return errors.New("load-balancer is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lb.Servers) == 0 {
|
|
|
|
lb.Servers = []dynamic.TCPServer{{}}
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if i.Address == "" {
|
|
|
|
return errors.New("address is missing")
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
2022-09-21 12:54:08 +00:00
|
|
|
|
|
|
|
port := lb.Servers[0].Port
|
2022-06-10 16:32:08 +00:00
|
|
|
lb.Servers[0].Port = ""
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if port == "" && i.Port > 0 {
|
|
|
|
port = strconv.Itoa(i.Port)
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if port == "" {
|
|
|
|
return errors.New("port is missing")
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lb.Servers[0].Address = net.JoinHostPort(i.Address, port)
|
2022-09-21 12:54:08 +00:00
|
|
|
|
2022-06-10 16:32:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) addServerUDP(i item, lb *dynamic.UDPServersLoadBalancer) error {
|
|
|
|
if lb == nil {
|
|
|
|
return errors.New("load-balancer is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lb.Servers) == 0 {
|
|
|
|
lb.Servers = []dynamic.UDPServer{{}}
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if i.Address == "" {
|
|
|
|
return errors.New("address is missing")
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
2022-09-21 12:54:08 +00:00
|
|
|
|
|
|
|
port := lb.Servers[0].Port
|
2022-06-10 16:32:08 +00:00
|
|
|
lb.Servers[0].Port = ""
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if port == "" && i.Port > 0 {
|
|
|
|
port = strconv.Itoa(i.Port)
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if port == "" {
|
|
|
|
return errors.New("port is missing")
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lb.Servers[0].Address = net.JoinHostPort(i.Address, port)
|
2022-09-21 12:54:08 +00:00
|
|
|
|
2022-06-10 16:32:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) addServer(i item, lb *dynamic.ServersLoadBalancer) error {
|
|
|
|
if lb == nil {
|
|
|
|
return errors.New("load-balancer is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lb.Servers) == 0 {
|
|
|
|
server := dynamic.Server{}
|
|
|
|
server.SetDefaults()
|
|
|
|
|
|
|
|
lb.Servers = []dynamic.Server{server}
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if i.Address == "" {
|
|
|
|
return errors.New("address is missing")
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
2022-09-21 12:54:08 +00:00
|
|
|
|
|
|
|
port := lb.Servers[0].Port
|
2022-06-10 16:32:08 +00:00
|
|
|
lb.Servers[0].Port = ""
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if port == "" && i.Port > 0 {
|
|
|
|
port = strconv.Itoa(i.Port)
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 12:54:08 +00:00
|
|
|
if port == "" {
|
|
|
|
return errors.New("port is missing")
|
2022-06-10 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scheme := lb.Servers[0].Scheme
|
|
|
|
lb.Servers[0].Scheme = ""
|
|
|
|
lb.Servers[0].URL = fmt.Sprintf("%s://%s", scheme, net.JoinHostPort(i.Address, port))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-01 15:52:08 +00:00
|
|
|
|
|
|
|
func getName(i item) string {
|
|
|
|
if !i.ExtraConf.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()))
|
|
|
|
}
|