2019-07-15 15:04:04 +00:00
|
|
|
package runtime
|
2019-05-16 08:58:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/log"
|
2019-05-16 08:58:06 +00:00
|
|
|
)
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
// Status of the router/service
|
|
|
|
const (
|
|
|
|
StatusEnabled = "enabled"
|
|
|
|
StatusDisabled = "disabled"
|
|
|
|
StatusWarning = "warning"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Configuration holds the information about the currently running traefik instance.
|
|
|
|
type Configuration struct {
|
2019-05-16 08:58:06 +00:00
|
|
|
Routers map[string]*RouterInfo `json:"routers,omitempty"`
|
|
|
|
Middlewares map[string]*MiddlewareInfo `json:"middlewares,omitempty"`
|
|
|
|
Services map[string]*ServiceInfo `json:"services,omitempty"`
|
|
|
|
TCPRouters map[string]*TCPRouterInfo `json:"tcpRouters,omitempty"`
|
|
|
|
TCPServices map[string]*TCPServiceInfo `json:"tcpServices,omitempty"`
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-05-16 08:58:06 +00:00
|
|
|
if conf.HTTP == nil && conf.TCP == 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := log.WithoutContext()
|
|
|
|
|
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 == "" {
|
|
|
|
logger.WithField(log.RouterName, routerName).Error("router name is not fully qualified")
|
|
|
|
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 == "" {
|
|
|
|
logger.WithField(log.RouterName, routerName).Error("tcp router name is not fully qualified")
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|