2019-07-15 15:04:04 +00:00
|
|
|
package runtime
|
2019-05-16 08:58:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/logs"
|
2019-05-16 08:58:06 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Status of the router/service.
|
2019-07-15 15:04:04 +00:00
|
|
|
const (
|
|
|
|
StatusEnabled = "enabled"
|
|
|
|
StatusDisabled = "disabled"
|
|
|
|
StatusWarning = "warning"
|
|
|
|
)
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
// Status of the servers.
|
|
|
|
const (
|
|
|
|
StatusUp = "UP"
|
|
|
|
StatusDown = "DOWN"
|
|
|
|
)
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
// Configuration holds the information about the currently running traefik instance.
|
|
|
|
type Configuration struct {
|
2021-06-11 13:30:05 +00:00
|
|
|
Routers map[string]*RouterInfo `json:"routers,omitempty"`
|
|
|
|
Middlewares map[string]*MiddlewareInfo `json:"middlewares,omitempty"`
|
|
|
|
TCPMiddlewares map[string]*TCPMiddlewareInfo `json:"tcpMiddlewares,omitempty"`
|
|
|
|
Services map[string]*ServiceInfo `json:"services,omitempty"`
|
|
|
|
TCPRouters map[string]*TCPRouterInfo `json:"tcpRouters,omitempty"`
|
|
|
|
TCPServices map[string]*TCPServiceInfo `json:"tcpServices,omitempty"`
|
|
|
|
UDPRouters map[string]*UDPRouterInfo `json:"udpRouters,omitempty"`
|
|
|
|
UDPServices map[string]*UDPServiceInfo `json:"udpServices,omitempty"`
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
// NewConfig returns a Configuration initialized with the given conf. It never returns nil.
|
|
|
|
func NewConfig(conf dynamic.Configuration) *Configuration {
|
2020-02-11 00:26:04 +00:00
|
|
|
if conf.HTTP == nil && conf.TCP == nil && conf.UDP == nil {
|
2019-07-15 15:04:04 +00:00
|
|
|
return &Configuration{}
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
runtimeConfig := &Configuration{}
|
2019-05-16 08:58:06 +00:00
|
|
|
|
|
|
|
if conf.HTTP != nil {
|
|
|
|
routers := conf.HTTP.Routers
|
|
|
|
if len(routers) > 0 {
|
|
|
|
runtimeConfig.Routers = make(map[string]*RouterInfo, len(routers))
|
|
|
|
for k, v := range routers {
|
2019-07-15 15:04:04 +00:00
|
|
|
runtimeConfig.Routers[k] = &RouterInfo{Router: v, Status: StatusEnabled}
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
services := conf.HTTP.Services
|
|
|
|
if len(services) > 0 {
|
|
|
|
runtimeConfig.Services = make(map[string]*ServiceInfo, len(services))
|
|
|
|
for k, v := range services {
|
2019-07-15 15:04:04 +00:00
|
|
|
runtimeConfig.Services[k] = &ServiceInfo{Service: v, Status: StatusEnabled}
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
middlewares := conf.HTTP.Middlewares
|
|
|
|
if len(middlewares) > 0 {
|
|
|
|
runtimeConfig.Middlewares = make(map[string]*MiddlewareInfo, len(middlewares))
|
|
|
|
for k, v := range middlewares {
|
2019-07-19 14:42:04 +00:00
|
|
|
runtimeConfig.Middlewares[k] = &MiddlewareInfo{Middleware: v, Status: StatusEnabled}
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.TCP != nil {
|
|
|
|
if len(conf.TCP.Routers) > 0 {
|
|
|
|
runtimeConfig.TCPRouters = make(map[string]*TCPRouterInfo, len(conf.TCP.Routers))
|
|
|
|
for k, v := range conf.TCP.Routers {
|
2019-07-15 15:04:04 +00:00
|
|
|
runtimeConfig.TCPRouters[k] = &TCPRouterInfo{TCPRouter: v, Status: StatusEnabled}
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(conf.TCP.Services) > 0 {
|
|
|
|
runtimeConfig.TCPServices = make(map[string]*TCPServiceInfo, len(conf.TCP.Services))
|
|
|
|
for k, v := range conf.TCP.Services {
|
2019-07-15 15:04:04 +00:00
|
|
|
runtimeConfig.TCPServices[k] = &TCPServiceInfo{TCPService: v, Status: StatusEnabled}
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-11 13:30:05 +00:00
|
|
|
|
|
|
|
if len(conf.TCP.Middlewares) > 0 {
|
|
|
|
runtimeConfig.TCPMiddlewares = make(map[string]*TCPMiddlewareInfo, len(conf.TCP.Middlewares))
|
|
|
|
for k, v := range conf.TCP.Middlewares {
|
|
|
|
runtimeConfig.TCPMiddlewares[k] = &TCPMiddlewareInfo{TCPMiddleware: v, Status: StatusEnabled}
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
if conf.UDP != nil {
|
|
|
|
if len(conf.UDP.Routers) > 0 {
|
|
|
|
runtimeConfig.UDPRouters = make(map[string]*UDPRouterInfo, len(conf.UDP.Routers))
|
|
|
|
for k, v := range conf.UDP.Routers {
|
|
|
|
runtimeConfig.UDPRouters[k] = &UDPRouterInfo{UDPRouter: v, Status: StatusEnabled}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(conf.UDP.Services) > 0 {
|
|
|
|
runtimeConfig.UDPServices = make(map[string]*UDPServiceInfo, len(conf.UDP.Services))
|
|
|
|
for k, v := range conf.UDP.Services {
|
|
|
|
runtimeConfig.UDPServices[k] = &UDPServiceInfo{UDPService: v, Status: StatusEnabled}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
return runtimeConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// PopulateUsedBy populates all the UsedBy lists of the underlying fields of r,
|
|
|
|
// based on the relations between the included services, routers, and middlewares.
|
2019-07-19 14:42:04 +00:00
|
|
|
func (c *Configuration) PopulateUsedBy() {
|
|
|
|
if c == nil {
|
2019-05-16 08:58:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:42:04 +00:00
|
|
|
for routerName, routerInfo := range c.Routers {
|
2019-07-15 15:04:04 +00:00
|
|
|
// lazily initialize Status in case caller forgot to do it
|
|
|
|
if routerInfo.Status == "" {
|
|
|
|
routerInfo.Status = StatusEnabled
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
providerName := getProviderName(routerName)
|
|
|
|
if providerName == "" {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Str(logs.RouterName, routerName).Msg("Router name is not fully qualified")
|
2019-05-16 08:58:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, midName := range routerInfo.Router.Middlewares {
|
|
|
|
fullMidName := getQualifiedName(providerName, midName)
|
2019-07-19 14:42:04 +00:00
|
|
|
if _, ok := c.Middlewares[fullMidName]; !ok {
|
2019-05-16 08:58:06 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-07-19 14:42:04 +00:00
|
|
|
c.Middlewares[fullMidName].UsedBy = append(c.Middlewares[fullMidName].UsedBy, routerName)
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serviceName := getQualifiedName(providerName, routerInfo.Router.Service)
|
2019-07-19 14:42:04 +00:00
|
|
|
if _, ok := c.Services[serviceName]; !ok {
|
2019-05-16 08:58:06 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-07-19 14:42:04 +00:00
|
|
|
c.Services[serviceName].UsedBy = append(c.Services[serviceName].UsedBy, routerName)
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:42:04 +00:00
|
|
|
for k, serviceInfo := range c.Services {
|
2019-07-15 15:04:04 +00:00
|
|
|
// lazily initialize Status in case caller forgot to do it
|
|
|
|
if serviceInfo.Status == "" {
|
|
|
|
serviceInfo.Status = StatusEnabled
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:42:04 +00:00
|
|
|
sort.Strings(c.Services[k].UsedBy)
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:42:04 +00:00
|
|
|
for midName, mid := range c.Middlewares {
|
|
|
|
// lazily initialize Status in case caller forgot to do it
|
|
|
|
if mid.Status == "" {
|
|
|
|
mid.Status = StatusEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(c.Middlewares[midName].UsedBy)
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:42:04 +00:00
|
|
|
for routerName, routerInfo := range c.TCPRouters {
|
2019-07-15 15:04:04 +00:00
|
|
|
// lazily initialize Status in case caller forgot to do it
|
|
|
|
if routerInfo.Status == "" {
|
|
|
|
routerInfo.Status = StatusEnabled
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
providerName := getProviderName(routerName)
|
|
|
|
if providerName == "" {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Str(logs.RouterName, routerName).Msg("TCP router name is not fully qualified")
|
2019-05-16 08:58:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceName := getQualifiedName(providerName, routerInfo.TCPRouter.Service)
|
2019-07-19 14:42:04 +00:00
|
|
|
if _, ok := c.TCPServices[serviceName]; !ok {
|
2019-05-16 08:58:06 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-07-19 14:42:04 +00:00
|
|
|
c.TCPServices[serviceName].UsedBy = append(c.TCPServices[serviceName].UsedBy, routerName)
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:42:04 +00:00
|
|
|
for k, serviceInfo := range c.TCPServices {
|
2019-07-15 15:04:04 +00:00
|
|
|
// lazily initialize Status in case caller forgot to do it
|
|
|
|
if serviceInfo.Status == "" {
|
|
|
|
serviceInfo.Status = StatusEnabled
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:42:04 +00:00
|
|
|
sort.Strings(c.TCPServices[k].UsedBy)
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
2020-02-11 00:26:04 +00:00
|
|
|
|
2021-06-11 13:30:05 +00:00
|
|
|
for midName, mid := range c.TCPMiddlewares {
|
|
|
|
// lazily initialize Status in case caller forgot to do it
|
|
|
|
if mid.Status == "" {
|
|
|
|
mid.Status = StatusEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(c.TCPMiddlewares[midName].UsedBy)
|
|
|
|
}
|
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
for routerName, routerInfo := range c.UDPRouters {
|
|
|
|
// lazily initialize Status in case caller forgot to do it
|
|
|
|
if routerInfo.Status == "" {
|
|
|
|
routerInfo.Status = StatusEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
providerName := getProviderName(routerName)
|
|
|
|
if providerName == "" {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Str(logs.RouterName, routerName).Msg("UDP router name is not fully qualified")
|
2020-02-11 00:26:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceName := getQualifiedName(providerName, routerInfo.UDPRouter.Service)
|
|
|
|
if _, ok := c.UDPServices[serviceName]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.UDPServices[serviceName].UsedBy = append(c.UDPServices[serviceName].UsedBy, routerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, serviceInfo := range c.UDPServices {
|
|
|
|
// lazily initialize Status in case caller forgot to do it
|
|
|
|
if serviceInfo.Status == "" {
|
|
|
|
serviceInfo.Status = StatusEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(c.UDPServices[k].UsedBy)
|
|
|
|
}
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:14:08 +00:00
|
|
|
func contains(entryPoints []string, entryPointName string) bool {
|
|
|
|
for _, name := range entryPoints {
|
|
|
|
if name == entryPointName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
func getProviderName(elementName string) string {
|
2019-06-19 22:40:05 +00:00
|
|
|
parts := strings.Split(elementName, "@")
|
2019-05-16 08:58:06 +00:00
|
|
|
if len(parts) > 1 {
|
2019-06-21 07:54:04 +00:00
|
|
|
return parts[1]
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getQualifiedName(provider, elementName string) string {
|
2019-06-19 22:40:05 +00:00
|
|
|
parts := strings.Split(elementName, "@")
|
2019-05-16 08:58:06 +00:00
|
|
|
if len(parts) == 1 {
|
2019-06-21 07:54:04 +00:00
|
|
|
return elementName + "@" + provider
|
2019-05-16 08:58:06 +00:00
|
|
|
}
|
|
|
|
return elementName
|
|
|
|
}
|