traefik/pkg/server/routerfactory.go

107 lines
3.5 KiB
Go
Raw Normal View History

package server
import (
"context"
2022-11-21 18:36:05 +01:00
"github.com/rs/zerolog/log"
2023-02-03 15:24:05 +01:00
"github.com/traefik/traefik/v3/pkg/config/runtime"
"github.com/traefik/traefik/v3/pkg/config/static"
"github.com/traefik/traefik/v3/pkg/server/middleware"
tcpmiddleware "github.com/traefik/traefik/v3/pkg/server/middleware/tcp"
"github.com/traefik/traefik/v3/pkg/server/router"
tcprouter "github.com/traefik/traefik/v3/pkg/server/router/tcp"
udprouter "github.com/traefik/traefik/v3/pkg/server/router/udp"
"github.com/traefik/traefik/v3/pkg/server/service"
tcpsvc "github.com/traefik/traefik/v3/pkg/server/service/tcp"
udpsvc "github.com/traefik/traefik/v3/pkg/server/service/udp"
"github.com/traefik/traefik/v3/pkg/tcp"
"github.com/traefik/traefik/v3/pkg/tls"
"github.com/traefik/traefik/v3/pkg/udp"
)
// RouterFactory the factory of TCP/UDP routers.
type RouterFactory struct {
entryPointsTCP []string
entryPointsUDP []string
managerFactory *service.ManagerFactory
2020-09-07 13:58:03 +02:00
pluginBuilder middleware.PluginsBuilder
2020-04-20 18:36:34 +02:00
observabilityMgr *middleware.ObservabilityMgr
tlsManager *tls.Manager
dialerManager *tcp.DialerManager
cancelPrevState func()
}
2020-05-11 12:06:07 +02:00
// NewRouterFactory creates a new RouterFactory.
2021-04-30 10:22:04 +02:00
func NewRouterFactory(staticConfiguration static.Configuration, managerFactory *service.ManagerFactory, tlsManager *tls.Manager,
observabilityMgr *middleware.ObservabilityMgr, pluginBuilder middleware.PluginsBuilder, dialerManager *tcp.DialerManager,
2022-03-21 10:42:08 +01:00
) *RouterFactory {
var entryPointsTCP, entryPointsUDP []string
for name, cfg := range staticConfiguration.EntryPoints {
protocol, err := cfg.GetProtocol()
if err != nil {
// Should never happen because Traefik should not start if protocol is invalid.
2022-11-21 18:36:05 +01:00
log.Error().Err(err).Msg("Invalid protocol")
}
if protocol == "udp" {
entryPointsUDP = append(entryPointsUDP, name)
} else {
entryPointsTCP = append(entryPointsTCP, name)
}
}
return &RouterFactory{
entryPointsTCP: entryPointsTCP,
entryPointsUDP: entryPointsUDP,
managerFactory: managerFactory,
observabilityMgr: observabilityMgr,
tlsManager: tlsManager,
pluginBuilder: pluginBuilder,
dialerManager: dialerManager,
}
}
2020-05-11 12:06:07 +02:00
// CreateRouters creates new TCPRouters and UDPRouters.
func (f *RouterFactory) CreateRouters(rtConf *runtime.Configuration) (map[string]*tcprouter.Router, map[string]udp.Handler) {
if f.cancelPrevState != nil {
f.cancelPrevState()
}
var ctx context.Context
ctx, f.cancelPrevState = context.WithCancel(context.Background())
// HTTP
serviceManager := f.managerFactory.Build(rtConf)
2020-04-20 18:36:34 +02:00
middlewaresBuilder := middleware.NewBuilder(rtConf.Middlewares, serviceManager, f.pluginBuilder)
routerManager := router.NewManager(rtConf, serviceManager, middlewaresBuilder, f.observabilityMgr, f.tlsManager)
handlersNonTLS := routerManager.BuildHandlers(ctx, f.entryPointsTCP, false)
handlersTLS := routerManager.BuildHandlers(ctx, f.entryPointsTCP, true)
serviceManager.LaunchHealthCheck(ctx)
2020-02-29 00:13:44 +01:00
// TCP
svcTCPManager := tcpsvc.NewManager(rtConf, f.dialerManager)
2022-03-17 11:02:08 -06:00
middlewaresTCPBuilder := tcpmiddleware.NewBuilder(rtConf.TCPMiddlewares)
2021-06-11 15:30:05 +02:00
2022-03-17 11:02:08 -06:00
rtTCPManager := tcprouter.NewManager(rtConf, svcTCPManager, middlewaresTCPBuilder, handlersNonTLS, handlersTLS, f.tlsManager)
routersTCP := rtTCPManager.BuildHandlers(ctx, f.entryPointsTCP)
// UDP
svcUDPManager := udpsvc.NewManager(rtConf)
2022-03-17 11:02:08 -06:00
rtUDPManager := udprouter.NewManager(rtConf, svcUDPManager)
routersUDP := rtUDPManager.BuildHandlers(ctx, f.entryPointsUDP)
rtConf.PopulateUsedBy()
return routersTCP, routersUDP
}