1
0
Fork 0

Improve API for the web UI

This commit is contained in:
Ludovic Fernandez 2019-09-02 11:38:04 +02:00 committed by Traefiker Bot
parent 17554202f6
commit af9762cf32
41 changed files with 1200 additions and 199 deletions

View file

@ -3,13 +3,14 @@ package runtime
import (
"context"
"fmt"
"sort"
"sync"
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/log"
)
// GetRoutersByEntryPoints returns all the http routers by entry points name and routers name
// GetRoutersByEntryPoints returns all the http routers by entry points name and routers name.
func (c *Configuration) GetRoutersByEntryPoints(ctx context.Context, entryPoints []string, tls bool) map[string]map[string]*RouterInfo {
entryPointsRouters := make(map[string]map[string]*RouterInfo)
@ -19,11 +20,13 @@ func (c *Configuration) GetRoutersByEntryPoints(ctx context.Context, entryPoints
}
logger := log.FromContext(log.With(ctx, log.Str(log.RouterName, rtName)))
eps := rt.EntryPoints
if len(eps) == 0 {
logger.Debugf("No entrypoint defined for this router, using the default one(s) instead: %+v", entryPoints)
logger.Debugf("No entryPoint defined for this router, using the default one(s) instead: %+v", entryPoints)
eps = entryPoints
}
entryPointsCount := 0
for _, entryPointName := range eps {
if !contains(entryPoints, entryPointName) {
@ -33,23 +36,44 @@ func (c *Configuration) GetRoutersByEntryPoints(ctx context.Context, entryPoints
continue
}
entryPointsCount++
if _, ok := entryPointsRouters[entryPointName]; !ok {
entryPointsRouters[entryPointName] = make(map[string]*RouterInfo)
}
entryPointsCount++
rt.Using = append(rt.Using, entryPointName)
entryPointsRouters[entryPointName][rtName] = rt
}
if entryPointsCount == 0 {
rt.AddError(fmt.Errorf("no valid entryPoint for this router"), true)
logger.Error("no valid entryPoint for this router")
}
rt.Using = unique(rt.Using)
}
return entryPointsRouters
}
// RouterInfo holds information about a currently running HTTP router
func unique(src []string) []string {
var uniq []string
set := make(map[string]struct{})
for _, v := range src {
if _, exist := set[v]; !exist {
set[v] = struct{}{}
uniq = append(uniq, v)
}
}
sort.Strings(uniq)
return uniq
}
// RouterInfo holds information about a currently running HTTP router.
type RouterInfo struct {
*dynamic.Router // dynamic configuration
// Err contains all the errors that occurred during router's creation.
@ -57,7 +81,8 @@ type RouterInfo struct {
// 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"`
Status string `json:"status,omitempty"`
Using []string `json:"using,omitempty"` // Effective entry points used by that router.
}
// AddError adds err to r.Err, if it does not already exist.
@ -81,13 +106,13 @@ func (r *RouterInfo) AddError(err error, critical bool) {
}
}
// MiddlewareInfo holds information about a currently running middleware
// 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
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.
@ -111,7 +136,7 @@ func (m *MiddlewareInfo) AddError(err error, critical bool) {
}
}
// ServiceInfo holds information about a currently running service
// ServiceInfo holds information about a currently running service.
type ServiceInfo struct {
*dynamic.Service // dynamic configuration
// Err contains all the errors that occurred during service creation.

View file

@ -104,6 +104,7 @@ func TestGetRoutersByEntryPoints(t *testing.T) {
Rule: "Host(`bar.foo`)",
},
Status: "enabled",
Using: []string{"web"},
},
"foobar": {
Router: &dynamic.Router{
@ -113,6 +114,7 @@ func TestGetRoutersByEntryPoints(t *testing.T) {
},
Status: "warning",
Err: []string{`entryPoint "webs" doesn't exist`},
Using: []string{"web"},
},
},
},
@ -169,6 +171,7 @@ func TestGetRoutersByEntryPoints(t *testing.T) {
Rule: "Host(`bar.foo`)",
},
Status: "enabled",
Using: []string{"web"},
},
"foobar": {
Router: &dynamic.Router{
@ -177,6 +180,7 @@ func TestGetRoutersByEntryPoints(t *testing.T) {
Rule: "Host(`bar.foobar`)",
},
Status: "enabled",
Using: []string{"web", "webs"},
},
},
"webs": {
@ -188,6 +192,7 @@ func TestGetRoutersByEntryPoints(t *testing.T) {
Rule: "Host(`foo.bar`)",
},
Status: "enabled",
Using: []string{"webs"},
},
"foobar": {
Router: &dynamic.Router{
@ -196,6 +201,7 @@ func TestGetRoutersByEntryPoints(t *testing.T) {
Rule: "Host(`bar.foobar`)",
},
Status: "enabled",
Using: []string{"web", "webs"},
},
},
},

