1
0
Fork 0

Move dynamic config into a dedicated package.

This commit is contained in:
Ludovic Fernandez 2019-07-10 09:26:04 +02:00 committed by Traefiker Bot
parent 09cc1161c9
commit c8bf8e896a
102 changed files with 3170 additions and 3166 deletions

View file

@ -10,7 +10,7 @@ import (
"net/url"
"time"
"github.com/containous/traefik/pkg/config"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/log"
"github.com/containous/traefik/pkg/types"
)
@ -21,7 +21,7 @@ const StatusClientClosedRequest = 499
// StatusClientClosedRequestText non-standard HTTP status for client disconnection
const StatusClientClosedRequestText = "Client Closed Request"
func buildProxy(passHostHeader bool, responseForwarding *config.ResponseForwarding, defaultRoundTripper http.RoundTripper, bufferPool httputil.BufferPool, responseModifier func(*http.Response) error) (http.Handler, error) {
func buildProxy(passHostHeader bool, responseForwarding *dynamic.ResponseForwarding, defaultRoundTripper http.RoundTripper, bufferPool httputil.BufferPool, responseModifier func(*http.Response) error) (http.Handler, error) {
var flushInterval types.Duration
if responseForwarding != nil {
err := flushInterval.Set(responseForwarding.FlushInterval)

View file

@ -9,7 +9,7 @@ import (
"time"
"github.com/containous/alice"
"github.com/containous/traefik/pkg/config"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/healthcheck"
"github.com/containous/traefik/pkg/log"
"github.com/containous/traefik/pkg/middlewares/accesslog"
@ -26,7 +26,7 @@ const (
)
// NewManager creates a new Manager
func NewManager(configs map[string]*config.ServiceInfo, defaultRoundTripper http.RoundTripper) *Manager {
func NewManager(configs map[string]*dynamic.ServiceInfo, defaultRoundTripper http.RoundTripper) *Manager {
return &Manager{
bufferPool: newBufferPool(),
defaultRoundTripper: defaultRoundTripper,
@ -40,7 +40,7 @@ type Manager struct {
bufferPool httputil.BufferPool
defaultRoundTripper http.RoundTripper
balancers map[string][]healthcheck.BalancerHandler
configs map[string]*config.ServiceInfo
configs map[string]*dynamic.ServiceInfo
}
// BuildHTTP Creates a http.Handler for a service configuration.
@ -74,7 +74,7 @@ func (m *Manager) BuildHTTP(rootCtx context.Context, serviceName string, respons
func (m *Manager) getLoadBalancerServiceHandler(
ctx context.Context,
serviceName string,
service *config.LoadBalancerService,
service *dynamic.LoadBalancerService,
responseModifier func(*http.Response) error,
) (http.Handler, error) {
fwd, err := buildProxy(service.PassHostHeader, service.ResponseForwarding, m.defaultRoundTripper, m.bufferPool, responseModifier)
@ -134,7 +134,7 @@ func (m *Manager) LaunchHealthCheck() {
healthcheck.GetHealthCheck().SetBackendsConfiguration(context.TODO(), backendConfigs)
}
func buildHealthCheckOptions(ctx context.Context, lb healthcheck.BalancerHandler, backend string, hc *config.HealthCheck) *healthcheck.Options {
func buildHealthCheckOptions(ctx context.Context, lb healthcheck.BalancerHandler, backend string, hc *dynamic.HealthCheck) *healthcheck.Options {
if hc == nil || hc.Path == "" {
return nil
}
@ -183,7 +183,7 @@ func buildHealthCheckOptions(ctx context.Context, lb healthcheck.BalancerHandler
}
}
func (m *Manager) getLoadBalancer(ctx context.Context, serviceName string, service *config.LoadBalancerService, fwd http.Handler) (healthcheck.BalancerHandler, error) {
func (m *Manager) getLoadBalancer(ctx context.Context, serviceName string, service *dynamic.LoadBalancerService, fwd http.Handler) (healthcheck.BalancerHandler, error) {
logger := log.FromContext(ctx)
logger.Debug("Creating load-balancer")
@ -210,7 +210,7 @@ func (m *Manager) getLoadBalancer(ctx context.Context, serviceName string, servi
return lb, nil
}
func (m *Manager) upsertServers(ctx context.Context, lb healthcheck.BalancerHandler, servers []config.Server) error {
func (m *Manager) upsertServers(ctx context.Context, lb healthcheck.BalancerHandler, servers []dynamic.Server) error {
logger := log.FromContext(ctx)
for name, srv := range servers {

View file

@ -7,7 +7,7 @@ import (
"strings"
"testing"
"github.com/containous/traefik/pkg/config"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/server/internal"
"github.com/containous/traefik/pkg/testhelpers"
"github.com/stretchr/testify/assert"
@ -26,15 +26,15 @@ func TestGetLoadBalancer(t *testing.T) {
testCases := []struct {
desc string
serviceName string
service *config.LoadBalancerService
service *dynamic.LoadBalancerService
fwd http.Handler
expectError bool
}{
{
desc: "Fails when provided an invalid URL",
serviceName: "test",
service: &config.LoadBalancerService{
Servers: []config.Server{
service: &dynamic.LoadBalancerService{
Servers: []dynamic.Server{
{
URL: ":",
},
@ -46,15 +46,15 @@ func TestGetLoadBalancer(t *testing.T) {
{
desc: "Succeeds when there are no servers",
serviceName: "test",
service: &config.LoadBalancerService{},
service: &dynamic.LoadBalancerService{},
fwd: &MockForwarder{},
expectError: false,
},
{
desc: "Succeeds when stickiness is set",
serviceName: "test",
service: &config.LoadBalancerService{
Stickiness: &config.Stickiness{},
service: &dynamic.LoadBalancerService{
Stickiness: &dynamic.Stickiness{},
},
fwd: &MockForwarder{},
expectError: false,
@ -113,7 +113,7 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
testCases := []struct {
desc string
serviceName string
service *config.LoadBalancerService
service *dynamic.LoadBalancerService
responseModifier func(*http.Response) error
expected []ExpectedResult
@ -121,8 +121,8 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
{
desc: "Load balances between the two servers",
serviceName: "test",
service: &config.LoadBalancerService{
Servers: []config.Server{
service: &dynamic.LoadBalancerService{
Servers: []dynamic.Server{
{
URL: server1.URL,
},
@ -145,8 +145,8 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
{
desc: "StatusBadGateway when the server is not reachable",
serviceName: "test",
service: &config.LoadBalancerService{
Servers: []config.Server{
service: &dynamic.LoadBalancerService{
Servers: []dynamic.Server{
{
URL: "http://foo",
},
@ -161,8 +161,8 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
{
desc: "ServiceUnavailable when no servers are available",
serviceName: "test",
service: &config.LoadBalancerService{
Servers: []config.Server{},
service: &dynamic.LoadBalancerService{
Servers: []dynamic.Server{},
},
expected: []ExpectedResult{
{
@ -173,9 +173,9 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
{
desc: "Always call the same server when stickiness is true",
serviceName: "test",
service: &config.LoadBalancerService{
Stickiness: &config.Stickiness{},
Servers: []config.Server{
service: &dynamic.LoadBalancerService{
Stickiness: &dynamic.Stickiness{},
Servers: []dynamic.Server{
{
URL: server1.URL,
},
@ -198,9 +198,9 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
{
desc: "Sticky Cookie's options set correctly",
serviceName: "test",
service: &config.LoadBalancerService{
Stickiness: &config.Stickiness{HTTPOnlyCookie: true, SecureCookie: true},
Servers: []config.Server{
service: &dynamic.LoadBalancerService{
Stickiness: &dynamic.Stickiness{HTTPOnlyCookie: true, SecureCookie: true},
Servers: []dynamic.Server{
{
URL: server1.URL,
},
@ -218,10 +218,10 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
{
desc: "PassHost passes the host instead of the IP",
serviceName: "test",
service: &config.LoadBalancerService{
Stickiness: &config.Stickiness{},
service: &dynamic.LoadBalancerService{
Stickiness: &dynamic.Stickiness{},
PassHostHeader: true,
Servers: []config.Server{
Servers: []dynamic.Server{
{
URL: serverPassHost.URL,
},
@ -237,9 +237,9 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
{
desc: "PassHost doesn't passe the host instead of the IP",
serviceName: "test",
service: &config.LoadBalancerService{
Stickiness: &config.Stickiness{},
Servers: []config.Server{
service: &dynamic.LoadBalancerService{
Stickiness: &dynamic.Stickiness{},
Servers: []dynamic.Server{
{
URL: serverPassHostFalse.URL,
},
@ -287,16 +287,16 @@ func TestManager_Build(t *testing.T) {
testCases := []struct {
desc string
serviceName string
configs map[string]*config.ServiceInfo
configs map[string]*dynamic.ServiceInfo
providerName string
}{
{
desc: "Simple service name",
serviceName: "serviceName",
configs: map[string]*config.ServiceInfo{
configs: map[string]*dynamic.ServiceInfo{
"serviceName": {
Service: &config.Service{
LoadBalancer: &config.LoadBalancerService{},
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{},
},
},
},
@ -304,10 +304,10 @@ func TestManager_Build(t *testing.T) {
{
desc: "Service name with provider",
serviceName: "serviceName@provider-1",
configs: map[string]*config.ServiceInfo{
configs: map[string]*dynamic.ServiceInfo{
"serviceName@provider-1": {
Service: &config.Service{
LoadBalancer: &config.LoadBalancerService{},
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{},
},
},
},
@ -315,10 +315,10 @@ func TestManager_Build(t *testing.T) {
{
desc: "Service name with provider in context",
serviceName: "serviceName",
configs: map[string]*config.ServiceInfo{
configs: map[string]*dynamic.ServiceInfo{
"serviceName@provider-1": {
Service: &config.Service{
LoadBalancer: &config.LoadBalancerService{},
Service: &dynamic.Service{
LoadBalancer: &dynamic.LoadBalancerService{},
},
},
},

View file

@ -5,7 +5,7 @@ import (
"fmt"
"net"
"github.com/containous/traefik/pkg/config"
"github.com/containous/traefik/pkg/config/dynamic"
"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]*config.TCPServiceInfo
configs map[string]*dynamic.TCPServiceInfo
}
// NewManager creates a new manager
func NewManager(conf *config.RuntimeConfiguration) *Manager {
func NewManager(conf *dynamic.RuntimeConfiguration) *Manager {
return &Manager{
configs: conf.TCPServices,
}

View file

@ -4,7 +4,7 @@ import (
"context"
"testing"
"github.com/containous/traefik/pkg/config"
"github.com/containous/traefik/pkg/config/dynamic"
"github.com/containous/traefik/pkg/server/internal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -14,7 +14,7 @@ func TestManager_BuildTCP(t *testing.T) {
testCases := []struct {
desc string
serviceName string
configs map[string]*config.TCPServiceInfo
configs map[string]*dynamic.TCPServiceInfo
providerName string
expectedError string
}{
@ -27,9 +27,9 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "missing lb configuration",
serviceName: "test",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"test": {
TCPService: &config.TCPService{},
TCPService: &dynamic.TCPService{},
},
},
expectedError: `the service "test" doesn't have any TCP load balancer`,
@ -37,11 +37,11 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "no such host, server is skipped, error is logged",
serviceName: "test",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"test": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{
Servers: []config.TCPServer{
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
Servers: []dynamic.TCPServer{
{Address: "test:31"},
},
},
@ -52,11 +52,11 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "invalid IP address, server is skipped, error is logged",
serviceName: "test",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"test": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{
Servers: []config.TCPServer{
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
Servers: []dynamic.TCPServer{
{Address: "foobar"},
},
},
@ -67,10 +67,10 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Simple service name",
serviceName: "serviceName",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"serviceName": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{},
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{},
},
},
},
@ -78,10 +78,10 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Service name with provider",
serviceName: "serviceName@provider-1",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{},
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{},
},
},
},
@ -89,10 +89,10 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Service name with provider in context",
serviceName: "serviceName",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{},
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{},
},
},
},
@ -101,11 +101,11 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Server with correct host:port as address",
serviceName: "serviceName",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{
Servers: []config.TCPServer{
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
Servers: []dynamic.TCPServer{
{
Address: "foobar.com:80",
},
@ -119,11 +119,11 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "Server with correct ip:port as address",
serviceName: "serviceName",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{
Servers: []config.TCPServer{
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
Servers: []dynamic.TCPServer{
{
Address: "192.168.0.12:80",
},
@ -137,11 +137,11 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "missing port in address with hostname, server is skipped, error is logged",
serviceName: "serviceName",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{
Servers: []config.TCPServer{
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
Servers: []dynamic.TCPServer{
{
Address: "foobar.com",
},
@ -155,11 +155,11 @@ func TestManager_BuildTCP(t *testing.T) {
{
desc: "missing port in address with ip, server is skipped, error is logged",
serviceName: "serviceName",
configs: map[string]*config.TCPServiceInfo{
configs: map[string]*dynamic.TCPServiceInfo{
"serviceName@provider-1": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{
Servers: []config.TCPServer{
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
Servers: []dynamic.TCPServer{
{
Address: "192.168.0.12",
},
@ -177,7 +177,7 @@ func TestManager_BuildTCP(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
manager := NewManager(&config.RuntimeConfiguration{
manager := NewManager(&dynamic.RuntimeConfiguration{
TCPServices: test.configs,
})