Improve error on router without service.

Co-authored-by: Mathieu Lonjaret <mathieu.lonjaret@gmail.com>
This commit is contained in:
Ludovic Fernandez 2019-07-19 16:42:04 +02:00 committed by Traefiker Bot
parent c39a550b00
commit 1800b0b69c
13 changed files with 125 additions and 40 deletions

View file

@ -55,7 +55,7 @@ func NewConfig(conf dynamic.Configuration) *Configuration {
if len(middlewares) > 0 {
runtimeConfig.Middlewares = make(map[string]*MiddlewareInfo, len(middlewares))
for k, v := range middlewares {
runtimeConfig.Middlewares[k] = &MiddlewareInfo{Middleware: v}
runtimeConfig.Middlewares[k] = &MiddlewareInfo{Middleware: v, Status: StatusEnabled}
}
}
}
@ -81,14 +81,14 @@ func NewConfig(conf dynamic.Configuration) *Configuration {
// 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 *Configuration) PopulateUsedBy() {
if r == nil {
func (c *Configuration) PopulateUsedBy() {
if c == nil {
return
}
logger := log.WithoutContext()
for routerName, routerInfo := range r.Routers {
for routerName, routerInfo := range c.Routers {
// lazily initialize Status in case caller forgot to do it
if routerInfo.Status == "" {
routerInfo.Status = StatusEnabled
@ -102,33 +102,38 @@ func (r *Configuration) PopulateUsedBy() {
for _, midName := range routerInfo.Router.Middlewares {
fullMidName := getQualifiedName(providerName, midName)
if _, ok := r.Middlewares[fullMidName]; !ok {
if _, ok := c.Middlewares[fullMidName]; !ok {
continue
}
r.Middlewares[fullMidName].UsedBy = append(r.Middlewares[fullMidName].UsedBy, routerName)
c.Middlewares[fullMidName].UsedBy = append(c.Middlewares[fullMidName].UsedBy, routerName)
}
serviceName := getQualifiedName(providerName, routerInfo.Router.Service)
if _, ok := r.Services[serviceName]; !ok {
if _, ok := c.Services[serviceName]; !ok {
continue
}
r.Services[serviceName].UsedBy = append(r.Services[serviceName].UsedBy, routerName)
c.Services[serviceName].UsedBy = append(c.Services[serviceName].UsedBy, routerName)
}
for k, serviceInfo := range r.Services {
for k, serviceInfo := range c.Services {
// lazily initialize Status in case caller forgot to do it
if serviceInfo.Status == "" {
serviceInfo.Status = StatusEnabled
}
sort.Strings(r.Services[k].UsedBy)
sort.Strings(c.Services[k].UsedBy)
}
for k := range r.Middlewares {
sort.Strings(r.Middlewares[k].UsedBy)
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)
}
for routerName, routerInfo := range r.TCPRouters {
for routerName, routerInfo := range c.TCPRouters {
// lazily initialize Status in case caller forgot to do it
if routerInfo.Status == "" {
routerInfo.Status = StatusEnabled
@ -141,19 +146,19 @@ func (r *Configuration) PopulateUsedBy() {
}
serviceName := getQualifiedName(providerName, routerInfo.TCPRouter.Service)
if _, ok := r.TCPServices[serviceName]; !ok {
if _, ok := c.TCPServices[serviceName]; !ok {
continue
}
r.TCPServices[serviceName].UsedBy = append(r.TCPServices[serviceName].UsedBy, routerName)
c.TCPServices[serviceName].UsedBy = append(c.TCPServices[serviceName].UsedBy, routerName)
}
for k, serviceInfo := range r.TCPServices {
for k, serviceInfo := range c.TCPServices {
// lazily initialize Status in case caller forgot to do it
if serviceInfo.Status == "" {
serviceInfo.Status = StatusEnabled
}
sort.Strings(r.TCPServices[k].UsedBy)
sort.Strings(c.TCPServices[k].UsedBy)
}
}
@ -167,10 +172,10 @@ func contains(entryPoints []string, entryPointName string) bool {
}
// 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 {
func (c *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 {
for rtName, rt := range c.Routers {
if (tls && rt.TLS == nil) || (!tls && rt.TLS != nil) {
continue
}
@ -198,10 +203,10 @@ func (r *Configuration) GetRoutersByEntryPoints(ctx context.Context, entryPoints
}
// 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 {
func (c *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 {
for rtName, rt := range c.TCPRouters {
eps := rt.EntryPoints
if len(eps) == 0 {
eps = entryPoints
@ -259,25 +264,47 @@ func (r *RouterInfo) AddError(err error, critical bool) {
// TCPRouterInfo holds information about a currently running TCP router
type TCPRouterInfo struct {
*dynamic.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"`
}
// AddError adds err to r.Err, if it does not already exist.
// If critical is set, r is marked as disabled.
func (r *TCPRouterInfo) 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
}
}
// MiddlewareInfo holds information about a currently running middleware
type MiddlewareInfo struct {
*dynamic.Middleware // dynamic configuration
// Err contains all the errors that occurred during service creation.
Err []string `json:"error,omitempty"`
Status string `json:"status,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) {
func (m *MiddlewareInfo) AddError(err error, critical bool) {
for _, value := range m.Err {
if value == err.Error() {
return
@ -285,6 +312,15 @@ func (m *MiddlewareInfo) AddError(err error) {
}
m.Err = append(m.Err, err.Error())
if critical {
m.Status = StatusDisabled
return
}
// only set it to "warning" if not already in a worse state
if m.Status != StatusDisabled {
m.Status = StatusWarning
}
}
// ServiceInfo holds information about a currently running service
@ -354,8 +390,8 @@ func (s *ServiceInfo) GetAllStatus() map[string]string {
// TCPServiceInfo holds information about a currently running TCP service
type TCPServiceInfo struct {
*dynamic.TCPService // dynamic configuration
Err error `json:"error,omitempty"` // initialization error
*dynamic.TCPService // dynamic configuration
Err []string `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.
@ -363,6 +399,27 @@ type TCPServiceInfo struct {
UsedBy []string `json:"usedBy,omitempty"` // list of routers using that service
}
// AddError adds err to s.Err, if it does not already exist.
// If critical is set, s is marked as disabled.
func (s *TCPServiceInfo) AddError(err error, critical bool) {
for _, value := range s.Err {
if value == err.Error() {
return
}
}
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
}
}
func getProviderName(elementName string) string {
parts := strings.Split(elementName, "@")
if len(parts) > 1 {