1
0
Fork 0

Allow configuring server URLs with label providers

This commit is contained in:
Taylor Yelverton 2025-01-09 10:20:06 -06:00 committed by GitHub
parent b0a72960bc
commit 95dd17e020
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 924 additions and 48 deletions

View file

@ -241,16 +241,20 @@ func (p *Provider) addServer(i item, lb *dynamic.ServersLoadBalancer) error {
}
if len(lb.Servers) == 0 {
server := dynamic.Server{}
server.SetDefaults()
lb.Servers = []dynamic.Server{server}
lb.Servers = []dynamic.Server{{}}
}
if i.Address == "" {
return errors.New("address is missing")
}
if lb.Servers[0].URL != "" {
if lb.Servers[0].Scheme != "" || lb.Servers[0].Port != "" {
return errors.New("defining scheme or port is not allowed when URL is defined")
}
return nil
}
port := lb.Servers[0].Port
lb.Servers[0].Port = ""
@ -264,6 +268,10 @@ func (p *Provider) addServer(i item, lb *dynamic.ServersLoadBalancer) error {
scheme := lb.Servers[0].Scheme
lb.Servers[0].Scheme = ""
if scheme == "" {
scheme = "http"
}
lb.Servers[0].URL = fmt.Sprintf("%s://%s", scheme, net.JoinHostPort(i.Address, port))
return nil

View file

@ -1551,6 +1551,194 @@ func Test_buildConfig(t *testing.T) {
},
},
},
{
desc: "one service with label url",
items: []item{
{
ID: "id1",
Name: "Test",
Tags: []string{
"traefik.http.services.Service1.LoadBalancer.server.url = http://1.2.3.4:5678",
},
Address: "127.0.0.1",
Port: 9999,
ExtraConf: configuration{Enable: true},
},
},
expected: &dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"Test": {
Service: "Service1",
Rule: "Host(`Test.traefik.test`)",
DefaultRule: true,
},
},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{
"Service1": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://1.2.3.4:5678",
},
},
PassHostHeader: pointer(true),
ResponseForwarding: &dynamic.ResponseForwarding{
FlushInterval: ptypes.Duration(100 * time.Millisecond),
},
},
},
},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{
Stores: map[string]tls.Store{},
},
},
},
{
desc: "one service with label url and preserve path",
items: []item{
{
ID: "id1",
Name: "Test",
Tags: []string{
"traefik.http.services.Service1.LoadBalancer.server.url = http://1.2.3.4:5678",
"traefik.http.services.Service1.LoadBalancer.server.preservepath = true",
},
Address: "127.0.0.1",
Port: 9999,
ExtraConf: configuration{Enable: true},
},
},
expected: &dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"Test": {
Service: "Service1",
Rule: "Host(`Test.traefik.test`)",
DefaultRule: true,
},
},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{
"Service1": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://1.2.3.4:5678",
PreservePath: true,
},
},
PassHostHeader: pointer(true),
ResponseForwarding: &dynamic.ResponseForwarding{
FlushInterval: ptypes.Duration(100 * time.Millisecond),
},
},
},
},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{
Stores: map[string]tls.Store{},
},
},
},
{
desc: "one service with label url and port",
items: []item{
{
ID: "id1",
Name: "Test",
Tags: []string{
"traefik.http.services.Service1.LoadBalancer.server.url = http://1.2.3.4:5678",
"traefik.http.services.Service1.LoadBalancer.server.port = 1234",
},
Address: "127.0.0.1",
Port: 9999,
ExtraConf: configuration{Enable: true},
},
},
expected: &dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{
Stores: map[string]tls.Store{},
},
},
},
{
desc: "one service with label url and scheme",
items: []item{
{
ID: "id1",
Name: "Test",
Tags: []string{
"traefik.http.services.Service1.LoadBalancer.server.url = http://1.2.3.4:5678",
"traefik.http.services.Service1.LoadBalancer.server.scheme = https",
},
Address: "127.0.0.1",
Port: 9999,
ExtraConf: configuration{Enable: true},
},
},
expected: &dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{
Stores: map[string]tls.Store{},
},
},
},
{
desc: "one service with label port on two services",
items: []item{