1
0
Fork 0

config: deal with multiple errors and their criticality

Co-authored-by: Julien Salleyron <julien.salleyron@gmail.com>
This commit is contained in:
mpl 2019-07-15 17:04:04 +02:00 committed by Traefiker Bot
parent 62800116d3
commit 6fdd48509e
45 changed files with 725 additions and 412 deletions

View file

@ -1,4 +1,4 @@
package dynamic
package runtime
import (
"context"
@ -6,11 +6,19 @@ import (
"strings"
"sync"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/log"
)
// RuntimeConfiguration holds the information about the currently running traefik instance.
type RuntimeConfiguration struct {
// 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 {
Routers map[string]*RouterInfo `json:"routers,omitempty"`
Middlewares map[string]*MiddlewareInfo `json:"middlewares,omitempty"`
Services map[string]*ServiceInfo `json:"services,omitempty"`
@ -18,20 +26,20 @@ type RuntimeConfiguration struct {
TCPServices map[string]*TCPServiceInfo `json:"tcpServices,omitempty"`
}
// NewRuntimeConfig returns a RuntimeConfiguration initialized with the given conf. It never returns nil.
func NewRuntimeConfig(conf Configuration) *RuntimeConfiguration {
// NewConfig returns a Configuration initialized with the given conf. It never returns nil.
func NewConfig(conf dynamic.Configuration) *Configuration {
if conf.HTTP == nil && conf.TCP == nil {
return &RuntimeConfiguration{}
return &Configuration{}
}
runtimeConfig := &RuntimeConfiguration{}
runtimeConfig := &Configuration{}
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 {
runtimeConfig.Routers[k] = &RouterInfo{Router: v}
runtimeConfig.Routers[k] = &RouterInfo{Router: v, Status: StatusEnabled}
}
}
@ -39,7 +47,7 @@ func NewRuntimeConfig(conf Configuration) *RuntimeConfiguration {
if len(services) > 0 {
runtimeConfig.Services = make(map[string]*ServiceInfo, len(services))
for k, v := range services {
runtimeConfig.Services[k] = &ServiceInfo{Service: v}
runtimeConfig.Services[k] = &ServiceInfo{Service: v, Status: StatusEnabled}
}
}
@ -56,14 +64,14 @@ func NewRuntimeConfig(conf Configuration) *RuntimeConfiguration {
if len(conf.TCP.Routers) > 0 {
runtimeConfig.TCPRouters = make(map[string]*TCPRouterInfo, len(conf.TCP.Routers))
for k, v := range conf.TCP.Routers {
runtimeConfig.TCPRouters[k] = &TCPRouterInfo{TCPRouter: v}
runtimeConfig.TCPRouters[k] = &TCPRouterInfo{TCPRouter: v, Status: StatusEnabled}
}
}
if len(conf.TCP.Services) > 0 {
runtimeConfig.TCPServices = make(map[string]*TCPServiceInfo, len(conf.TCP.Services))
for k, v := range conf.TCP.Services {
runtimeConfig.TCPServices[k] = &TCPServiceInfo{TCPService: v}
runtimeConfig.TCPServices[k] = &TCPServiceInfo{TCPService: v, Status: StatusEnabled}
}
}
}
@ -73,7 +81,7 @@ func NewRuntimeConfig(conf Configuration) *RuntimeConfiguration {
// PopulateUsedBy populates all the UsedBy lists of the underlying fields of r,
// based on the relations between the included services, routers, and middlewares.
func (r *RuntimeConfiguration) PopulateUsedBy() {
func (r *Configuration) PopulateUsedBy() {
if r == nil {
return
}
@ -81,6 +89,11 @@ func (r *RuntimeConfiguration) PopulateUsedBy() {
logger := log.WithoutContext()
for routerName, routerInfo := range r.Routers {
// lazily initialize Status in case caller forgot to do it
if routerInfo.Status == "" {
routerInfo.Status = StatusEnabled
}
providerName := getProviderName(routerName)
if providerName == "" {
logger.WithField(log.RouterName, routerName).Error("router name is not fully qualified")
@ -102,7 +115,12 @@ func (r *RuntimeConfiguration) PopulateUsedBy() {
r.Services[serviceName].UsedBy = append(r.Services[serviceName].UsedBy, routerName)
}
for k := range r.Services {
for k, serviceInfo := range r.Services {
// lazily initialize Status in case caller forgot to do it
if serviceInfo.Status == "" {
serviceInfo.Status = StatusEnabled
}
sort.Strings(r.Services[k].UsedBy)
}
@ -111,6 +129,11 @@ func (r *RuntimeConfiguration) PopulateUsedBy() {
}
for routerName, routerInfo := range r.TCPRouters {
// lazily initialize Status in case caller forgot to do it
if routerInfo.Status == "" {
routerInfo.Status = StatusEnabled
}
providerName := getProviderName(routerName)
if providerName == "" {
logger.WithField(log.RouterName, routerName).Error("tcp router name is not fully qualified")
@ -124,7 +147,12 @@ func (r *RuntimeConfiguration) PopulateUsedBy() {
r.TCPServices[serviceName].UsedBy = append(r.TCPServices[serviceName].UsedBy, routerName)
}
for k := range r.TCPServices {
for k, serviceInfo := range r.TCPServices {
// lazily initialize Status in case caller forgot to do it
if serviceInfo.Status == "" {
serviceInfo.Status = StatusEnabled
}
sort.Strings(r.TCPServices[k].UsedBy)
}
}
@ -138,8 +166,8 @@ func contains(entryPoints []string, entryPointName string) bool {
return false
}
// GetRoutersByEntrypoints returns all the http routers by entrypoints name and routers name
func (r *RuntimeConfiguration) GetRoutersByEntrypoints(ctx context.Context, entryPoints []string, tls bool) map[string]map[string]*RouterInfo {
// GetRoutersByEntryPoints returns all the http routers by entry points name and routers name
func (r *Configuration) GetRoutersByEntryPoints(ctx context.Context, entryPoints []string, tls bool) map[string]map[string]*RouterInfo {
entryPointsRouters := make(map[string]map[string]*RouterInfo)
for rtName, rt := range r.Routers {
@ -169,8 +197,8 @@ func (r *RuntimeConfiguration) GetRoutersByEntrypoints(ctx context.Context, entr
return entryPointsRouters
}
// GetTCPRoutersByEntrypoints returns all the tcp routers by entrypoints name and routers name
func (r *RuntimeConfiguration) GetTCPRoutersByEntrypoints(ctx context.Context, entryPoints []string) map[string]map[string]*TCPRouterInfo {
// GetTCPRoutersByEntryPoints returns all the tcp routers by entry points name and routers name
func (r *Configuration) GetTCPRoutersByEntryPoints(ctx context.Context, entryPoints []string) map[string]map[string]*TCPRouterInfo {
entryPointsRouters := make(map[string]map[string]*TCPRouterInfo)
for rtName, rt := range r.TCPRouters {
@ -199,57 +227,126 @@ func (r *RuntimeConfiguration) GetTCPRoutersByEntrypoints(ctx context.Context, e
// RouterInfo holds information about a currently running HTTP router
type RouterInfo struct {
*Router // dynamic configuration
Err string `json:"error,omitempty"` // initialization error
*dynamic.Router // dynamic configuration
// Err contains all the errors that occurred during router's creation.
Err []string `json:"error,omitempty"`
// Status reports whether the router is disabled, in a warning state, or all good (enabled).
// If not in "enabled" state, the reason for it should be in the list of Err.
// It is the caller's responsibility to set the initial status.
Status string `json:"status,omitempty"`
}
// AddError adds err to r.Err, if it does not already exist.
// If critical is set, r is marked as disabled.
func (r *RouterInfo) AddError(err error, critical bool) {
for _, value := range r.Err {
if value == err.Error() {
return
}
}
r.Err = append(r.Err, err.Error())
if critical {
r.Status = StatusDisabled
return
}
// only set it to "warning" if not already in a worse state
if r.Status != StatusDisabled {
r.Status = StatusWarning
}
}
// TCPRouterInfo holds information about a currently running TCP router
type TCPRouterInfo struct {
*TCPRouter // dynamic configuration
Err string `json:"error,omitempty"` // initialization error
*dynamic.TCPRouter // dynamic configuration
Err string `json:"error,omitempty"` // initialization error
// Status reports whether the router is disabled, in a warning state, or all good (enabled).
// If not in "enabled" state, the reason for it should be in the list of Err.
// It is the caller's responsibility to set the initial status.
Status string `json:"status,omitempty"`
}
// MiddlewareInfo holds information about a currently running middleware
type MiddlewareInfo struct {
*Middleware // dynamic configuration
Err error `json:"error,omitempty"` // initialization error
UsedBy []string `json:"usedBy,omitempty"` // list of routers and services using that middleware
*dynamic.Middleware // dynamic configuration
// Err contains all the errors that occurred during service creation.
Err []string `json:"error,omitempty"`
UsedBy []string `json:"usedBy,omitempty"` // list of routers and services using that middleware
}
// AddError adds err to s.Err, if it does not already exist.
// If critical is set, m is marked as disabled.
func (m *MiddlewareInfo) AddError(err error) {
for _, value := range m.Err {
if value == err.Error() {
return
}
}
m.Err = append(m.Err, err.Error())
}
// ServiceInfo holds information about a currently running service
type ServiceInfo struct {
*Service // dynamic configuration
Err error `json:"error,omitempty"` // initialization error
UsedBy []string `json:"usedBy,omitempty"` // list of routers using that service
*dynamic.Service // dynamic configuration
// Err contains all the errors that occurred during service creation.
Err []string `json:"error,omitempty"`
// Status reports whether the service is disabled, in a warning state, or all good (enabled).
// If not in "enabled" state, the reason for it should be in the list of Err.
// It is the caller's responsibility to set the initial status.
Status string `json:"status,omitempty"`
UsedBy []string `json:"usedBy,omitempty"` // list of routers using that service
statusMu sync.RWMutex
status map[string]string // keyed by server URL
serverStatusMu sync.RWMutex
serverStatus map[string]string // keyed by server URL
}
// UpdateStatus sets the status of the server in the ServiceInfo.
// It is the responsibility of the caller to check that s is not nil.
func (s *ServiceInfo) UpdateStatus(server string, status string) {
s.statusMu.Lock()
defer s.statusMu.Unlock()
if s.status == nil {
s.status = make(map[string]string)
// AddError adds err to s.Err, if it does not already exist.
// If critical is set, s is marked as disabled.
func (s *ServiceInfo) AddError(err error, critical bool) {
for _, value := range s.Err {
if value == err.Error() {
return
}
}
s.status[server] = status
s.Err = append(s.Err, err.Error())
if critical {
s.Status = StatusDisabled
return
}
// only set it to "warning" if not already in a worse state
if s.Status != StatusDisabled {
s.Status = StatusWarning
}
}
// UpdateServerStatus sets the status of the server in the ServiceInfo.
// It is the responsibility of the caller to check that s is not nil.
func (s *ServiceInfo) UpdateServerStatus(server string, status string) {
s.serverStatusMu.Lock()
defer s.serverStatusMu.Unlock()
if s.serverStatus == nil {
s.serverStatus = make(map[string]string)
}
s.serverStatus[server] = status
}
// GetAllStatus returns all the statuses of all the servers in ServiceInfo.
// It is the responsibility of the caller to check that s is not nil
func (s *ServiceInfo) GetAllStatus() map[string]string {
s.statusMu.RLock()
defer s.statusMu.RUnlock()
s.serverStatusMu.RLock()
defer s.serverStatusMu.RUnlock()
if len(s.status) == 0 {
if len(s.serverStatus) == 0 {
return nil
}
allStatus := make(map[string]string, len(s.status))
for k, v := range s.status {
allStatus := make(map[string]string, len(s.serverStatus))
for k, v := range s.serverStatus {
allStatus[k] = v
}
return allStatus
@ -257,9 +354,13 @@ func (s *ServiceInfo) GetAllStatus() map[string]string {
// TCPServiceInfo holds information about a currently running TCP service
type TCPServiceInfo struct {
*TCPService // dynamic configuration
Err error `json:"error,omitempty"` // initialization error
UsedBy []string `json:"usedBy,omitempty"` // list of routers using that service
*dynamic.TCPService // dynamic configuration
Err error `json:"error,omitempty"` // initialization error
// Status reports whether the service is disabled, in a warning state, or all good (enabled).
// If not in "enabled" state, the reason for it should be in the list of Err.
// It is the caller's responsibility to set the initial status.
Status string `json:"status,omitempty"`
UsedBy []string `json:"usedBy,omitempty"` // list of routers using that service
}
func getProviderName(elementName string) string {

View file

@ -1,30 +1,31 @@
package dynamic_test
package runtime_test
import (
"context"
"testing"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/config/runtime"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// all the Routers/Middlewares/Services are considered fully qualified
func TestPopulateUsedby(t *testing.T) {
func TestPopulateUsedBy(t *testing.T) {
testCases := []struct {
desc string
conf *dynamic.RuntimeConfiguration
expected dynamic.RuntimeConfiguration
conf *runtime.Configuration
expected runtime.Configuration
}{
{
desc: "nil config",
conf: nil,
expected: dynamic.RuntimeConfiguration{},
expected: runtime.Configuration{},
},
{
desc: "One service used by two routers",
conf: &dynamic.RuntimeConfiguration{
Routers: map[string]*dynamic.RouterInfo{
conf: &runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"foo@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
@ -40,7 +41,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Services: map[string]*dynamic.ServiceInfo{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{
@ -57,12 +58,12 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
Routers: map[string]*dynamic.RouterInfo{
expected: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"foo@myprovider": {},
"bar@myprovider": {},
},
Services: map[string]*dynamic.ServiceInfo{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider", "foo@myprovider"},
},
@ -71,8 +72,8 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "One service used by two routers, but one router with wrong rule",
conf: &dynamic.RuntimeConfiguration{
Services: map[string]*dynamic.ServiceInfo{
conf: &runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{
@ -83,7 +84,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Routers: map[string]*dynamic.RouterInfo{
Routers: map[string]*runtime.RouterInfo{
"foo@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
@ -100,12 +101,12 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
Routers: map[string]*dynamic.RouterInfo{
expected: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"foo@myprovider": {},
"bar@myprovider": {},
},
Services: map[string]*dynamic.ServiceInfo{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider", "foo@myprovider"},
},
@ -114,15 +115,15 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "Broken Service used by one Router",
conf: &dynamic.RuntimeConfiguration{
Services: map[string]*dynamic.ServiceInfo{
conf: &runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: nil,
},
},
},
Routers: map[string]*dynamic.RouterInfo{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
@ -132,11 +133,11 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
Routers: map[string]*dynamic.RouterInfo{
expected: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {},
},
Services: map[string]*dynamic.ServiceInfo{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider"},
},
@ -145,8 +146,8 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "2 different Services each used by a disctinct router.",
conf: &dynamic.RuntimeConfiguration{
Services: map[string]*dynamic.ServiceInfo{
conf: &runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{
@ -184,7 +185,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Routers: map[string]*dynamic.RouterInfo{
Routers: map[string]*runtime.RouterInfo{
"foo@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
@ -201,12 +202,12 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
Routers: map[string]*dynamic.RouterInfo{
expected: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {},
"foo@myprovider": {},
},
Services: map[string]*dynamic.ServiceInfo{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"foo@myprovider"},
},
@ -218,8 +219,8 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "2 middlewares both used by 2 Routers",
conf: &dynamic.RuntimeConfiguration{
Services: map[string]*dynamic.ServiceInfo{
conf: &runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{
@ -232,7 +233,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Middlewares: map[string]*dynamic.MiddlewareInfo{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
@ -248,7 +249,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Routers: map[string]*dynamic.RouterInfo{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
@ -267,17 +268,17 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
Routers: map[string]*dynamic.RouterInfo{
expected: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {},
"test@myprovider": {},
},
Services: map[string]*dynamic.ServiceInfo{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider", "test@myprovider"},
},
},
Middlewares: map[string]*dynamic.MiddlewareInfo{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
UsedBy: []string{"bar@myprovider", "test@myprovider"},
},
@ -289,8 +290,8 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "Unknown middleware is not used by the Router",
conf: &dynamic.RuntimeConfiguration{
Services: map[string]*dynamic.ServiceInfo{
conf: &runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{
@ -303,7 +304,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Middlewares: map[string]*dynamic.MiddlewareInfo{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
@ -312,7 +313,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Routers: map[string]*dynamic.RouterInfo{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
@ -323,8 +324,8 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
Services: map[string]*dynamic.ServiceInfo{
expected: runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider"},
},
@ -333,8 +334,8 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "Broken middleware is used by Router",
conf: &dynamic.RuntimeConfiguration{
Services: map[string]*dynamic.ServiceInfo{
conf: &runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{
@ -347,7 +348,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Middlewares: map[string]*dynamic.MiddlewareInfo{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
@ -356,7 +357,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Routers: map[string]*dynamic.RouterInfo{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
@ -367,16 +368,16 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
Routers: map[string]*dynamic.RouterInfo{
expected: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {},
},
Services: map[string]*dynamic.ServiceInfo{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider"},
},
},
Middlewares: map[string]*dynamic.MiddlewareInfo{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
UsedBy: []string{"bar@myprovider"},
},
@ -385,8 +386,8 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "2 middlewares from 2 disctinct providers both used by 2 Routers",
conf: &dynamic.RuntimeConfiguration{
Services: map[string]*dynamic.ServiceInfo{
conf: &runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{
@ -399,7 +400,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Middlewares: map[string]*dynamic.MiddlewareInfo{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
@ -422,7 +423,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
Routers: map[string]*dynamic.RouterInfo{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
@ -441,17 +442,17 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
Routers: map[string]*dynamic.RouterInfo{
expected: runtime.Configuration{
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {},
"test@myprovider": {},
},
Services: map[string]*dynamic.ServiceInfo{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider", "test@myprovider"},
},
},
Middlewares: map[string]*dynamic.MiddlewareInfo{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
UsedBy: []string{"bar@myprovider", "test@myprovider"},
},
@ -468,8 +469,8 @@ func TestPopulateUsedby(t *testing.T) {
// TCP tests from hereon
{
desc: "TCP, One service used by two routers",
conf: &dynamic.RuntimeConfiguration{
TCPRouters: map[string]*dynamic.TCPRouterInfo{
conf: &runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"foo@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
@ -485,7 +486,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
TCPServices: map[string]*dynamic.TCPServiceInfo{
TCPServices: map[string]*runtime.TCPServiceInfo{
"foo-service@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -504,12 +505,12 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
TCPRouters: map[string]*dynamic.TCPRouterInfo{
expected: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"foo@myprovider": {},
"bar@myprovider": {},
},
TCPServices: map[string]*dynamic.TCPServiceInfo{
TCPServices: map[string]*runtime.TCPServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider", "foo@myprovider"},
},
@ -518,8 +519,8 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "TCP, One service used by two routers, but one router with wrong rule",
conf: &dynamic.RuntimeConfiguration{
TCPServices: map[string]*dynamic.TCPServiceInfo{
conf: &runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
"foo-service@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -532,7 +533,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
TCPRouters: map[string]*dynamic.TCPRouterInfo{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"foo@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
@ -549,12 +550,12 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
TCPRouters: map[string]*dynamic.TCPRouterInfo{
expected: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"foo@myprovider": {},
"bar@myprovider": {},
},
TCPServices: map[string]*dynamic.TCPServiceInfo{
TCPServices: map[string]*runtime.TCPServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider", "foo@myprovider"},
},
@ -563,15 +564,15 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "TCP, Broken Service used by one Router",
conf: &dynamic.RuntimeConfiguration{
TCPServices: map[string]*dynamic.TCPServiceInfo{
conf: &runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
"foo-service@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: nil,
},
},
},
TCPRouters: map[string]*dynamic.TCPRouterInfo{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"bar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
@ -581,11 +582,11 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
TCPRouters: map[string]*dynamic.TCPRouterInfo{
expected: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"bar@myprovider": {},
},
TCPServices: map[string]*dynamic.TCPServiceInfo{
TCPServices: map[string]*runtime.TCPServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"bar@myprovider"},
},
@ -594,8 +595,8 @@ func TestPopulateUsedby(t *testing.T) {
},
{
desc: "TCP, 2 different Services each used by a disctinct router.",
conf: &dynamic.RuntimeConfiguration{
TCPServices: map[string]*dynamic.TCPServiceInfo{
conf: &runtime.Configuration{
TCPServices: map[string]*runtime.TCPServiceInfo{
"foo-service@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -629,7 +630,7 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
TCPRouters: map[string]*dynamic.TCPRouterInfo{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"foo@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
@ -646,12 +647,12 @@ func TestPopulateUsedby(t *testing.T) {
},
},
},
expected: dynamic.RuntimeConfiguration{
TCPRouters: map[string]*dynamic.TCPRouterInfo{
expected: runtime.Configuration{
TCPRouters: map[string]*runtime.TCPRouterInfo{
"bar@myprovider": {},
"foo@myprovider": {},
},
TCPServices: map[string]*dynamic.TCPServiceInfo{
TCPServices: map[string]*runtime.TCPServiceInfo{
"foo-service@myprovider": {
UsedBy: []string{"foo@myprovider"},
},
@ -690,24 +691,24 @@ func TestPopulateUsedby(t *testing.T) {
}
func TestGetTCPRoutersByEntrypoints(t *testing.T) {
func TestGetTCPRoutersByEntryPoints(t *testing.T) {
testCases := []struct {
desc string
conf dynamic.Configuration
entryPoints []string
expected map[string]map[string]*dynamic.TCPRouterInfo
expected map[string]map[string]*runtime.TCPRouterInfo
}{
{
desc: "Empty Configuration without entrypoint",
conf: dynamic.Configuration{},
entryPoints: []string{""},
expected: map[string]map[string]*dynamic.TCPRouterInfo{},
expected: map[string]map[string]*runtime.TCPRouterInfo{},
},
{
desc: "Empty Configuration with unknown entrypoints",
conf: dynamic.Configuration{},
entryPoints: []string{"foo"},
expected: map[string]map[string]*dynamic.TCPRouterInfo{},
expected: map[string]map[string]*runtime.TCPRouterInfo{},
},
{
desc: "Valid configuration with an unknown entrypoint",
@ -732,7 +733,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
},
},
entryPoints: []string{"foo"},
expected: map[string]map[string]*dynamic.TCPRouterInfo{},
expected: map[string]map[string]*runtime.TCPRouterInfo{},
},
{
desc: "Valid configuration with a known entrypoint",
@ -777,7 +778,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
},
},
entryPoints: []string{"web"},
expected: map[string]map[string]*dynamic.TCPRouterInfo{
expected: map[string]map[string]*runtime.TCPRouterInfo{
"web": {
"foo": {
TCPRouter: &dynamic.TCPRouter{
@ -785,6 +786,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
Service: "foo-service@myprovider",
Rule: "HostSNI(`bar.foo`)",
},
Status: "enabled",
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
@ -792,6 +794,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
Status: "enabled",
},
},
},
@ -839,7 +842,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
},
},
entryPoints: []string{"web", "webs"},
expected: map[string]map[string]*dynamic.TCPRouterInfo{
expected: map[string]map[string]*runtime.TCPRouterInfo{
"web": {
"foo": {
TCPRouter: &dynamic.TCPRouter{
@ -847,6 +850,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
Service: "foo-service@myprovider",
Rule: "HostSNI(`bar.foo`)",
},
Status: "enabled",
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
@ -854,6 +858,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
Status: "enabled",
},
},
"webs": {
@ -864,6 +869,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
Service: "bar-service@myprovider",
Rule: "HostSNI(`foo.bar`)",
},
Status: "enabled",
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
@ -871,6 +877,7 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
Status: "enabled",
},
},
},
@ -881,31 +888,31 @@ func TestGetTCPRoutersByEntrypoints(t *testing.T) {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
runtimeConfig := dynamic.NewRuntimeConfig(test.conf)
actual := runtimeConfig.GetTCPRoutersByEntrypoints(context.Background(), test.entryPoints)
runtimeConfig := runtime.NewConfig(test.conf)
actual := runtimeConfig.GetTCPRoutersByEntryPoints(context.Background(), test.entryPoints)
assert.Equal(t, test.expected, actual)
})
}
}
func TestGetRoutersByEntrypoints(t *testing.T) {
func TestGetRoutersByEntryPoints(t *testing.T) {
testCases := []struct {
desc string
conf dynamic.Configuration
entryPoints []string
expected map[string]map[string]*dynamic.RouterInfo
expected map[string]map[string]*runtime.RouterInfo
}{
{
desc: "Empty Configuration without entrypoint",
conf: dynamic.Configuration{},
entryPoints: []string{""},
expected: map[string]map[string]*dynamic.RouterInfo{},
expected: map[string]map[string]*runtime.RouterInfo{},
},
{
desc: "Empty Configuration with unknown entrypoints",
conf: dynamic.Configuration{},
entryPoints: []string{"foo"},
expected: map[string]map[string]*dynamic.RouterInfo{},
expected: map[string]map[string]*runtime.RouterInfo{},
},
{
desc: "Valid configuration with an unknown entrypoint",
@ -930,7 +937,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
},
},
entryPoints: []string{"foo"},
expected: map[string]map[string]*dynamic.RouterInfo{},
expected: map[string]map[string]*runtime.RouterInfo{},
},
{
desc: "Valid configuration with a known entrypoint",
@ -975,7 +982,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
},
},
entryPoints: []string{"web"},
expected: map[string]map[string]*dynamic.RouterInfo{
expected: map[string]map[string]*runtime.RouterInfo{
"web": {
"foo": {
Router: &dynamic.Router{
@ -983,6 +990,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
Service: "foo-service@myprovider",
Rule: "Host(`bar.foo`)",
},
Status: "enabled",
},
"foobar": {
Router: &dynamic.Router{
@ -990,6 +998,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
Service: "foobar-service@myprovider",
Rule: "Host(`bar.foobar`)",
},
Status: "enabled",
},
},
},
@ -1037,7 +1046,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
},
},
entryPoints: []string{"web", "webs"},
expected: map[string]map[string]*dynamic.RouterInfo{
expected: map[string]map[string]*runtime.RouterInfo{
"web": {
"foo": {
Router: &dynamic.Router{
@ -1045,6 +1054,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
Service: "foo-service@myprovider",
Rule: "Host(`bar.foo`)",
},
Status: "enabled",
},
"foobar": {
Router: &dynamic.Router{
@ -1052,6 +1062,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
Service: "foobar-service@myprovider",
Rule: "Host(`bar.foobar`)",
},
Status: "enabled",
},
},
"webs": {
@ -1062,6 +1073,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
Service: "bar-service@myprovider",
Rule: "Host(`foo.bar`)",
},
Status: "enabled",
},
"foobar": {
Router: &dynamic.Router{
@ -1069,6 +1081,7 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
Service: "foobar-service@myprovider",
Rule: "Host(`bar.foobar`)",
},
Status: "enabled",
},
},
},
@ -1079,8 +1092,8 @@ func TestGetRoutersByEntrypoints(t *testing.T) {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
runtimeConfig := dynamic.NewRuntimeConfig(test.conf)
actual := runtimeConfig.GetRoutersByEntrypoints(context.Background(), test.entryPoints, false)
runtimeConfig := runtime.NewConfig(test.conf)
actual := runtimeConfig.GetRoutersByEntryPoints(context.Background(), test.entryPoints, false)
assert.Equal(t, test.expected, actual)
})
}