2018-11-14 09:18:03 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-19 14:42:04 +00:00
|
|
|
"errors"
|
2022-12-06 17:28:05 +00:00
|
|
|
"fmt"
|
2018-11-14 09:18:03 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/containous/alice"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/runtime"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2021-04-30 08:22:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/metrics"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/accesslog"
|
2021-04-30 08:22:04 +00:00
|
|
|
metricsMiddle "github.com/traefik/traefik/v2/pkg/middlewares/metrics"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/recovery"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/tracing"
|
2022-03-17 17:02:08 +00:00
|
|
|
httpmuxer "github.com/traefik/traefik/v2/pkg/muxer/http"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/server/middleware"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/server/provider"
|
2022-12-06 17:28:05 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/tls"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
2019-11-14 15:40:05 +00:00
|
|
|
type middlewareBuilder interface {
|
|
|
|
BuildChain(ctx context.Context, names []string) *alice.Chain
|
|
|
|
}
|
|
|
|
|
|
|
|
type serviceManager interface {
|
2020-09-01 16:16:04 +00:00
|
|
|
BuildHTTP(rootCtx context.Context, serviceName string) (http.Handler, error)
|
2019-11-14 15:40:05 +00:00
|
|
|
LaunchHealthCheck()
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Manager A route/router manager.
|
2019-11-14 15:40:05 +00:00
|
|
|
type Manager struct {
|
|
|
|
routerHandlers map[string]http.Handler
|
|
|
|
serviceManager serviceManager
|
2021-04-30 08:22:04 +00:00
|
|
|
metricsRegistry metrics.Registry
|
2019-11-14 15:40:05 +00:00
|
|
|
middlewaresBuilder middlewareBuilder
|
|
|
|
chainBuilder *middleware.ChainBuilder
|
|
|
|
conf *runtime.Configuration
|
2022-12-06 17:28:05 +00:00
|
|
|
tlsManager *tls.Manager
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 17:28:05 +00:00
|
|
|
// NewManager creates a new Manager.
|
|
|
|
func NewManager(conf *runtime.Configuration, serviceManager serviceManager, middlewaresBuilder middlewareBuilder, chainBuilder *middleware.ChainBuilder, metricsRegistry metrics.Registry, tlsManager *tls.Manager) *Manager {
|
2018-11-14 09:18:03 +00:00
|
|
|
return &Manager{
|
|
|
|
routerHandlers: make(map[string]http.Handler),
|
|
|
|
serviceManager: serviceManager,
|
2021-04-30 08:22:04 +00:00
|
|
|
metricsRegistry: metricsRegistry,
|
2018-11-14 09:18:03 +00:00
|
|
|
middlewaresBuilder: middlewaresBuilder,
|
2019-11-14 15:40:05 +00:00
|
|
|
chainBuilder: chainBuilder,
|
2019-06-17 16:14:08 +00:00
|
|
|
conf: conf,
|
2022-12-06 17:28:05 +00:00
|
|
|
tlsManager: tlsManager,
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
func (m *Manager) getHTTPRouters(ctx context.Context, entryPoints []string, tls bool) map[string]map[string]*runtime.RouterInfo {
|
2019-06-17 16:14:08 +00:00
|
|
|
if m.conf != nil {
|
2019-07-15 15:04:04 +00:00
|
|
|
return m.conf.GetRoutersByEntryPoints(ctx, entryPoints, tls)
|
2019-06-17 16:14:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
return make(map[string]map[string]*runtime.RouterInfo)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// BuildHandlers Builds handler for all entry points.
|
2019-03-14 08:30:04 +00:00
|
|
|
func (m *Manager) BuildHandlers(rootCtx context.Context, entryPoints []string, tls bool) map[string]http.Handler {
|
2018-11-14 09:18:03 +00:00
|
|
|
entryPointHandlers := make(map[string]http.Handler)
|
2019-06-17 16:14:08 +00:00
|
|
|
|
|
|
|
for entryPointName, routers := range m.getHTTPRouters(rootCtx, entryPoints, tls) {
|
2019-01-30 15:24:07 +00:00
|
|
|
entryPointName := entryPointName
|
2018-11-14 09:18:03 +00:00
|
|
|
ctx := log.With(rootCtx, log.Str(log.EntryPointName, entryPointName))
|
|
|
|
|
|
|
|
handler, err := m.buildEntryPointHandler(ctx, routers)
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(ctx).Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-01-18 14:18:04 +00:00
|
|
|
|
|
|
|
handlerWithAccessLog, err := alice.New(func(next http.Handler) (http.Handler, error) {
|
|
|
|
return accesslog.NewFieldHandler(next, log.EntryPointName, entryPointName, accesslog.AddOriginFields), nil
|
|
|
|
}).Then(handler)
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(ctx).Error(err)
|
|
|
|
entryPointHandlers[entryPointName] = handler
|
|
|
|
} else {
|
|
|
|
entryPointHandlers[entryPointName] = handlerWithAccessLog
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 15:40:05 +00:00
|
|
|
for _, entryPointName := range entryPoints {
|
|
|
|
ctx := log.With(rootCtx, log.Str(log.EntryPointName, entryPointName))
|
|
|
|
|
|
|
|
handler, ok := entryPointHandlers[entryPointName]
|
|
|
|
if !ok || handler == nil {
|
|
|
|
handler = BuildDefaultHTTPRouter()
|
|
|
|
}
|
|
|
|
|
|
|
|
handlerWithMiddlewares, err := m.chainBuilder.Build(ctx, entryPointName).Then(handler)
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(ctx).Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
entryPointHandlers[entryPointName] = handlerWithMiddlewares
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
return entryPointHandlers
|
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
func (m *Manager) buildEntryPointHandler(ctx context.Context, configs map[string]*runtime.RouterInfo) (http.Handler, error) {
|
2022-03-17 17:02:08 +00:00
|
|
|
muxer, err := httpmuxer.NewMuxer()
|
2019-01-30 15:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
for routerName, routerConfig := range configs {
|
2020-01-27 09:40:05 +00:00
|
|
|
ctxRouter := log.With(provider.AddInContext(ctx, routerName), log.Str(log.RouterName, routerName))
|
2019-01-30 15:24:07 +00:00
|
|
|
logger := log.FromContext(ctxRouter)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-06-17 16:14:08 +00:00
|
|
|
handler, err := m.buildRouterHandler(ctxRouter, routerName, routerConfig)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
2019-07-15 15:04:04 +00:00
|
|
|
routerConfig.AddError(err, true)
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
err = muxer.AddRoute(routerConfig.Rule, routerConfig.Priority, handler)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
2019-07-15 15:04:04 +00:00
|
|
|
routerConfig.AddError(err, true)
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
muxer.SortRoutes()
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
chain := alice.New()
|
|
|
|
chain = chain.Append(func(next http.Handler) (http.Handler, error) {
|
2021-01-21 09:04:04 +00:00
|
|
|
return recovery.New(ctx, next)
|
2018-11-14 09:18:03 +00:00
|
|
|
})
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
return chain.Then(muxer)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
func (m *Manager) buildRouterHandler(ctx context.Context, routerName string, routerConfig *runtime.RouterInfo) (http.Handler, error) {
|
2018-11-14 09:18:03 +00:00
|
|
|
if handler, ok := m.routerHandlers[routerName]; ok {
|
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
2022-12-06 17:28:05 +00:00
|
|
|
if routerConfig.TLS != nil {
|
|
|
|
// Don't build the router if the TLSOptions configuration is invalid.
|
|
|
|
tlsOptionsName := tls.DefaultTLSConfigName
|
|
|
|
if len(routerConfig.TLS.Options) > 0 && routerConfig.TLS.Options != tls.DefaultTLSConfigName {
|
|
|
|
tlsOptionsName = provider.GetQualifiedName(ctx, routerConfig.TLS.Options)
|
|
|
|
}
|
|
|
|
if _, err := m.tlsManager.Get(tls.DefaultTLSStoreName, tlsOptionsName); err != nil {
|
|
|
|
return nil, fmt.Errorf("building router handler: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 16:14:08 +00:00
|
|
|
handler, err := m.buildHTTPHandler(ctx, routerConfig, routerName)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
handlerWithAccessLog, err := alice.New(func(next http.Handler) (http.Handler, error) {
|
|
|
|
return accesslog.NewFieldHandler(next, accesslog.RouterName, routerName, nil), nil
|
|
|
|
}).Then(handler)
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(ctx).Error(err)
|
|
|
|
m.routerHandlers[routerName] = handler
|
|
|
|
} else {
|
|
|
|
m.routerHandlers[routerName] = handlerWithAccessLog
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.routerHandlers[routerName], nil
|
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
func (m *Manager) buildHTTPHandler(ctx context.Context, router *runtime.RouterInfo, routerName string) (http.Handler, error) {
|
2019-09-25 16:20:04 +00:00
|
|
|
var qualifiedNames []string
|
|
|
|
for _, name := range router.Middlewares {
|
2020-01-27 09:40:05 +00:00
|
|
|
qualifiedNames = append(qualifiedNames, provider.GetQualifiedName(ctx, name))
|
2019-04-04 09:50:04 +00:00
|
|
|
}
|
2019-09-10 14:12:05 +00:00
|
|
|
router.Middlewares = qualifiedNames
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-07-19 14:42:04 +00:00
|
|
|
if router.Service == "" {
|
|
|
|
return nil, errors.New("the service is missing on the router")
|
|
|
|
}
|
|
|
|
|
2020-09-01 16:16:04 +00:00
|
|
|
sHandler, err := m.serviceManager.BuildHTTP(ctx, router.Service)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:28:04 +00:00
|
|
|
mHandler := m.middlewaresBuilder.BuildChain(ctx, router.Middlewares)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
tHandler := func(next http.Handler) (http.Handler, error) {
|
|
|
|
return tracing.NewForwarder(ctx, routerName, router.Service, next), nil
|
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
chain := alice.New()
|
|
|
|
|
|
|
|
if m.metricsRegistry != nil && m.metricsRegistry.IsRouterEnabled() {
|
2021-09-15 15:26:06 +00:00
|
|
|
chain = chain.Append(metricsMiddle.WrapRouterHandler(ctx, m.metricsRegistry, routerName, provider.GetQualifiedName(ctx, router.Service)))
|
2021-04-30 08:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return chain.Extend(*mHandler).Append(tHandler).Then(sHandler)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
|
|
|
|
// BuildDefaultHTTPRouter creates a default HTTP router.
|
|
|
|
func BuildDefaultHTTPRouter() http.Handler {
|
|
|
|
return http.NotFoundHandler()
|
|
|
|
}
|