Build all UDP services on an entrypoint

This commit is contained in:
Julien Salleyron 2020-02-17 18:02:04 +01:00 committed by GitHub
parent 1c764052f7
commit ee6d28b25e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 48 deletions

View file

@ -3,6 +3,7 @@ package udp
import (
"context"
"errors"
"sort"
"github.com/containous/traefik/v2/pkg/config/runtime"
"github.com/containous/traefik/v2/pkg/log"
@ -47,23 +48,39 @@ func (m *Manager) BuildHandlers(rootCtx context.Context, entryPoints []string) m
ctx := log.With(rootCtx, log.Str(log.EntryPointName, entryPointName))
handler, err := m.buildEntryPointHandler(ctx, routers)
if len(routers) > 1 {
log.FromContext(ctx).Warn("Config has more than one udp router for a given entrypoint.")
}
handlers, err := m.buildEntryPointHandlers(ctx, routers)
if err != nil {
log.FromContext(ctx).Error(err)
continue
}
entryPointHandlers[entryPointName] = handler
if len(handlers) > 0 {
// As UDP support only one router per entrypoint, we only take the first one.
entryPointHandlers[entryPointName] = handlers[0]
}
}
return entryPointHandlers
}
func (m *Manager) buildEntryPointHandler(ctx context.Context, configs map[string]*runtime.UDPRouterInfo) (udp.Handler, error) {
logger := log.FromContext(ctx)
if len(configs) > 1 {
logger.Warn("Warning: config has more than one udp router for a given entrypoint")
func (m *Manager) buildEntryPointHandlers(ctx context.Context, configs map[string]*runtime.UDPRouterInfo) ([]udp.Handler, error) {
var rtNames []string
for routerName := range configs {
rtNames = append(rtNames, routerName)
}
for routerName, routerConfig := range configs {
sort.Slice(rtNames, func(i, j int) bool {
return rtNames[i] > rtNames[j]
})
var handlers []udp.Handler
for _, routerName := range rtNames {
routerConfig := configs[routerName]
ctxRouter := log.With(provider.AddInContext(ctx, routerName), log.Str(log.RouterName, routerName))
logger := log.FromContext(ctxRouter)
@ -80,8 +97,9 @@ func (m *Manager) buildEntryPointHandler(ctx context.Context, configs map[string
logger.Error(err)
continue
}
return handler, nil
handlers = append(handlers, handler)
}
return nil, nil
return handlers, nil
}