feat(consulcatalog): add HealthCheck tags.

This commit is contained in:
Fernandez Ludovic 2018-01-01 03:49:06 +01:00 committed by Traefiker
parent c9b871a03a
commit 97ce77169a
3 changed files with 76 additions and 0 deletions

View file

@ -37,6 +37,7 @@ func (p *CatalogProvider) buildConfiguration(catalog []catalogUpdate) *types.Con
"getCircuitBreaker": p.getCircuitBreaker,
"getLoadBalancer": p.getLoadBalancer,
"getMaxConn": p.getMaxConn,
"getHealthCheck": p.getHealthCheck,
// Frontend functions
"getFrontendRule": p.getFrontendRule,
@ -266,6 +267,23 @@ func (p *CatalogProvider) getMaxConn(tags []string) *types.MaxConn {
}
}
func (p *CatalogProvider) getHealthCheck(tags []string) *types.HealthCheck {
path := p.getAttribute(label.SuffixBackendHealthCheckPath, tags, "")
if len(path) == 0 {
return nil
}
port := p.getIntAttribute(label.SuffixBackendHealthCheckPort, tags, label.DefaultBackendHealthCheckPort)
interval := p.getAttribute(label.SuffixBackendHealthCheckInterval, tags, "")
return &types.HealthCheck{
Path: path,
Port: port,
Interval: interval,
}
}
// Base functions
func (p *CatalogProvider) getFuncStringAttribute(name string, defaultValue string) func(tags []string) string {

View file

@ -946,3 +946,53 @@ func TestCatalogProviderGetMaxConn(t *testing.T) {
})
}
}
func TestCatalogProviderGetHealthCheck(t *testing.T) {
p := &CatalogProvider{
Prefix: "traefik",
}
testCases := []struct {
desc string
tags []string
expected *types.HealthCheck
}{
{
desc: "should return nil when no tags",
tags: []string{},
expected: nil,
},
{
desc: "should return nil when Path tag is missing",
tags: []string{
label.TraefikBackendHealthCheckPort + "=80",
label.TraefikBackendHealthCheckInterval + "=7",
},
expected: nil,
},
{
desc: "should return a struct when has tags",
tags: []string{
label.TraefikBackendHealthCheckPath + "=/health",
label.TraefikBackendHealthCheckPort + "=80",
label.TraefikBackendHealthCheckInterval + "=7",
},
expected: &types.HealthCheck{
Path: "/health",
Port: 80,
Interval: "7",
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
result := p.getHeathCheck(test.tags)
assert.Equal(t, test.expected, result)
})
}
}

View file

@ -26,6 +26,14 @@
amount = {{ $maxConn.Amount }}
{{end}}
{{ $healthCheck := getHealthCheck $service.Attributes }}
{{if $healthCheck }}
[backends.backend-{{ $sname }}.healthCheck]
path = "{{ $healthCheck.Path }}"
port = {{ $healthCheck.Port }}
interval = "{{ $healthCheck.Interval }}"
{{end}}
{{end}}
{{range $index, $node := .Nodes}}