2019-04-05 10:22:04 +00:00
|
|
|
package rancher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
2019-04-05 10:22:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-09-30 16:12:04 +00:00
|
|
|
func Int(v int) *int { return &v }
|
|
|
|
func Bool(v bool) *bool { return &v }
|
2019-09-13 15:46:04 +00:00
|
|
|
|
2019-04-05 10:22:04 +00:00
|
|
|
func Test_buildConfiguration(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
containers []rancherData
|
2019-06-21 07:24:04 +00:00
|
|
|
constraints string
|
2019-07-10 07:26:04 +00:00
|
|
|
expected *dynamic.Configuration
|
2019-04-05 10:22:04 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "one service no label",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
|
|
|
Service: "Test",
|
|
|
|
Rule: "Host(`Test.traefik.wtf`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "two services no label",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test1",
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Test2",
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.2"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test1": {
|
|
|
|
Service: "Test1",
|
|
|
|
Rule: "Host(`Test1.traefik.wtf`)",
|
|
|
|
},
|
|
|
|
"Test2": {
|
|
|
|
Service: "Test2",
|
|
|
|
Rule: "Host(`Test2.traefik.wtf`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test1": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"Test2": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.2:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "two services no label multiple containers",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test1",
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1", "127.0.0.2"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Test2",
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"128.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test1": {
|
|
|
|
Service: "Test1",
|
|
|
|
Rule: "Host(`Test1.traefik.wtf`)",
|
|
|
|
},
|
|
|
|
"Test2": {
|
|
|
|
Service: "Test2",
|
|
|
|
Rule: "Host(`Test2.traefik.wtf`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test1": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.2:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"Test2": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://128.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service some labels",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
2019-06-05 20:18:06 +00:00
|
|
|
"traefik.http.services.Service1.loadbalancer.passhostheader": "true",
|
|
|
|
"traefik.http.routers.Router1.rule": "Host(`foo.com`)",
|
|
|
|
"traefik.http.routers.Router1.service": "Service1",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Router1": {
|
|
|
|
Service: "Service1",
|
|
|
|
Rule: "Host(`foo.com`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Service1": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service which is unhealthy",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "broken",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service which is upgrading",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "upgradefailed",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service with rule label and has a host exposed port",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.http.routers.Router1.rule": "Host(`foo.com`)",
|
|
|
|
},
|
|
|
|
Port: "12345:80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Router1": {
|
|
|
|
Service: "Test",
|
|
|
|
Rule: "Host(`foo.com`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service with non matching constraints",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.http.routers.Router1.rule": "Host(`foo.com`)",
|
|
|
|
},
|
|
|
|
Port: "12345:80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:24:04 +00:00
|
|
|
constraints: `Label("traefik.tags", "bar")`,
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "one service with matching constraints",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.tags": "foo",
|
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:24:04 +00:00
|
|
|
constraints: `Label("traefik.tags", "foo")`,
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
|
|
|
Service: "Test",
|
|
|
|
Rule: "Host(`Test.traefik.wtf`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Middlewares used in router",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.http.middlewares.Middleware1.basicauth.users": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
|
|
"traefik.http.routers.Test.middlewares": "Middleware1",
|
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
|
|
|
Service: "Test",
|
|
|
|
Rule: "Host(`Test.traefik.wtf`)",
|
|
|
|
Middlewares: []string{"Middleware1"},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Middleware1": {
|
2019-07-10 07:26:04 +00:00
|
|
|
BasicAuth: &dynamic.BasicAuth{
|
2019-04-05 10:22:04 +00:00
|
|
|
Users: []string{
|
|
|
|
"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
|
|
|
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Port in labels",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.http.services.Test.loadbalancer.server.port": "80",
|
|
|
|
},
|
|
|
|
Port: "",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
|
|
|
Service: "Test",
|
|
|
|
Rule: "Host(`Test.traefik.wtf`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "tcp with label",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.tcp.routers.foo.rule": "HostSNI(`foo.bar`)",
|
|
|
|
"traefik.tcp.routers.foo.tls": "true",
|
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{
|
2019-04-05 10:22:04 +00:00
|
|
|
"foo": {
|
|
|
|
Service: "Test",
|
|
|
|
Rule: "HostSNI(`foo.bar`)",
|
2019-07-10 07:26:04 +00:00
|
|
|
TLS: &dynamic.RouterTCPTLSConfig{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.TCPService{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
|
|
|
Address: "127.0.0.1:80",
|
|
|
|
},
|
|
|
|
},
|
2019-09-13 15:46:04 +00:00
|
|
|
TerminationDelay: Int(100),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "tcp with label without rule",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.tcp.routers.foo.tls": "true",
|
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
|
|
|
Address: "127.0.0.1:80",
|
|
|
|
},
|
|
|
|
},
|
2019-09-13 15:46:04 +00:00
|
|
|
TerminationDelay: Int(100),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "tcp with label and port",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.tcp.routers.foo.rule": "HostSNI(`foo.bar`)",
|
|
|
|
"traefik.tcp.services.foo.loadbalancer.server.port": "8080",
|
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{
|
2019-04-05 10:22:04 +00:00
|
|
|
"foo": {
|
|
|
|
Service: "foo",
|
|
|
|
Rule: "HostSNI(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.TCPService{
|
2019-04-05 10:22:04 +00:00
|
|
|
"foo": {
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
|
|
|
Address: "127.0.0.1:8080",
|
|
|
|
},
|
|
|
|
},
|
2019-09-13 15:46:04 +00:00
|
|
|
TerminationDelay: Int(100),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "tcp with label and port and http service",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
2019-06-05 20:18:06 +00:00
|
|
|
"traefik.tcp.routers.foo.rule": "HostSNI(`foo.bar`)",
|
|
|
|
"traefik.tcp.routers.foo.tls": "true",
|
|
|
|
"traefik.tcp.services.foo.loadbalancer.server.port": "8080",
|
|
|
|
"traefik.http.services.Service1.loadbalancer.passhostheader": "true",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1", "127.0.0.2"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{
|
2019-04-05 10:22:04 +00:00
|
|
|
"foo": {
|
|
|
|
Service: "foo",
|
|
|
|
Rule: "HostSNI(`foo.bar`)",
|
2019-07-10 07:26:04 +00:00
|
|
|
TLS: &dynamic.RouterTCPTLSConfig{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Services: map[string]*dynamic.TCPService{
|
2019-04-05 10:22:04 +00:00
|
|
|
"foo": {
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
|
|
|
Address: "127.0.0.1:8080",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Address: "127.0.0.2:8080",
|
|
|
|
},
|
|
|
|
},
|
2019-09-13 15:46:04 +00:00
|
|
|
TerminationDelay: Int(100),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Test": {
|
|
|
|
Service: "Service1",
|
|
|
|
Rule: "Host(`Test.traefik.wtf`)",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{
|
2019-04-05 10:22:04 +00:00
|
|
|
"Service1": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.2:80",
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-09-30 16:12:04 +00:00
|
|
|
PassHostHeader: Bool(true),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "tcp with label for tcp service",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.tcp.services.foo.loadbalancer.server.port": "8080",
|
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{
|
2019-04-05 10:22:04 +00:00
|
|
|
"foo": {
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
2019-04-05 10:22:04 +00:00
|
|
|
{
|
|
|
|
Address: "127.0.0.1:8080",
|
|
|
|
},
|
|
|
|
},
|
2019-09-13 15:46:04 +00:00
|
|
|
TerminationDelay: Int(100),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "tcp with label for tcp service, with termination delay",
|
|
|
|
containers: []rancherData{
|
|
|
|
{
|
|
|
|
Name: "Test",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"traefik.tcp.services.foo.loadbalancer.server.port": "8080",
|
|
|
|
"traefik.tcp.services.foo.loadbalancer.terminationdelay": "200",
|
|
|
|
},
|
|
|
|
Port: "80/tcp",
|
|
|
|
Containers: []string{"127.0.0.1"},
|
|
|
|
Health: "",
|
|
|
|
State: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &dynamic.Configuration{
|
|
|
|
TCP: &dynamic.TCPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.TCPRouter{},
|
|
|
|
Services: map[string]*dynamic.TCPService{
|
|
|
|
"foo": {
|
2019-09-13 18:00:06 +00:00
|
|
|
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
2019-09-13 15:46:04 +00:00
|
|
|
Servers: []dynamic.TCPServer{
|
|
|
|
{
|
|
|
|
Address: "127.0.0.1:8080",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TerminationDelay: Int(200),
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
|
|
|
Services: map[string]*dynamic.Service{},
|
2019-04-05 10:22:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
p := Provider{
|
|
|
|
ExposedByDefault: true,
|
|
|
|
DefaultRule: "Host(`{{ normalize .Name }}.traefik.wtf`)",
|
|
|
|
EnableServiceHealthFilter: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Constraints = test.constraints
|
|
|
|
|
|
|
|
err := p.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for i := 0; i < len(test.containers); i++ {
|
|
|
|
var err error
|
|
|
|
test.containers[i].ExtraConf, err = p.getConfiguration(test.containers[i])
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configuration := p.buildConfiguration(context.Background(), test.containers)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, configuration)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|