2020-02-11 03:26:04 +03:00
package server
import (
"context"
"github.com/containous/traefik/v2/pkg/config/runtime"
"github.com/containous/traefik/v2/pkg/config/static"
"github.com/containous/traefik/v2/pkg/log"
2020-04-20 19:36:34 +03:00
"github.com/containous/traefik/v2/pkg/plugins"
2020-02-11 03:26:04 +03:00
"github.com/containous/traefik/v2/pkg/server/middleware"
"github.com/containous/traefik/v2/pkg/server/router"
routertcp "github.com/containous/traefik/v2/pkg/server/router/tcp"
routerudp "github.com/containous/traefik/v2/pkg/server/router/udp"
"github.com/containous/traefik/v2/pkg/server/service"
"github.com/containous/traefik/v2/pkg/server/service/tcp"
"github.com/containous/traefik/v2/pkg/server/service/udp"
tcpCore "github.com/containous/traefik/v2/pkg/tcp"
"github.com/containous/traefik/v2/pkg/tls"
udpCore "github.com/containous/traefik/v2/pkg/udp"
)
// RouterFactory the factory of TCP/UDP routers.
type RouterFactory struct {
entryPointsTCP [ ] string
entryPointsUDP [ ] string
managerFactory * service . ManagerFactory
2020-04-20 19:36:34 +03:00
pluginBuilder * plugins . Builder
2020-02-11 03:26:04 +03:00
chainBuilder * middleware . ChainBuilder
tlsManager * tls . Manager
}
2020-05-11 13:06:07 +03:00
// NewRouterFactory creates a new RouterFactory.
2020-04-20 19:36:34 +03:00
func NewRouterFactory ( staticConfiguration static . Configuration , managerFactory * service . ManagerFactory , tlsManager * tls . Manager , chainBuilder * middleware . ChainBuilder , pluginBuilder * plugins . Builder ) * RouterFactory {
2020-02-11 03:26:04 +03:00
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.
log . WithoutContext ( ) . Errorf ( "Invalid protocol: %v" , err )
}
if protocol == "udp" {
entryPointsUDP = append ( entryPointsUDP , name )
} else {
entryPointsTCP = append ( entryPointsTCP , name )
}
}
return & RouterFactory {
entryPointsTCP : entryPointsTCP ,
entryPointsUDP : entryPointsUDP ,
managerFactory : managerFactory ,
tlsManager : tlsManager ,
chainBuilder : chainBuilder ,
2020-04-20 19:36:34 +03:00
pluginBuilder : pluginBuilder ,
2020-02-11 03:26:04 +03:00
}
}
2020-05-11 13:06:07 +03:00
// CreateRouters creates new TCPRouters and UDPRouters.
2020-05-04 12:40:46 +03:00
func ( f * RouterFactory ) CreateRouters ( rtConf * runtime . Configuration ) ( map [ string ] * tcpCore . Router , map [ string ] udpCore . Handler ) {
2020-02-11 03:26:04 +03:00
ctx := context . Background ( )
// HTTP
serviceManager := f . managerFactory . Build ( rtConf )
2020-04-20 19:36:34 +03:00
middlewaresBuilder := middleware . NewBuilder ( rtConf . Middlewares , serviceManager , f . pluginBuilder )
2020-02-11 03:26:04 +03:00
2020-09-01 19:16:04 +03:00
routerManager := router . NewManager ( rtConf , serviceManager , middlewaresBuilder , f . chainBuilder )
2020-02-11 03:26:04 +03:00
handlersNonTLS := routerManager . BuildHandlers ( ctx , f . entryPointsTCP , false )
handlersTLS := routerManager . BuildHandlers ( ctx , f . entryPointsTCP , true )
2020-02-29 02:13:44 +03:00
serviceManager . LaunchHealthCheck ( )
2020-02-11 03:26:04 +03:00
// TCP
svcTCPManager := tcp . NewManager ( rtConf )
rtTCPManager := routertcp . NewManager ( rtConf , svcTCPManager , handlersNonTLS , handlersTLS , f . tlsManager )
routersTCP := rtTCPManager . BuildHandlers ( ctx , f . entryPointsTCP )
// UDP
svcUDPManager := udp . NewManager ( rtConf )
rtUDPManager := routerudp . NewManager ( rtConf , svcUDPManager )
routersUDP := rtUDPManager . BuildHandlers ( ctx , f . entryPointsUDP )
rtConf . PopulateUsedBy ( )
return routersTCP , routersUDP
}