traefik/pkg/config/runtime/runtime_tcp.go

115 lines
3.6 KiB
Go
Raw Normal View History

2019-08-14 15:28:04 +00:00
package runtime
import (
"context"
2019-09-02 09:38:04 +00:00
"fmt"
2019-08-14 15:28:04 +00:00
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/log"
)
2019-09-02 09:38:04 +00:00
// GetTCPRoutersByEntryPoints returns all the tcp routers by entry points name and routers name.
2019-08-14 15:28:04 +00:00
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 {
2019-09-02 09:38:04 +00:00
logger := log.FromContext(log.With(ctx, log.Str(log.RouterName, rtName)))
2019-08-14 15:28:04 +00:00
eps := rt.EntryPoints
if len(eps) == 0 {
2019-09-02 09:38:04 +00:00
logger.Debugf("No entryPoint defined for this router, using the default one(s) instead: %+v", entryPoints)
2019-08-14 15:28:04 +00:00
eps = entryPoints
}
2019-09-02 09:38:04 +00:00
entryPointsCount := 0
2019-08-14 15:28:04 +00:00
for _, entryPointName := range eps {
if !contains(entryPoints, entryPointName) {
2019-09-02 09:38:04 +00:00
rt.AddError(fmt.Errorf("entryPoint %q doesn't exist", entryPointName), false)
logger.WithField(log.EntryPointName, entryPointName).
2019-08-14 15:28:04 +00:00
Errorf("entryPoint %q doesn't exist", entryPointName)
continue
}
if _, ok := entryPointsRouters[entryPointName]; !ok {
entryPointsRouters[entryPointName] = make(map[string]*TCPRouterInfo)
}
2019-09-02 09:38:04 +00:00
entryPointsCount++
rt.Using = append(rt.Using, entryPointName)
2019-08-14 15:28:04 +00:00
entryPointsRouters[entryPointName][rtName] = rt
}
2019-09-02 09:38:04 +00:00
if entryPointsCount == 0 {
rt.AddError(fmt.Errorf("no valid entryPoint for this router"), true)
logger.Error("no valid entryPoint for this router")
}
2019-08-14 15:28:04 +00:00
}
return entryPointsRouters
}
2019-09-02 09:38:04 +00:00
// TCPRouterInfo holds information about a currently running TCP router.
2019-08-14 15:28:04 +00:00
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.
2019-09-02 09:38:04 +00:00
Status string `json:"status,omitempty"`
Using []string `json:"using,omitempty"` // Effective entry points used by that router.
2019-08-14 15:28:04 +00:00
}
// 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
}
}
2019-09-02 09:38:04 +00:00
// TCPServiceInfo holds information about a currently running TCP service.
2019-08-14 15:28:04 +00:00
type TCPServiceInfo struct {
*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.
Status string `json:"status,omitempty"`
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
}
}