View file

@ -2,24 +2,30 @@ package runtime
import (
"context"
"fmt"
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/log"
)
// GetTCPRoutersByEntryPoints returns all the tcp routers by entry points name and routers name
// GetTCPRoutersByEntryPoints returns all the tcp routers by entry points name and routers name.
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 c.TCPRouters {
logger := log.FromContext(log.With(ctx, log.Str(log.RouterName, rtName)))
eps := rt.EntryPoints
if len(eps) == 0 {
logger.Debugf("No entryPoint defined for this router, using the default one(s) instead: %+v", entryPoints)
eps = entryPoints
}
entryPointsCount := 0
for _, entryPointName := range eps {
if !contains(entryPoints, entryPointName) {
log.FromContext(log.With(ctx, log.Str(log.EntryPointName, entryPointName))).
rt.AddError(fmt.Errorf("entryPoint %q doesn't exist", entryPointName), false)
logger.WithField(log.EntryPointName, entryPointName).
Errorf("entryPoint %q doesn't exist", entryPointName)
continue
}
@ -28,21 +34,30 @@ func (c *Configuration) GetTCPRoutersByEntryPoints(ctx context.Context, entryPoi
entryPointsRouters[entryPointName] = make(map[string]*TCPRouterInfo)
}
entryPointsCount++
rt.Using = append(rt.Using, entryPointName)
entryPointsRouters[entryPointName][rtName] = rt
}
if entryPointsCount == 0 {
rt.AddError(fmt.Errorf("no valid entryPoint for this router"), true)
logger.Error("no valid entryPoint for this router")
}
}
return entryPointsRouters
}
// TCPRouterInfo holds information about a currently running TCP router
// TCPRouterInfo holds information about a currently running TCP router.
type TCPRouterInfo struct {
*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"`
Status string `json:"status,omitempty"`
Using []string `json:"using,omitempty"` // Effective entry points used by that router.
}
// AddError adds err to r.Err, if it does not already exist.
@ -66,7 +81,7 @@ func (r *TCPRouterInfo) AddError(err error, critical bool) {
}
}
// TCPServiceInfo holds information about a currently running TCP service
// TCPServiceInfo holds information about a currently running TCP service.
type TCPServiceInfo struct {
*dynamic.TCPService // dynamic configuration
Err []string `json:"error,omitempty"` // initialization error

View file

@ -104,6 +104,7 @@ func TestGetTCPRoutersByEntryPoints(t *testing.T) {
Rule: "HostSNI(`bar.foo`)",
},
Status: "enabled",
Using: []string{"web"},
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
@ -111,7 +112,9 @@ func TestGetTCPRoutersByEntryPoints(t *testing.T) {
Service: "foobar-service@myprovider",
Rule: "HostSNI(`bar.foobar`)",
},
Status: "enabled",
Status: "warning",
Err: []string{`entryPoint "webs" doesn't exist`},
Using: []string{"web"},
},
},
},
@ -168,6 +171,7 @@ func TestGetTCPRoutersByEntryPoints(t *testing.T) {
Rule: "HostSNI(`bar.foo`)",
},
Status: "enabled",
Using: []string{"web"},
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
@ -176,6 +180,7 @@ func TestGetTCPRoutersByEntryPoints(t *testing.T) {
Rule: "HostSNI(`bar.foobar`)",
},
Status: "enabled",
Using: []string{"web", "webs"},
},
},
"webs": {
@ -187,6 +192,7 @@ func TestGetTCPRoutersByEntryPoints(t *testing.T) {
Rule: "HostSNI(`foo.bar`)",
},
Status: "enabled",
Using: []string{"webs"},
},
"foobar": {
TCPRouter: &dynamic.TCPRouter{
@ -195,6 +201,7 @@ func TestGetTCPRoutersByEntryPoints(t *testing.T) {
Rule: "HostSNI(`bar.foobar`)",
},
Status: "enabled",
Using: []string{"web", "webs"},
},
},
},