Build all UDP services on an entrypoint

This commit is contained in:
Julien Salleyron 2020-02-17 18:02:04 +01:00 committed by GitHub
parent 1c764052f7
commit ee6d28b25e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 48 deletions

View file

@ -20,11 +20,11 @@
## dynamic configuration ## ## dynamic configuration ##
[http.routers] [http.routers]
[http.routers.router4] [http.routers.router1]
service = "service1" service = "service1"
rule = "Host(`snitest.net`)" rule = "Host(`snitest.net`)"
[http.routers.router5] [http.routers.router2]
service = "service2" service = "service2"
rule = "Host(`snitest.com`)" rule = "Host(`snitest.com`)"
@ -34,36 +34,4 @@
[http.services.service2] [http.services.service2]
[http.services.service2.loadBalancer] [http.services.service2.loadBalancer]
[[http.services.service2.loadBalancer.servers]] [[http.services.service2.loadBalancer.servers]]
url = "http://127.0.0.1:9010" url = "http://127.0.0.1:9010"
[tcp.routers]
[tcp.routers.router4]
service = "service1"
rule = "HostSNI(`snitest.net`)"
[tcp.routers.router5]
service = "service2"
rule = "HostSNI(`snitest.com`)"
[tcp.services]
[tcp.services.service1]
[tcp.services.service2]
[tcp.services.service2.loadBalancer]
[[tcp.services.service2.loadBalancer.servers]]
address = "127.0.0.1:9010"
[udp.routers]
[udp.routers.router4]
service = "service1"
[udp.routers.router5]
service = "service2"
[udp.services]
[udp.services.service1]
[udp.services.service2]
[udp.services.service2.loadBalancer]
[[udp.services.service2.loadBalancer.servers]]
address = "127.0.0.1:9010"

View file

@ -0,0 +1,37 @@
[global]
checkNewVersion = false
sendAnonymousUsage = false
[log]
level = "DEBUG"
[entryPoints]
[entryPoints.websecure]
address = ":4443"
[entryPoints.udp]
address = ":4443/udp"
[api]
insecure = true
[providers.file]
filename = "{{ .SelfFilename }}"
## dynamic configuration ##
[tcp.routers]
[tcp.routers.router1]
service = "service1"
rule = "HostSNI(`snitest.net`)"
[tcp.routers.router2]
service = "service2"
rule = "HostSNI(`snitest.com`)"
[tcp.services]
[tcp.services.service1]
[tcp.services.service2]
[tcp.services.service2.loadBalancer]
[[tcp.services.service2.loadBalancer.servers]]
address = "127.0.0.1:9010"

View file

@ -0,0 +1,35 @@
[global]
checkNewVersion = false
sendAnonymousUsage = false
[log]
level = "DEBUG"
[entryPoints]
[entryPoints.websecure]
address = ":4443"
[entryPoints.udp]
address = ":4443/udp"
[api]
insecure = true
[providers.file]
filename = "{{ .SelfFilename }}"
## dynamic configuration ##
[udp.routers]
[udp.routers.router1]
service = "service1"
[udp.routers.router2]
service = "service2"
[udp.services]
[udp.services.service1]
[udp.services.service2]
[udp.services.service2.loadBalancer]
[[udp.services.service2.loadBalancer.servers]]
address = "127.0.0.1:9010"

View file

