ConsulCatalog StrictChecks
This commit is contained in:
parent
c5808af4d9
commit
0e89a6bec7
8 changed files with 521 additions and 12 deletions
|
@ -287,11 +287,13 @@ func TestDefaultRule(t *testing.T) {
|
|||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var config Configuration
|
||||
|
||||
config.SetDefaults()
|
||||
config.DefaultRule = test.defaultRule
|
||||
|
||||
p := Provider{
|
||||
Configuration: Configuration{
|
||||
ExposedByDefault: true,
|
||||
DefaultRule: test.defaultRule,
|
||||
},
|
||||
Configuration: config,
|
||||
}
|
||||
|
||||
err := p.Init()
|
||||
|
@ -3125,13 +3127,15 @@ func Test_buildConfiguration(t *testing.T) {
|
|||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var config Configuration
|
||||
|
||||
config.SetDefaults()
|
||||
config.DefaultRule = "Host(`{{ normalize .Name }}.traefik.wtf`)"
|
||||
config.ConnectAware = test.ConnectAware
|
||||
config.Constraints = test.constraints
|
||||
|
||||
p := Provider{
|
||||
Configuration: Configuration{
|
||||
ExposedByDefault: true,
|
||||
DefaultRule: "Host(`{{ normalize .Name }}.traefik.wtf`)",
|
||||
ConnectAware: test.ConnectAware,
|
||||
Constraints: test.constraints,
|
||||
},
|
||||
Configuration: config,
|
||||
}
|
||||
|
||||
err := p.Init()
|
||||
|
@ -3206,3 +3210,449 @@ func extractNSFromProvider(providers []*Provider) []string {
|
|||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func TestFilterHealthStatuses(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
items []itemData
|
||||
strictChecks []string
|
||||
expected *dynamic.Configuration
|
||||
}{
|
||||
{
|
||||
// No value passed in here, we assume the default of ["passing", "warning"]
|
||||
desc: "test default strict checks",
|
||||
strictChecks: defaultStrictChecks(),
|
||||
items: []itemData{
|
||||
{
|
||||
ID: "id",
|
||||
Node: "Node1",
|
||||
Name: "Test1",
|
||||
Address: "127.0.0.1",
|
||||
Port: "80",
|
||||
Labels: nil,
|
||||
Status: api.HealthPassing,
|
||||
},
|
||||
{
|
||||
ID: "id",
|
||||
Node: "Node2",
|
||||
Name: "Test2",
|
||||
Address: "127.0.0.1",
|
||||
Port: "81",
|
||||
Labels: nil,
|
||||
Status: api.HealthWarning,
|
||||
},
|
||||
},
|
||||
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{
|
||||
"Test1": {
|
||||
Service: "Test1",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
"Test2": {
|
||||
Service: "Test2",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
},
|
||||
Middlewares: map[string]*dynamic.Middleware{},
|
||||
Services: map[string]*dynamic.Service{
|
||||
"Test1": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:80",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
"Test2": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:81",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ServersTransports: map[string]*dynamic.ServersTransport{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
// The item's health status is not included in the default checks, do not expect any containers
|
||||
desc: "test status not included",
|
||||
strictChecks: defaultStrictChecks(),
|
||||
items: []itemData{
|
||||
{
|
||||
ID: "id",
|
||||
Node: "Node1",
|
||||
Name: "Test",
|
||||
Address: "127.0.0.1",
|
||||
Port: "80",
|
||||
Labels: nil,
|
||||
Status: api.HealthCritical,
|
||||
},
|
||||
},
|
||||
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{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
// Allow only "warning" status containers to be included
|
||||
desc: "test only include warning",
|
||||
strictChecks: []string{api.HealthWarning},
|
||||
items: []itemData{
|
||||
{
|
||||
ID: "id",
|
||||
Node: "Node1",
|
||||
Name: "Test1",
|
||||
Address: "127.0.0.1",
|
||||
Port: "80",
|
||||
Labels: nil,
|
||||
Status: api.HealthPassing,
|
||||
},
|
||||
{
|
||||
ID: "id2",
|
||||
Node: "Node2",
|
||||
Name: "Test2",
|
||||
Address: "127.0.0.1",
|
||||
Port: "81",
|
||||
Labels: nil,
|
||||
Status: api.HealthWarning,
|
||||
},
|
||||
},
|
||||
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{
|
||||
"Test2": {
|
||||
Service: "Test2",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
},
|
||||
Middlewares: map[string]*dynamic.Middleware{},
|
||||
Services: map[string]*dynamic.Service{
|
||||
"Test2": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:81",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ServersTransports: map[string]*dynamic.ServersTransport{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
// Reject "critical" health status
|
||||
desc: "test critical status not included",
|
||||
strictChecks: defaultStrictChecks(),
|
||||
items: []itemData{
|
||||
{
|
||||
ID: "id",
|
||||
Node: "Node1",
|
||||
Name: "Test1",
|
||||
Address: "127.0.0.1",
|
||||
Port: "80",
|
||||
Labels: nil,
|
||||
Status: api.HealthPassing,
|
||||
},
|
||||
{
|
||||
ID: "id2",
|
||||
Node: "Node2",
|
||||
Name: "Test2",
|
||||
Address: "127.0.0.1",
|
||||
Port: "81",
|
||||
Labels: nil,
|
||||
Status: api.HealthWarning,
|
||||
},
|
||||
{
|
||||
ID: "id3",
|
||||
Node: "Node3",
|
||||
Name: "Test3",
|
||||
Address: "127.0.0.1",
|
||||
Port: "82",
|
||||
Labels: nil,
|
||||
Status: api.HealthCritical,
|
||||
},
|
||||
},
|
||||
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{
|
||||
"Test1": {
|
||||
Service: "Test1",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
"Test2": {
|
||||
Service: "Test2",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
},
|
||||
Middlewares: map[string]*dynamic.Middleware{},
|
||||
Services: map[string]*dynamic.Service{
|
||||
"Test1": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:80",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
"Test2": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:81",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ServersTransports: map[string]*dynamic.ServersTransport{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
// The "any" health status allows for all status types, including ones not yet directly included in Consul
|
||||
desc: "test include 'any' health status",
|
||||
strictChecks: []string{api.HealthAny},
|
||||
items: []itemData{
|
||||
{
|
||||
ID: "id",
|
||||
Node: "Node1",
|
||||
Name: "Test1",
|
||||
Address: "127.0.0.1",
|
||||
Port: "80",
|
||||
Labels: nil,
|
||||
Status: api.HealthPassing,
|
||||
},
|
||||
{
|
||||
ID: "id2",
|
||||
Node: "Node2",
|
||||
Name: "Test2",
|
||||
Address: "127.0.0.1",
|
||||
Port: "81",
|
||||
Labels: nil,
|
||||
Status: api.HealthWarning,
|
||||
},
|
||||
{
|
||||
ID: "id3",
|
||||
Node: "Node3",
|
||||
Name: "Test3",
|
||||
Address: "127.0.0.1",
|
||||
Port: "82",
|
||||
Labels: nil,
|
||||
Status: api.HealthCritical,
|
||||
},
|
||||
{
|
||||
ID: "id4",
|
||||
Node: "Node4",
|
||||
Name: "Test4",
|
||||
Address: "127.0.0.1",
|
||||
Port: "83",
|
||||
Labels: nil,
|
||||
Status: "some unsupported status",
|
||||
},
|
||||
},
|
||||
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{
|
||||
"Test1": {
|
||||
Service: "Test1",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
"Test2": {
|
||||
Service: "Test2",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
"Test3": {
|
||||
Service: "Test3",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
"Test4": {
|
||||
Service: "Test4",
|
||||
Rule: "Host(`foo.bar`)",
|
||||
DefaultRule: true,
|
||||
},
|
||||
},
|
||||
Middlewares: map[string]*dynamic.Middleware{},
|
||||
Services: map[string]*dynamic.Service{
|
||||
"Test1": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:80",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
"Test2": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:81",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
"Test3": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:82",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
"Test4": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:83",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
ResponseForwarding: &dynamic.ResponseForwarding{
|
||||
FlushInterval: ptypes.Duration(100 * time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ServersTransports: map[string]*dynamic.ServersTransport{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var config Configuration
|
||||
|
||||
config.SetDefaults()
|
||||
config.DefaultRule = "Host(`foo.bar`)"
|
||||
|
||||
if test.strictChecks != nil {
|
||||
config.StrictChecks = test.strictChecks
|
||||
}
|
||||
|
||||
p := Provider{
|
||||
Configuration: config,
|
||||
}
|
||||
|
||||
err := p.Init()
|
||||
require.NoError(t, err)
|
||||
|
||||
for i := 0; i < len(test.items); i++ {
|
||||
var err error
|
||||
test.items[i].ExtraConf, err = p.getExtraConf(test.items[i].Labels)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
configuration := p.buildConfiguration(context.Background(), test.items, nil)
|
||||
|
||||
assert.Equal(t, test.expected, configuration)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue