1
0
Fork 0

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:
mpl 2019-05-16 10:58:06 +02:00 committed by Traefiker Bot
parent 5cd9396dae
commit f6df556eb0
50 changed files with 2250 additions and 1158 deletions

View file

@ -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
}

View file

@ -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)
}
})