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

@ -23,6 +23,7 @@ type Configurations map[string]*Configuration
type Configuration struct {
HTTP *HTTPConfiguration `json:"http,omitempty" toml:"http,omitempty" yaml:"http,omitempty"`
TCP *TCPConfiguration `json:"tcp,omitempty" toml:"tcp,omitempty" yaml:"tcp,omitempty"`
UDP *UDPConfiguration `json:"udp,omitempty" toml:"udp,omitempty" yaml:"udp,omitempty"`
TLS *TLSConfiguration `json:"tls,omitempty" toml:"tls,omitempty" yaml:"tls,omitempty"`
}

View file

@ -0,0 +1,82 @@
package dynamic
import (
"reflect"
)
// +k8s:deepcopy-gen=true
// UDPConfiguration contains all the UDP configuration parameters.
type UDPConfiguration struct {
Routers map[string]*UDPRouter `json:"routers,omitempty" toml:"routers,omitempty" yaml:"routers,omitempty"`
Services map[string]*UDPService `json:"services,omitempty" toml:"services,omitempty" yaml:"services,omitempty"`
}
// +k8s:deepcopy-gen=true
// UDPService defines the configuration for a UDP service. All fields are mutually exclusive.
type UDPService struct {
LoadBalancer *UDPServersLoadBalancer `json:"loadBalancer,omitempty" toml:"loadBalancer,omitempty" yaml:"loadBalancer,omitempty"`
Weighted *UDPWeightedRoundRobin `json:"weighted,omitempty" toml:"weighted,omitempty" yaml:"weighted,omitempty" label:"-"`
}
// +k8s:deepcopy-gen=true
// UDPWeightedRoundRobin is a weighted round robin UDP load-balancer of services.
type UDPWeightedRoundRobin struct {
Services []UDPWRRService `json:"services,omitempty" toml:"services,omitempty" yaml:"services,omitempty"`
}
// +k8s:deepcopy-gen=true
// UDPWRRService is a reference to a UDP service load-balanced with weighted round robin.
type UDPWRRService struct {
Name string `json:"name,omitempty" toml:"name,omitempty" yaml:"name,omitempty"`
Weight *int `json:"weight,omitempty" toml:"weight,omitempty" yaml:"weight,omitempty"`
}
// SetDefaults sets the default values for a UDPWRRService.
func (w *UDPWRRService) SetDefaults() {
defaultWeight := 1
w.Weight = &defaultWeight
}
// +k8s:deepcopy-gen=true
// UDPRouter defines the configuration for an UDP router.
type UDPRouter struct {
EntryPoints []string `json:"entryPoints,omitempty" toml:"entryPoints,omitempty" yaml:"entryPoints,omitempty"`
Service string `json:"service,omitempty" toml:"service,omitempty" yaml:"service,omitempty"`
}
// +k8s:deepcopy-gen=true
// UDPServersLoadBalancer defines the configuration for a load-balancer of UDP servers.
type UDPServersLoadBalancer struct {
Servers []UDPServer `json:"servers,omitempty" toml:"servers,omitempty" yaml:"servers,omitempty" label-slice-as-struct:"server"`
}
// Mergeable reports whether the given load-balancer can be merged with the receiver.
func (l *UDPServersLoadBalancer) Mergeable(loadBalancer *UDPServersLoadBalancer) bool {
savedServers := l.Servers
defer func() {
l.Servers = savedServers
}()
l.Servers = nil
savedServersLB := loadBalancer.Servers
defer func() {
loadBalancer.Servers = savedServersLB
}()
loadBalancer.Servers = nil
return reflect.DeepEqual(l, loadBalancer)
}
// +k8s:deepcopy-gen=true
// UDPServer defines a UDP server configuration.
type UDPServer struct {
Address string `json:"address,omitempty" toml:"address,omitempty" yaml:"address,omitempty" label:"-"`
Port string `toml:"-" json:"-" yaml:"-"`
}

View file

