1
0
Fork 0

UDP support

Co-authored-by: Julien Salleyron <julien.salleyron@gmail.com>
This commit is contained in:
mpl 2020-02-11 01:26:04 +01:00 committed by GitHub
parent 8988c8f9af
commit 115d42e0f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
72 changed files with 4730 additions and 321 deletions

View file

@ -0,0 +1,87 @@
package udp
import (
"context"
"errors"
"github.com/containous/traefik/v2/pkg/config/runtime"
"github.com/containous/traefik/v2/pkg/log"
"github.com/containous/traefik/v2/pkg/server/provider"
udpservice "github.com/containous/traefik/v2/pkg/server/service/udp"
"github.com/containous/traefik/v2/pkg/udp"
)
// NewManager Creates a new Manager
func NewManager(conf *runtime.Configuration,
serviceManager *udpservice.Manager,
) *Manager {
return &Manager{
serviceManager: serviceManager,
conf: conf,
}
}
// Manager is a route/router manager
type Manager struct {
serviceManager *udpservice.Manager
conf *runtime.Configuration
}
func (m *Manager) getUDPRouters(ctx context.Context, entryPoints []string) map[string]map[string]*runtime.UDPRouterInfo {
if m.conf != nil {
return m.conf.GetUDPRoutersByEntryPoints(ctx, entryPoints)
}
return make(map[string]map[string]*runtime.UDPRouterInfo)
}
// BuildHandlers builds the handlers for the given entrypoints
func (m *Manager) BuildHandlers(rootCtx context.Context, entryPoints []string) map[string]udp.Handler {
entryPointsRouters := m.getUDPRouters(rootCtx, entryPoints)
entryPointHandlers := make(map[string]udp.Handler)
for _, entryPointName := range entryPoints {
entryPointName := entryPointName
routers := entryPointsRouters[entryPointName]
ctx := log.With(rootCtx, log.Str(log.EntryPointName, entryPointName))
handler, err := m.buildEntryPointHandler(ctx, routers)
if err != nil {
log.FromContext(ctx).Error(err)
continue
}
entryPointHandlers[entryPointName] = handler
}
return entryPointHandlers
}
func (m *Manager) buildEntryPointHandler(ctx context.Context, configs map[string]*runtime.UDPRouterInfo) (udp.Handler, error) {
logger := log.FromContext(ctx)
if len(configs) > 1 {
logger.Warn("Warning: config has more than one udp router for a given entrypoint")
}
for routerName, routerConfig := range configs {
ctxRouter := log.With(provider.AddInContext(ctx, routerName), log.Str(log.RouterName, routerName))
logger := log.FromContext(ctxRouter)
if routerConfig.Service == "" {
err := errors.New("the service is missing on the udp router")
routerConfig.AddError(err, true)
logger.Error(err)
continue
}
handler, err := m.serviceManager.BuildUDP(ctxRouter, routerConfig.Service)
if err != nil {
routerConfig.AddError(err, true)
logger.Error(err)
continue
}
return handler, nil
}
return nil, nil
}

View file

@ -0,0 +1,144 @@
package udp
import (
"context"
"testing"
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/config/runtime"
"github.com/containous/traefik/v2/pkg/server/service/udp"
"github.com/stretchr/testify/assert"
)
func TestRuntimeConfiguration(t *testing.T) {
testCases := []struct {
desc string
serviceConfig map[string]*runtime.UDPServiceInfo
routerConfig map[string]*runtime.UDPRouterInfo
expectedError int
}{
{
desc: "No error",
serviceConfig: map[string]*runtime.UDPServiceInfo{
"foo-service": {
UDPService: &dynamic.UDPService{
LoadBalancer: &dynamic.UDPServersLoadBalancer{
Servers: []dynamic.UDPServer{
{
Port: "8085",
Address: "127.0.0.1:8085",
},
{
Address: "127.0.0.1:8086",
Port: "8086",
},
},
},
},
},
},
routerConfig: map[string]*runtime.UDPRouterInfo{
"foo": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web"},
Service: "foo-service",
},
},
"bar": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web"},
Service: "foo-service",
},
},
},
expectedError: 0,
},
{
desc: "Router with unknown service",
serviceConfig: map[string]*runtime.UDPServiceInfo{
"foo-service": {
UDPService: &dynamic.UDPService{
LoadBalancer: &dynamic.UDPServersLoadBalancer{
Servers: []dynamic.UDPServer{
{
Address: "127.0.0.1:80",
},
},
},
},
},
},
routerConfig: map[string]*runtime.UDPRouterInfo{
"foo": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web"},
Service: "wrong-service",
},
},
"bar": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web"},
Service: "foo-service",
},
},
},
expectedError: 1,
},
{
desc: "Router with broken service",
serviceConfig: map[string]*runtime.UDPServiceInfo{
"foo-service": {
UDPService: &dynamic.UDPService{
LoadBalancer: nil,
},
},
},
routerConfig: map[string]*runtime.UDPRouterInfo{
"bar": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web"},
Service: "foo-service",
},
},
},
expectedError: 2,
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
entryPoints := []string{"web"}
conf := &runtime.Configuration{
UDPServices: test.serviceConfig,
UDPRouters: test.routerConfig,
}
serviceManager := udp.NewManager(conf)
routerManager := NewManager(conf, serviceManager)
_ = routerManager.BuildHandlers(context.Background(), entryPoints)
// even though conf was passed by argument to the manager builders above,
// it's ok to use it as the result we check, because everything worth checking
// can be accessed by pointers in it.
var allErrors int
for _, v := range conf.UDPServices {
if v.Err != nil {
allErrors++
}
}
for _, v := range conf.UDPRouters {
if len(v.Err) > 0 {
allErrors++
}
}
assert.Equal(t, test.expectedError, allErrors)
})
}
}