2020-02-11 00:26:04 +00:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-02-17 17:02:04 +00:00
|
|
|
"sort"
|
2020-02-11 00:26:04 +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/runtime"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/logs"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/server/provider"
|
|
|
|
udpservice "github.com/traefik/traefik/v3/pkg/server/service/udp"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/udp"
|
2020-02-11 00:26:04 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// NewManager Creates a new Manager.
|
2020-02-11 00:26:04 +00:00
|
|
|
func NewManager(conf *runtime.Configuration,
|
|
|
|
serviceManager *udpservice.Manager,
|
|
|
|
) *Manager {
|
|
|
|
return &Manager{
|
|
|
|
serviceManager: serviceManager,
|
|
|
|
conf: conf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Manager is a route/router manager.
|
2020-02-11 00:26:04 +00:00
|
|
|
type Manager struct {
|
|
|
|
serviceManager *udpservice.Manager
|
|
|
|
conf *runtime.Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) getUDPRouters(ctx context.Context, entryPoints []string) map[string]map[string]*runtime.UDPRouterInfo {
|
|
|
|
if m.conf != nil {
|
|
|
|
return m.conf.GetUDPRoutersByEntryPoints(ctx, entryPoints)
|
|
|
|
}
|
|
|
|
|
|
|
|
return make(map[string]map[string]*runtime.UDPRouterInfo)
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// BuildHandlers builds the handlers for the given entrypoints.
|
2020-02-11 00:26:04 +00:00
|
|
|
func (m *Manager) BuildHandlers(rootCtx context.Context, entryPoints []string) map[string]udp.Handler {
|
|
|
|
entryPointsRouters := m.getUDPRouters(rootCtx, entryPoints)
|
|
|
|
|
|
|
|
entryPointHandlers := make(map[string]udp.Handler)
|
|
|
|
for _, entryPointName := range entryPoints {
|
|
|
|
routers := entryPointsRouters[entryPointName]
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(rootCtx).With().Str(logs.EntryPointName, entryPointName).Logger()
|
|
|
|
ctx := logger.WithContext(rootCtx)
|
2020-02-11 00:26:04 +00:00
|
|
|
|
2020-02-17 17:02:04 +00:00
|
|
|
if len(routers) > 1 {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Warn().Msg("Config has more than one udp router for a given entrypoint.")
|
2020-02-17 17:02:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:24:08 +00:00
|
|
|
handlers := m.buildEntryPointHandlers(ctx, routers)
|
2020-02-17 17:02:04 +00:00
|
|
|
|
|
|
|
if len(handlers) > 0 {
|
|
|
|
// As UDP support only one router per entrypoint, we only take the first one.
|
|
|
|
entryPointHandlers[entryPointName] = handlers[0]
|
|
|
|
}
|
2020-02-11 00:26:04 +00:00
|
|
|
}
|
|
|
|
return entryPointHandlers
|
|
|
|
}
|
|
|
|
|
2022-08-31 06:24:08 +00:00
|
|
|
func (m *Manager) buildEntryPointHandlers(ctx context.Context, configs map[string]*runtime.UDPRouterInfo) []udp.Handler {
|
2020-02-17 17:02:04 +00:00
|
|
|
var rtNames []string
|
|
|
|
for routerName := range configs {
|
|
|
|
rtNames = append(rtNames, routerName)
|
2020-02-11 00:26:04 +00:00
|
|
|
}
|
2020-02-17 17:02:04 +00:00
|
|
|
|
|
|
|
sort.Slice(rtNames, func(i, j int) bool {
|
|
|
|
return rtNames[i] > rtNames[j]
|
|
|
|
})
|
|
|
|
|
|
|
|
var handlers []udp.Handler
|
|
|
|
|
|
|
|
for _, routerName := range rtNames {
|
|
|
|
routerConfig := configs[routerName]
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx).With().Str(logs.RouterName, routerName).Logger()
|
|
|
|
ctxRouter := logger.WithContext(provider.AddInContext(ctx, routerName))
|
2020-02-11 00:26:04 +00:00
|
|
|
|
|
|
|
if routerConfig.Service == "" {
|
|
|
|
err := errors.New("the service is missing on the udp router")
|
|
|
|
routerConfig.AddError(err, true)
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2020-02-11 00:26:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, err := m.serviceManager.BuildUDP(ctxRouter, routerConfig.Service)
|
|
|
|
if err != nil {
|
|
|
|
routerConfig.AddError(err, true)
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2020-02-11 00:26:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-02-17 17:02:04 +00:00
|
|
|
|
|
|
|
handlers = append(handlers, handler)
|
2020-02-11 00:26:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:24:08 +00:00
|
|
|
return handlers
|
2020-02-11 00:26:04 +00:00
|
|
|
}
|