@ -22,11 +22,13 @@ type Configuration struct {
Services map[string]*ServiceInfo `json:"services,omitempty"`
TCPRouters map[string]*TCPRouterInfo `json:"tcpRouters,omitempty"`
TCPServices map[string]*TCPServiceInfo `json:"tcpServices,omitempty"`
UDPRouters map[string]*UDPRouterInfo `json:"udpRouters,omitempty"`
UDPServices map[string]*UDPServiceInfo `json:"updServices,omitempty"`
}
// NewConfig returns a Configuration initialized with the given conf. It never returns nil.
func NewConfig(conf dynamic.Configuration) *Configuration {
if conf.HTTP == nil && conf.TCP == nil {
if conf.HTTP == nil && conf.TCP == nil && conf.UDP == nil {
return &Configuration{}
}
@ -74,6 +76,22 @@ func NewConfig(conf dynamic.Configuration) *Configuration {
}
}
if conf.UDP != nil {
if len(conf.UDP.Routers) > 0 {
runtimeConfig.UDPRouters = make(map[string]*UDPRouterInfo, len(conf.UDP.Routers))
for k, v := range conf.UDP.Routers {
runtimeConfig.UDPRouters[k] = &UDPRouterInfo{UDPRouter: v, Status: StatusEnabled}
}
}
if len(conf.UDP.Services) > 0 {
runtimeConfig.UDPServices = make(map[string]*UDPServiceInfo, len(conf.UDP.Services))
for k, v := range conf.UDP.Services {
runtimeConfig.UDPServices[k] = &UDPServiceInfo{UDPService: v, Status: StatusEnabled}
}
}
}
return runtimeConfig
}
@ -158,6 +176,34 @@ func (c *Configuration) PopulateUsedBy() {
sort.Strings(c.TCPServices[k].UsedBy)
}
for routerName, routerInfo := range c.UDPRouters {
// lazily initialize Status in case caller forgot to do it
if routerInfo.Status == "" {
routerInfo.Status = StatusEnabled
}
providerName := getProviderName(routerName)
if providerName == "" {
logger.WithField(log.RouterName, routerName).Error("udp router name is not fully qualified")
continue
}
serviceName := getQualifiedName(providerName, routerInfo.UDPRouter.Service)
if _, ok := c.UDPServices[serviceName]; !ok {
continue
}
c.UDPServices[serviceName].UsedBy = append(c.UDPServices[serviceName].UsedBy, routerName)
}
for k, serviceInfo := range c.UDPServices {
// lazily initialize Status in case caller forgot to do it
if serviceInfo.Status == "" {
serviceInfo.Status = StatusEnabled
}
sort.Strings(c.UDPServices[k].UsedBy)
}
}
func contains(entryPoints []string, entryPointName string) bool {

View file

@ -0,0 +1,114 @@
package runtime
import (
"context"
"fmt"
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/log"
)
// GetUDPRoutersByEntryPoints returns all the UDP routers by entry points name and routers name.
func (c *Configuration) GetUDPRoutersByEntryPoints(ctx context.Context, entryPoints []string) map[string]map[string]*UDPRouterInfo {
entryPointsRouters := make(map[string]map[string]*UDPRouterInfo)
for rtName, rt := range c.UDPRouters {
logger := log.FromContext(log.With(ctx, log.Str(log.RouterName, rtName)))
eps := rt.EntryPoints
if len(eps) == 0 {
logger.Debugf("No entryPoint defined for this router, using the default one(s) instead: %+v", entryPoints)
eps = entryPoints
}
entryPointsCount := 0
for _, entryPointName := range eps {
if !contains(entryPoints, entryPointName) {
rt.AddError(fmt.Errorf("entryPoint %q doesn't exist", entryPointName), false)
logger.WithField(log.EntryPointName, entryPointName).
Errorf("entryPoint %q doesn't exist", entryPointName)
continue
}
if _, ok := entryPointsRouters[entryPointName]; !ok {
entryPointsRouters[entryPointName] = make(map[string]*UDPRouterInfo)
}
entryPointsCount++
rt.Using = append(rt.Using, entryPointName)
entryPointsRouters[entryPointName][rtName] = rt
}
if entryPointsCount == 0 {
rt.AddError(fmt.Errorf("no valid entryPoint for this router"), true)
logger.Error("no valid entryPoint for this router")
}
}
return entryPointsRouters
}
// UDPRouterInfo holds information about a currently running UDP router.
type UDPRouterInfo struct {
*dynamic.UDPRouter // dynamic configuration
Err []string `json:"error,omitempty"` // initialization error
// Status reports whether the router is disabled, in a warning state, or all good (enabled).
// If not in "enabled" state, the reason for it should be in the list of Err.
// It is the caller's responsibility to set the initial status.
Status string `json:"status,omitempty"`
Using []string `json:"using,omitempty"` // Effective entry points used by that router.
}
// AddError adds err to r.Err, if it does not already exist.
// If critical is set, r is marked as disabled.
func (r *UDPRouterInfo) AddError(err error, critical bool) {
for _, value := range r.Err {
if value == err.Error() {
return
}
}
r.Err = append(r.Err, err.Error())
if critical {
r.Status = StatusDisabled
return
}
// only set it to "warning" if not already in a worse state
if r.Status != StatusDisabled {
r.Status = StatusWarning
}
}
// UDPServiceInfo holds information about a currently running UDP service.
type UDPServiceInfo struct {
*dynamic.UDPService // dynamic configuration
Err []string `json:"error,omitempty"` // initialization error
// Status reports whether the service is disabled, in a warning state, or all good (enabled).
// If not in "enabled" state, the reason for it should be in the list of Err.
// It is the caller's responsibility to set the initial status.
Status string `json:"status,omitempty"`
UsedBy []string `json:"usedBy,omitempty"` // list of routers using that service
}
// AddError adds err to s.Err, if it does not already exist.
// If critical is set, s is marked as disabled.
func (s *UDPServiceInfo) AddError(err error, critical bool) {
for _, value := range s.Err {
if value == err.Error() {
return
}
}
s.Err = append(s.Err, err.Error())
if critical {
s.Status = StatusDisabled
return
}
// only set it to "warning" if not already in a worse state
if s.Status != StatusDisabled {
s.Status = StatusWarning
}
}

View file

@ -0,0 +1,201 @@
package runtime
import (
"context"
"testing"
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/stretchr/testify/assert"
)
func TestGetUDPRoutersByEntryPoints(t *testing.T) {
testCases := []struct {
desc string
conf dynamic.Configuration
entryPoints []string
expected map[string]map[string]*UDPRouterInfo
}{
{
desc: "Empty Configuration without entrypoint",
conf: dynamic.Configuration{},
entryPoints: []string{""},
expected: map[string]map[string]*UDPRouterInfo{},
},
{
desc: "Empty Configuration with unknown entrypoints",
conf: dynamic.Configuration{},
entryPoints: []string{"foo"},
expected: map[string]map[string]*UDPRouterInfo{},
},
{
desc: "Valid configuration with an unknown entrypoint",
conf: dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`bar.foo`)",
},
},
},
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
},
},
},
},
entryPoints: []string{"foo"},
expected: map[string]map[string]*UDPRouterInfo{},
},
{
desc: "Valid configuration with a known entrypoint",
conf: dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
},
"bar": {
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
},
"foobar": {
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
},
},
},
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
},
"bar": {
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
},
"foobar": {
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
},
},
},
},
entryPoints: []string{"web"},
expected: map[string]map[string]*UDPRouterInfo{
"web": {
"foo": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
},
Status: "enabled",
Using: []string{"web"},
},
"foobar": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
},
Status: "warning",
Err: []string{`entryPoint "webs" doesn't exist`},
Using: []string{"web"},
},
},
},
},
{
desc: "Valid configuration with multiple known entrypoints",
conf: dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
},
"bar": {
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
},
"foobar": {
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
},
},
},
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{
"foo": {
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
},
"bar": {
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
},
"foobar": {
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
},
},
},
},
entryPoints: []string{"web", "webs"},
expected: map[string]map[string]*UDPRouterInfo{
"web": {
"foo": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
},
Status: "enabled",
Using: []string{"web"},
},
"foobar": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
},
Status: "enabled",
Using: []string{"web", "webs"},
},
},
"webs": {
"bar": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"webs"},
Service: "bar-service@myprovider",
},
Status: "enabled",
Using: []string{"webs"},
},
"foobar": {
UDPRouter: &dynamic.UDPRouter{
EntryPoints: []string{"web", "webs"},
Service: "foobar-service@myprovider",
},
Status: "enabled",
Using: []string{"web", "webs"},
},
},
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
runtimeConfig := NewConfig(test.conf)
actual := runtimeConfig.GetUDPRoutersByEntryPoints(context.Background(), test.entryPoints)
assert.Equal(t, test.expected, actual)
})
}
}

View file

@ -1,5 +1,10 @@
package static
import (
"fmt"
"strings"
)
// EntryPoint holds the entry point configuration.
type EntryPoint struct {
Address string `description:"Entry point address." json:"address,omitempty" toml:"address,omitempty" yaml:"address,omitempty"`
@ -8,11 +13,34 @@ type EntryPoint struct {
ForwardedHeaders *ForwardedHeaders `description:"Trust client forwarding headers." json:"forwardedHeaders,omitempty" toml:"forwardedHeaders,omitempty" yaml:"forwardedHeaders,omitempty"`
}
// GetAddress strips any potential protocol part of the address field of the
// entry point, in order to return the actual address.
func (ep EntryPoint) GetAddress() string {
splitN := strings.SplitN(ep.Address, "/", 2)
return splitN[0]
}
// GetProtocol returns the protocol part of the address field of the entry point.
// If none is specified, it defaults to "tcp".
func (ep EntryPoint) GetProtocol() (string, error) {
splitN := strings.SplitN(ep.Address, "/", 2)
if len(splitN) < 2 {
return "tcp", nil
}
protocol := strings.ToLower(splitN[1])
if protocol == "tcp" || protocol == "udp" {
return protocol, nil
}
return "", fmt.Errorf("invalid protocol: %s", splitN[1])
}
// SetDefaults sets the default values.
func (e *EntryPoint) SetDefaults() {
e.Transport = &EntryPointsTransport{}
e.Transport.SetDefaults()
e.ForwardedHeaders = &ForwardedHeaders{}
func (ep *EntryPoint) SetDefaults() {
ep.Transport = &EntryPointsTransport{}
ep.Transport.SetDefaults()
ep.ForwardedHeaders = &ForwardedHeaders{}
}
// ForwardedHeaders Trust client forwarding headers.

View file

@ -0,0 +1,67 @@
package static
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEntryPointProtocol(t *testing.T) {
tests := []struct {
name string
address string
expectedAddress string
expectedProtocol string
expectedError bool
}{
{
name: "Without protocol",
address: "127.0.0.1:8080",
expectedAddress: "127.0.0.1:8080",
expectedProtocol: "tcp",
expectedError: false,
},
{
name: "With TCP protocol in upper case",
address: "127.0.0.1:8080/TCP",
expectedAddress: "127.0.0.1:8080",
expectedProtocol: "tcp",
expectedError: false,
},
{
name: "With UDP protocol in upper case",
address: "127.0.0.1:8080/UDP",
expectedAddress: "127.0.0.1:8080",
expectedProtocol: "udp",
expectedError: false,
},
{
name: "With UDP protocol in weird case",
address: "127.0.0.1:8080/uDp",
expectedAddress: "127.0.0.1:8080",
expectedProtocol: "udp",
expectedError: false,
},
{
name: "With invalid protocol",
address: "127.0.0.1:8080/toto/tata",
expectedError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ep := EntryPoint{
Address: tt.address,
}
protocol, err := ep.GetProtocol()
if tt.expectedError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.expectedProtocol, protocol)
require.Equal(t, tt.expectedAddress, ep.GetAddress())
})
}
}