@ -570,7 +570,7 @@ func (s *SimpleSuite) TestTCPRouterConfigErrors(c *check.C) {
} }
func (s *SimpleSuite) TestTCPServiceConfigErrors(c *check.C) { func (s *SimpleSuite) TestTCPServiceConfigErrors(c *check.C) {
file := s.adaptFile(c, "fixtures/service_errors.toml", struct{}{}) file := s.adaptFile(c, "fixtures/tcp/service_errors.toml", struct{}{})
defer os.Remove(file) defer os.Remove(file)
cmd, output := s.traefikCmd(withConfigFile(file)) cmd, output := s.traefikCmd(withConfigFile(file))
@ -606,7 +606,7 @@ func (s *SimpleSuite) TestUDPRouterConfigErrors(c *check.C) {
} }
func (s *SimpleSuite) TestUDPServiceConfigErrors(c *check.C) { func (s *SimpleSuite) TestUDPServiceConfigErrors(c *check.C) {
file := s.adaptFile(c, "fixtures/service_errors.toml", struct{}{}) file := s.adaptFile(c, "fixtures/udp/service_errors.toml", struct{}{})
defer os.Remove(file) defer os.Remove(file)
cmd, output := s.traefikCmd(withConfigFile(file)) cmd, output := s.traefikCmd(withConfigFile(file))
@ -616,7 +616,7 @@ func (s *SimpleSuite) TestUDPServiceConfigErrors(c *check.C) {
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
defer cmd.Process.Kill() defer cmd.Process.Kill()
err = try.GetRequest("http://127.0.0.1:8080/api/udp/services", 3000*time.Millisecond, try.BodyContains(`["the udp service \"service1@file\" does not have any type defined"]`)) err = try.GetRequest("http://127.0.0.1:8080/api/udp/services", 1000*time.Millisecond, try.BodyContains(`["the udp service \"service1@file\" does not have any type defined"]`))
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
err = try.GetRequest("http://127.0.0.1:8080/api/udp/services/service1@file", 1000*time.Millisecond, try.BodyContains(`"status":"disabled"`)) err = try.GetRequest("http://127.0.0.1:8080/api/udp/services/service1@file", 1000*time.Millisecond, try.BodyContains(`"status":"disabled"`))

View file

@ -3,6 +3,7 @@ package udp
import ( import (
"context" "context"
"errors" "errors"
"sort"
"github.com/containous/traefik/v2/pkg/config/runtime" "github.com/containous/traefik/v2/pkg/config/runtime"
"github.com/containous/traefik/v2/pkg/log" "github.com/containous/traefik/v2/pkg/log"
@ -47,23 +48,39 @@ func (m *Manager) BuildHandlers(rootCtx context.Context, entryPoints []string) m
ctx := log.With(rootCtx, log.Str(log.EntryPointName, entryPointName)) ctx := log.With(rootCtx, log.Str(log.EntryPointName, entryPointName))
handler, err := m.buildEntryPointHandler(ctx, routers) if len(routers) > 1 {
log.FromContext(ctx).Warn("Config has more than one udp router for a given entrypoint.")
}
handlers, err := m.buildEntryPointHandlers(ctx, routers)
if err != nil { if err != nil {
log.FromContext(ctx).Error(err) log.FromContext(ctx).Error(err)
continue continue
} }
entryPointHandlers[entryPointName] = handler
if len(handlers) > 0 {
// As UDP support only one router per entrypoint, we only take the first one.
entryPointHandlers[entryPointName] = handlers[0]
}
} }
return entryPointHandlers return entryPointHandlers
} }
func (m *Manager) buildEntryPointHandler(ctx context.Context, configs map[string]*runtime.UDPRouterInfo) (udp.Handler, error) { func (m *Manager) buildEntryPointHandlers(ctx context.Context, configs map[string]*runtime.UDPRouterInfo) ([]udp.Handler, error) {
logger := log.FromContext(ctx) var rtNames []string
for routerName := range configs {
if len(configs) > 1 { rtNames = append(rtNames, routerName)
logger.Warn("Warning: config has more than one udp router for a given entrypoint")
} }
for routerName, routerConfig := range configs {
sort.Slice(rtNames, func(i, j int) bool {
return rtNames[i] > rtNames[j]
})
var handlers []udp.Handler
for _, routerName := range rtNames {
routerConfig := configs[routerName]
ctxRouter := log.With(provider.AddInContext(ctx, routerName), log.Str(log.RouterName, routerName)) ctxRouter := log.With(provider.AddInContext(ctx, routerName), log.Str(log.RouterName, routerName))
logger := log.FromContext(ctxRouter) logger := log.FromContext(ctxRouter)
@ -80,8 +97,9 @@ func (m *Manager) buildEntryPointHandler(ctx context.Context, configs map[string
logger.Error(err) logger.Error(err)
continue continue
} }
return handler, nil
handlers = append(handlers, handler)
} }
return nil, nil return handlers, nil
} }