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

@ -10,6 +10,7 @@ import (
"github.com/containous/alice"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/config/runtime"
"github.com/containous/traefik/pkg/healthcheck"
"github.com/containous/traefik/pkg/log"
"github.com/containous/traefik/pkg/middlewares/accesslog"
@ -26,7 +27,7 @@ const (
)
// NewManager creates a new Manager
func NewManager(configs map[string]*dynamic.ServiceInfo, defaultRoundTripper http.RoundTripper) *Manager {
func NewManager(configs map[string]*runtime.ServiceInfo, defaultRoundTripper http.RoundTripper) *Manager {
return &Manager{
bufferPool: newBufferPool(),
defaultRoundTripper: defaultRoundTripper,
@ -40,7 +41,7 @@ type Manager struct {
bufferPool httputil.BufferPool
defaultRoundTripper http.RoundTripper
balancers map[string][]healthcheck.BalancerHandler
configs map[string]*dynamic.ServiceInfo
configs map[string]*runtime.ServiceInfo
}
// BuildHTTP Creates a http.Handler for a service configuration.
@ -58,13 +59,14 @@ func (m *Manager) BuildHTTP(rootCtx context.Context, serviceName string, respons
// TODO Should handle multiple service types
// FIXME Check if the service is declared multiple times with different types
if conf.LoadBalancer == nil {
conf.Err = fmt.Errorf("the service %q doesn't have any load balancer", serviceName)
return nil, conf.Err
sErr := fmt.Errorf("the service %q doesn't have any load balancer", serviceName)
conf.AddError(sErr, true)
return nil, sErr
}
lb, err := m.getLoadBalancerServiceHandler(ctx, serviceName, conf.LoadBalancer, responseModifier)
if err != nil {
conf.Err = err
conf.AddError(err, true)
return nil, err
}

View file

@ -8,6 +8,7 @@ import (
"testing"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/config/runtime"
"github.com/containous/traefik/pkg/server/internal"
"github.com/containous/traefik/pkg/testhelpers"
"github.com/stretchr/testify/assert"
@ -287,13 +288,13 @@ func TestManager_Build(t *testing.T) {
testCases := []struct {
desc string
serviceName string
configs map[string]*dynamic.ServiceInfo
configs map[string]*runtime.ServiceInfo
providerName string
}{
{
desc: "Simple service name",
serviceName: "serviceName",
configs: map[string]*dynamic.ServiceInfo{
configs: map[string]*runtime.ServiceInfo{
"serviceName": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{},
@ -304,7 +305,7 @@ func TestManager_Build(t *testing.T) {
{
desc: "Service name with provider",
serviceName: "serviceName@provider-1",
configs: map[string]*dynamic.ServiceInfo{
configs: map[string]*runtime.ServiceInfo{
"serviceName@provider-1": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{},
@ -315,7 +316,7 @@ func TestManager_Build(t *testing.T) {
{
desc: "Service name with provider in context",
serviceName: "serviceName",
configs: map[string]*dynamic.ServiceInfo{
configs: map[string]*runtime.ServiceInfo{
"serviceName@provider-1": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{},

View file

@ -5,7 +5,7 @@ import (
"fmt"
"net"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/config/runtime"
"github.com/containous/traefik/pkg/log"
"github.com/containous/traefik/pkg/server/internal"
"github.com/containous/traefik/pkg/tcp"
@ -13,11 +13,11 @@ import (
// Manager is the TCPHandlers factory
type Manager struct {
configs map[string]*dynamic.TCPServiceInfo
configs map[string]*runtime.TCPServiceInfo
}
// NewManager creates a new manager
func NewManager(conf *dynamic.RuntimeConfiguration) *Manager {
func NewManager(conf *runtime.Configuration) *Manager {
return &Manager{
configs: conf.TCPServices,
}

View file

@ -5,6 +5,7 @@ import (
"testing"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/config/runtime"
"github.com/containous/traefik/pkg/server/internal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -14,7 +15,7 @@ func TestManager_BuildTCP(t *testing.T) {
testCases := []struct {
desc string
serviceName string
configs map[string]*dynamic.TCPServiceInfo
configs map[string]*runtime.TCPServiceInfo
providerName string
expectedError string
}{
@ -27,7 +28,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "missing lb configuration",
serviceName: "test",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"test": {
TCPService: &dynamic.TCPService{},
},
@ -37,7 +38,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "no such host, server is skipped, error is logged",
serviceName: "test",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"test": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -52,7 +53,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "invalid IP address, server is skipped, error is logged",
serviceName: "test",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"test": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -67,7 +68,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Simple service name",
serviceName: "serviceName",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"serviceName": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{},
@ -78,7 +79,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Service name with provider",
serviceName: "serviceName@provider-1",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{},
@ -89,7 +90,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Service name with provider in context",
serviceName: "serviceName",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{},
@ -101,7 +102,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Server with correct host:port as address",
serviceName: "serviceName",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -119,7 +120,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Server with correct ip:port as address",
serviceName: "serviceName",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -137,7 +138,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "missing port in address with hostname, server is skipped, error is logged",
serviceName: "serviceName",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -155,7 +156,7 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "missing port in address with ip, server is skipped, error is logged",
serviceName: "serviceName",
configs: map[string]*dynamic.TCPServiceInfo{
configs: map[string]*runtime.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
@ -177,7 +178,7 @@ func TestManager_BuildTCP(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
manager := NewManager(&dynamic.RuntimeConfiguration{
manager := NewManager(&runtime.Configuration{
TCPServices: test.configs,
})