API: expose runtime representation
Co-authored-by: Julien Salleyron <julien.salleyron@gmail.com> Co-authored-by: Jean-Baptiste Doumenjou <jb.doumenjou@gmail.com>
This commit is contained in:
parent
5cd9396dae
commit
f6df556eb0
50 changed files with 2250 additions and 1158 deletions
|
@ -26,7 +26,7 @@ const (
|
|||
)
|
||||
|
||||
// NewManager creates a new Manager
|
||||
func NewManager(configs map[string]*config.Service, defaultRoundTripper http.RoundTripper) *Manager {
|
||||
func NewManager(configs map[string]*config.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.Service
|
||||
configs map[string]*config.ServiceInfo
|
||||
}
|
||||
|
||||
// BuildHTTP Creates a http.Handler for a service configuration.
|
||||
|
@ -50,15 +50,25 @@ func (m *Manager) BuildHTTP(rootCtx context.Context, serviceName string, respons
|
|||
serviceName = internal.GetQualifiedName(ctx, serviceName)
|
||||
ctx = internal.AddProviderInContext(ctx, serviceName)
|
||||
|
||||
if conf, ok := m.configs[serviceName]; ok {
|
||||
// TODO Should handle multiple service types
|
||||
// FIXME Check if the service is declared multiple times with different types
|
||||
if conf.LoadBalancer != nil {
|
||||
return m.getLoadBalancerServiceHandler(ctx, serviceName, conf.LoadBalancer, responseModifier)
|
||||
}
|
||||
return nil, fmt.Errorf("the service %q doesn't have any load balancer", serviceName)
|
||||
conf, ok := m.configs[serviceName]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("the service %q does not exist", serviceName)
|
||||
}
|
||||
return nil, fmt.Errorf("the service %q does not exits", serviceName)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
lb, err := m.getLoadBalancerServiceHandler(ctx, serviceName, conf.LoadBalancer, responseModifier)
|
||||
if err != nil {
|
||||
conf.Err = err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return lb, nil
|
||||
}
|
||||
|
||||
func (m *Manager) getLoadBalancerServiceHandler(
|
||||
|
@ -158,7 +168,7 @@ func buildHealthCheckOptions(ctx context.Context, lb healthcheck.BalancerHandler
|
|||
}
|
||||
|
||||
if timeout >= interval {
|
||||
logger.Warnf("Health check timeout for backend '%s' should be lower than the health check interval. Interval set to timeout + 1 second (%s).", backend)
|
||||
logger.Warnf("Health check timeout for backend '%s' should be lower than the health check interval. Interval set to timeout + 1 second (%s).", backend, interval)
|
||||
}
|
||||
|
||||
return &healthcheck.Options{
|
||||
|
@ -229,7 +239,8 @@ func (m *Manager) getLoadBalancer(ctx context.Context, serviceName string, servi
|
|||
}
|
||||
}
|
||||
|
||||
if err := m.upsertServers(ctx, lb, service.Servers); err != nil {
|
||||
lbsu := healthcheck.NewLBStatusUpdater(lb, m.configs[serviceName])
|
||||
if err := m.upsertServers(ctx, lbsu, service.Servers); err != nil {
|
||||
return nil, fmt.Errorf("error configuring load balancer for service %s: %v", serviceName, err)
|
||||
}
|
||||
|
||||
|
|
|
@ -275,33 +275,39 @@ func TestManager_Build(t *testing.T) {
|
|||
testCases := []struct {
|
||||
desc string
|
||||
serviceName string
|
||||
configs map[string]*config.Service
|
||||
configs map[string]*config.ServiceInfo
|
||||
providerName string
|
||||
}{
|
||||
{
|
||||
desc: "Simple service name",
|
||||
serviceName: "serviceName",
|
||||
configs: map[string]*config.Service{
|
||||
configs: map[string]*config.ServiceInfo{
|
||||
"serviceName": {
|
||||
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
|
||||
Service: &config.Service{
|
||||
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "Service name with provider",
|
||||
serviceName: "provider-1.serviceName",
|
||||
configs: map[string]*config.Service{
|
||||
configs: map[string]*config.ServiceInfo{
|
||||
"provider-1.serviceName": {
|
||||
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
|
||||
Service: &config.Service{
|
||||
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "Service name with provider in context",
|
||||
serviceName: "serviceName",
|
||||
configs: map[string]*config.Service{
|
||||
configs: map[string]*config.ServiceInfo{
|
||||
"provider-1.serviceName": {
|
||||
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
|
||||
Service: &config.Service{
|
||||
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
|
||||
},
|
||||
},
|
||||
},
|
||||
providerName: "provider-1",
|
||||
|
|
|
@ -13,13 +13,13 @@ import (
|
|||
|
||||
// Manager is the TCPHandlers factory
|
||||
type Manager struct {
|
||||
configs map[string]*config.TCPService
|
||||
configs map[string]*config.TCPServiceInfo
|
||||
}
|
||||
|
||||
// NewManager creates a new manager
|
||||
func NewManager(configs map[string]*config.TCPService) *Manager {
|
||||
func NewManager(conf *config.RuntimeConfiguration) *Manager {
|
||||
return &Manager{
|
||||
configs: configs,
|
||||
configs: conf.TCPServices,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,23 +29,23 @@ func (m *Manager) BuildTCP(rootCtx context.Context, serviceName string) (tcp.Han
|
|||
ctx := internal.AddProviderInContext(rootCtx, serviceQualifiedName)
|
||||
ctx = log.With(ctx, log.Str(log.ServiceName, serviceName))
|
||||
|
||||
// FIXME Check if the service is declared multiple times with different types
|
||||
conf, ok := m.configs[serviceQualifiedName]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("the service %q does not exits", serviceQualifiedName)
|
||||
return nil, fmt.Errorf("the service %q does not exist", serviceQualifiedName)
|
||||
}
|
||||
|
||||
if conf.LoadBalancer == nil {
|
||||
return nil, fmt.Errorf("the service %q doesn't have any TCP load balancer", serviceQualifiedName)
|
||||
conf.Err = fmt.Errorf("the service %q doesn't have any TCP load balancer", serviceQualifiedName)
|
||||
return nil, conf.Err
|
||||
}
|
||||
|
||||
logger := log.FromContext(ctx)
|
||||
|
||||
// FIXME Check if the service is declared multiple times with different types
|
||||
loadBalancer := tcp.NewRRLoadBalancer()
|
||||
|
||||
for _, server := range conf.LoadBalancer.Servers {
|
||||
if _, err := parseIP(server.Address); err != nil {
|
||||
logger.Errorf("Invalid IP address for a %q server %q: %v", serviceQualifiedName, server.Address, err)
|
||||
if _, _, err := net.SplitHostPort(server.Address); err != nil {
|
||||
logger.Errorf("In service %q: %v", serviceQualifiedName, err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -57,20 +57,5 @@ func (m *Manager) BuildTCP(rootCtx context.Context, serviceName string) (tcp.Han
|
|||
|
||||
loadBalancer.AddServer(handler)
|
||||
}
|
||||
|
||||
return loadBalancer, nil
|
||||
}
|
||||
|
||||
func parseIP(s string) (string, error) {
|
||||
ip, _, err := net.SplitHostPort(s)
|
||||
if err == nil {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
ipNoPort := net.ParseIP(s)
|
||||
if ipNoPort == nil {
|
||||
return "", fmt.Errorf("invalid IP Address %s", ipNoPort)
|
||||
}
|
||||
|
||||
return ipNoPort.String(), nil
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/containous/traefik/pkg/config"
|
||||
"github.com/containous/traefik/pkg/server/internal"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
@ -12,49 +14,166 @@ func TestManager_BuildTCP(t *testing.T) {
|
|||
testCases := []struct {
|
||||
desc string
|
||||
serviceName string
|
||||
configs map[string]*config.TCPService
|
||||
configs map[string]*config.TCPServiceInfo
|
||||
providerName string
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
desc: "without configuration",
|
||||
serviceName: "test",
|
||||
configs: nil,
|
||||
expectedError: `the service "test" does not exits`,
|
||||
expectedError: `the service "test" does not exist`,
|
||||
},
|
||||
{
|
||||
desc: "missing lb configuration",
|
||||
serviceName: "test",
|
||||
configs: map[string]*config.TCPService{
|
||||
"test": {},
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"test": {
|
||||
TCPService: &config.TCPService{},
|
||||
},
|
||||
},
|
||||
expectedError: `the service "test" doesn't have any TCP load balancer`,
|
||||
},
|
||||
{
|
||||
desc: "no such host",
|
||||
desc: "no such host, server is skipped, error is logged",
|
||||
serviceName: "test",
|
||||
configs: map[string]*config.TCPService{
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"test": {
|
||||
LoadBalancer: &config.TCPLoadBalancerService{
|
||||
Servers: []config.TCPServer{
|
||||
{Address: "test:31"},
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{
|
||||
Servers: []config.TCPServer{
|
||||
{Address: "test:31"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "invalid IP address",
|
||||
desc: "invalid IP address, server is skipped, error is logged",
|
||||
serviceName: "test",
|
||||
configs: map[string]*config.TCPService{
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"test": {
|
||||
LoadBalancer: &config.TCPLoadBalancerService{
|
||||
Servers: []config.TCPServer{
|
||||
{Address: "foobar"},
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{
|
||||
Servers: []config.TCPServer{
|
||||
{Address: "foobar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "Simple service name",
|
||||
serviceName: "serviceName",
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"serviceName": {
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{Method: "wrr"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "Service name with provider",
|
||||
serviceName: "provider-1.serviceName",
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"provider-1.serviceName": {
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{Method: "wrr"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "Service name with provider in context",
|
||||
serviceName: "serviceName",
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"provider-1.serviceName": {
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{Method: "wrr"},
|
||||
},
|
||||
},
|
||||
},
|
||||
providerName: "provider-1",
|
||||
},
|
||||
{
|
||||
desc: "Server with correct host:port as address",
|
||||
serviceName: "serviceName",
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"provider-1.serviceName": {
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{
|
||||
Servers: []config.TCPServer{
|
||||
{
|
||||
Address: "foobar.com:80",
|
||||
},
|
||||
},
|
||||
Method: "wrr",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
providerName: "provider-1",
|
||||
},
|
||||
{
|
||||
desc: "Server with correct ip:port as address",
|
||||
serviceName: "serviceName",
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"provider-1.serviceName": {
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{
|
||||
Servers: []config.TCPServer{
|
||||
{
|
||||
Address: "192.168.0.12:80",
|
||||
},
|
||||
},
|
||||
Method: "wrr",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
providerName: "provider-1",
|
||||
},
|
||||
{
|
||||
desc: "missing port in address with hostname, server is skipped, error is logged",
|
||||
serviceName: "serviceName",
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"provider-1.serviceName": {
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{
|
||||
Servers: []config.TCPServer{
|
||||
{
|
||||
Address: "foobar.com",
|
||||
},
|
||||
},
|
||||
Method: "wrr",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
providerName: "provider-1",
|
||||
},
|
||||
{
|
||||
desc: "missing port in address with ip, server is skipped, error is logged",
|
||||
serviceName: "serviceName",
|
||||
configs: map[string]*config.TCPServiceInfo{
|
||||
"provider-1.serviceName": {
|
||||
TCPService: &config.TCPService{
|
||||
LoadBalancer: &config.TCPLoadBalancerService{
|
||||
Servers: []config.TCPServer{
|
||||
{
|
||||
Address: "192.168.0.12",
|
||||
},
|
||||
},
|
||||
Method: "wrr",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
providerName: "provider-1",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
@ -62,19 +181,22 @@ func TestManager_BuildTCP(t *testing.T) {
|
|||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
manager := NewManager(test.configs)
|
||||
manager := NewManager(&config.RuntimeConfiguration{
|
||||
TCPServices: test.configs,
|
||||
})
|
||||
|
||||
handler, err := manager.BuildTCP(context.Background(), test.serviceName)
|
||||
ctx := context.Background()
|
||||
if len(test.providerName) > 0 {
|
||||
ctx = internal.AddProviderInContext(ctx, test.providerName+".foobar")
|
||||
}
|
||||
|
||||
handler, err := manager.BuildTCP(ctx, test.serviceName)
|
||||
|
||||
if test.expectedError != "" {
|
||||
if err == nil {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.EqualError(t, err, test.expectedError)
|
||||
require.Nil(t, handler)
|
||||
}
|
||||
assert.EqualError(t, err, test.expectedError)
|
||||
require.Nil(t, handler)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, err)
|
||||
require.NotNil(t, handler)
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue