From 97ce77169a98930dcad2a7327c95c33bf3da28b8 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 1 Jan 2018 03:49:06 +0100 Subject: [PATCH] feat(consulcatalog): add HealthCheck tags. --- provider/consul/consul_catalog_config.go | 18 +++++++ provider/consul/consul_catalog_config_test.go | 50 +++++++++++++++++++ templates/consul_catalog.tmpl | 8 +++ 3 files changed, 76 insertions(+) diff --git a/provider/consul/consul_catalog_config.go b/provider/consul/consul_catalog_config.go index e72e48752..e2289d98b 100644 --- a/provider/consul/consul_catalog_config.go +++ b/provider/consul/consul_catalog_config.go @@ -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 { diff --git a/provider/consul/consul_catalog_config_test.go b/provider/consul/consul_catalog_config_test.go index 4919145dc..fd1d5f823 100644 --- a/provider/consul/consul_catalog_config_test.go +++ b/provider/consul/consul_catalog_config_test.go @@ -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) + }) + } +} diff --git a/templates/consul_catalog.tmpl b/templates/consul_catalog.tmpl index 66df8bec3..c831778be 100644 --- a/templates/consul_catalog.tmpl +++ b/templates/consul_catalog.tmpl @@ -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}}