Homogenization of templates: Eureka

This commit is contained in:
Ludovic Fernandez 2018-02-13 09:20:04 +01:00 committed by Traefiker Bot
parent 17e85e31cd
commit abdcb9e332
7 changed files with 115 additions and 88 deletions

View file

@ -754,20 +754,29 @@ func templatesEcsTmpl() (*asset, error) {
return a, nil return a, nil
} }
var _templatesEurekaTmpl = []byte(`[backends]{{range .Applications}} var _templatesEurekaTmpl = []byte(`[backends]
{{ $app := .}} {{range $app := .Applications }}
{{range .Instances}}
[backends.backend{{$app.Name}}.servers.server-{{ getInstanceID . }}]
url = "{{ getProtocol . }}://{{ .IpAddr }}:{{ getPort . }}"
weight = {{ getWeight . }}
{{end}}{{end}}
[frontends]{{range .Applications}} [backends.backend-{{ $app.Name }}]
[frontends.frontend{{.Name}}]
backend = "backend{{.Name}}" {{range $instance := .Instances }}
[backends.backend-{{ $app.Name }}.servers.server-{{ getInstanceID $instance }}]
url = "{{ getProtocol $instance }}://{{ .IpAddr }}:{{ getPort $instance }}"
weight = {{ getWeight $instance }}
{{end}}
{{end}}
[frontends]
{{range $app := .Applications }}
[frontends.frontend-{{ $app.Name }}]
backend = "backend-{{ $app.Name }}"
entryPoints = ["http"] entryPoints = ["http"]
[frontends.frontend{{.Name }}.routes.route-host{{.Name}}]
rule = "Host:{{ .Name | tolower }}" [frontends.frontend-{{ $app.Name }}.routes.route-host{{ $app.Name }}]
rule = "Host:{{ $app.Name | tolower }}"
{{end}} {{end}}
`) `)

View file

@ -432,7 +432,7 @@ func TestDo_globalConfiguration(t *testing.T) {
DebugLogGeneratedTemplate: true, DebugLogGeneratedTemplate: true,
}, },
Endpoint: "eureka Endpoint", Endpoint: "eureka Endpoint",
Delay: "eureka Delay", Delay: flaeg.Duration(30 * time.Second),
} }
config.ECS = &ecs.Provider{ config.ECS = &ecs.Provider{
BaseProvider: provider.BaseProvider{ BaseProvider: provider.BaseProvider{

View file

@ -166,7 +166,7 @@ func NewTraefikDefaultPointersConfiguration() *TraefikConfiguration {
// default Eureka // default Eureka
var defaultEureka eureka.Provider var defaultEureka eureka.Provider
defaultEureka.Delay = "30s" defaultEureka.Delay = flaeg.Duration(30 * time.Second)
// default ServiceFabric // default ServiceFabric
var defaultServiceFabric servicefabric.Provider var defaultServiceFabric servicefabric.Provider

View file

@ -1,7 +1,6 @@
package eureka package eureka
import ( import (
"io/ioutil"
"strconv" "strconv"
"text/template" "text/template"
@ -13,32 +12,21 @@ import (
) )
// Build the configuration from Provider server // Build the configuration from Provider server
func (p *Provider) buildConfiguration() (*types.Configuration, error) { func (p *Provider) buildConfiguration(apps *eureka.Applications) (*types.Configuration, error) {
var EurekaFuncMap = template.FuncMap{ var eurekaFuncMap = template.FuncMap{
"getPort": getPort, "getPort": getPort,
"getProtocol": getProtocol, "getProtocol": getProtocol,
"getWeight": getWeight, "getWeight": getWeight,
"getInstanceID": getInstanceID, "getInstanceID": getInstanceID,
} }
eureka.GetLogger().SetOutput(ioutil.Discard)
client := eureka.NewClient([]string{
p.Endpoint,
})
applications, err := client.GetApplications()
if err != nil {
return nil, err
}
templateObjects := struct { templateObjects := struct {
Applications []eureka.Application Applications []eureka.Application
}{ }{
applications.Applications, Applications: apps.Applications,
} }
configuration, err := p.GetConfiguration("templates/eureka.tmpl", EurekaFuncMap, templateObjects) configuration, err := p.GetConfiguration("templates/eureka.tmpl", eurekaFuncMap, templateObjects)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }

View file

@ -1,14 +1,16 @@
package eureka package eureka
import ( import (
"strconv"
"testing" "testing"
"github.com/ArthurHlt/go-eureka-client/eureka" "github.com/ArthurHlt/go-eureka-client/eureka"
"github.com/containous/traefik/provider/label" "github.com/containous/traefik/provider/label"
"github.com/stretchr/testify/assert"
) )
func TestGetPort(t *testing.T) { func TestGetPort(t *testing.T) {
cases := []struct { testCases := []struct {
expectedPort string expectedPort string
instanceInfo eureka.InstanceInfo instanceInfo eureka.InstanceInfo
}{ }{
@ -36,16 +38,19 @@ func TestGetPort(t *testing.T) {
}, },
} }
for _, c := range cases { for i, test := range testCases {
port := getPort(c.instanceInfo) test := test
if port != c.expectedPort { t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Fatalf("Should have been %s, got %s", c.expectedPort, port) t.Parallel()
}
port := getPort(test.instanceInfo)
assert.Equal(t, test.expectedPort, port)
})
} }
} }
func TestGetProtocol(t *testing.T) { func TestGetProtocol(t *testing.T) {
cases := []struct { testCases := []struct {
expectedProtocol string expectedProtocol string
instanceInfo eureka.InstanceInfo instanceInfo eureka.InstanceInfo
}{ }{
@ -72,16 +77,20 @@ func TestGetProtocol(t *testing.T) {
}, },
}, },
} }
for _, c := range cases {
protocol := getProtocol(c.instanceInfo) for i, test := range testCases {
if protocol != c.expectedProtocol { test := test
t.Fatalf("Should have been %s, got %s", c.expectedProtocol, protocol) t.Run(strconv.Itoa(i), func(t *testing.T) {
} t.Parallel()
protocol := getProtocol(test.instanceInfo)
assert.Equal(t, test.expectedProtocol, protocol)
})
} }
} }
func TestGetWeight(t *testing.T) { func TestGetWeight(t *testing.T) {
cases := []struct { testCases := []struct {
expectedWeight string expectedWeight string
instanceInfo eureka.InstanceInfo instanceInfo eureka.InstanceInfo
}{ }{
@ -111,16 +120,19 @@ func TestGetWeight(t *testing.T) {
}, },
} }
for _, c := range cases { for i, test := range testCases {
weight := getWeight(c.instanceInfo) test := test
if weight != c.expectedWeight { t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Fatalf("Should have been %s, got %s", c.expectedWeight, weight) t.Parallel()
}
weight := getWeight(test.instanceInfo)
assert.Equal(t, test.expectedWeight, weight)
})
} }
} }
func TestGetInstanceId(t *testing.T) { func TestGetInstanceId(t *testing.T) {
cases := []struct { testCases := []struct {
expectedID string expectedID string
instanceInfo eureka.InstanceInfo instanceInfo eureka.InstanceInfo
}{ }{
@ -158,10 +170,13 @@ func TestGetInstanceId(t *testing.T) {
}, },
} }
for _, c := range cases { for i, test := range testCases {
id := getInstanceID(c.instanceInfo) test := test
if id != c.expectedID { t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Fatalf("Should have been %s, got %s", c.expectedID, id) t.Parallel()
}
id := getInstanceID(test.instanceInfo)
assert.Equal(t, test.expectedID, id)
})
} }
} }

View file

@ -1,9 +1,12 @@
package eureka package eureka
import ( import (
"io/ioutil"
"time" "time"
"github.com/ArthurHlt/go-eureka-client/eureka"
"github.com/cenk/backoff" "github.com/cenk/backoff"
"github.com/containous/flaeg"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/containous/traefik/provider" "github.com/containous/traefik/provider"
@ -14,16 +17,25 @@ import (
// Provider holds configuration of the Provider provider. // Provider holds configuration of the Provider provider.
type Provider struct { type Provider struct {
provider.BaseProvider `mapstructure:",squash" export:"true"` provider.BaseProvider `mapstructure:",squash" export:"true"`
Endpoint string `description:"Eureka server endpoint"` Endpoint string `description:"Eureka server endpoint"`
Delay string `description:"Override default configuration time between refresh" export:"true"` Delay flaeg.Duration `description:"Override default configuration time between refresh" export:"true"`
} }
// Provide allows the eureka provider to provide configurations to traefik // Provide allows the eureka provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ types.Constraints) error { func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ types.Constraints) error {
eureka.GetLogger().SetOutput(ioutil.Discard)
operation := func() error { operation := func() error {
configuration, err := p.buildConfiguration() client := eureka.NewClient([]string{p.Endpoint})
applications, err := client.GetApplications()
if err != nil {
log.Errorf("Failed to retrieve applications, error: %s", err)
return err
}
configuration, err := p.buildConfiguration(applications)
if err != nil { if err != nil {
log.Errorf("Failed to build configuration for Provider, error: %s", err) log.Errorf("Failed to build configuration for Provider, error: %s", err)
return err return err
@ -34,25 +46,18 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
Configuration: configuration, Configuration: configuration,
} }
var delay time.Duration ticker := time.NewTicker(time.Duration(p.Delay))
if len(p.Delay) > 0 {
var err error
delay, err = time.ParseDuration(p.Delay)
if err != nil {
log.Errorf("Failed to parse delay for Provider, error: %s", err)
return err
}
} else {
delay = time.Second * 30
}
ticker := time.NewTicker(delay)
safe.Go(func() { safe.Go(func() {
for t := range ticker.C { for t := range ticker.C {
log.Debugf("Refreshing Provider %s", t.String()) log.Debugf("Refreshing Provider %s", t.String())
configuration, err := p.buildConfiguration() applications, err := client.GetApplications()
if err != nil {
log.Errorf("Failed to retrieve applications, error: %s", err)
continue
}
configuration, err := p.buildConfiguration(applications)
if err != nil { if err != nil {
log.Errorf("Failed to refresh Provider configuration, error: %s", err) log.Errorf("Failed to refresh Provider configuration, error: %s", err)
continue continue
@ -67,9 +72,6 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
return nil return nil
} }
notify := func(err error, time time.Duration) {
log.Errorf("Provider connection error %+v, retrying in %s", err, time)
}
err := backoff.RetryNotify(operation, job.NewBackOff(backoff.NewExponentialBackOff()), notify) err := backoff.RetryNotify(operation, job.NewBackOff(backoff.NewExponentialBackOff()), notify)
if err != nil { if err != nil {
log.Errorf("Cannot connect to Provider server %+v", err) log.Errorf("Cannot connect to Provider server %+v", err)
@ -77,3 +79,7 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
} }
return nil return nil
} }
func notify(err error, time time.Duration) {
log.Errorf("Provider connection error %+v, retrying in %s", err, time)
}

View file

@ -1,15 +1,24 @@
[backends]{{range .Applications}} [backends]
{{ $app := .}} {{range $app := .Applications }}
{{range .Instances}}
[backends.backend{{$app.Name}}.servers.server-{{ getInstanceID . }}] [backends.backend-{{ $app.Name }}]
url = "{{ getProtocol . }}://{{ .IpAddr }}:{{ getPort . }}"
weight = {{ getWeight . }} {{range $instance := .Instances }}
{{end}}{{end}} [backends.backend-{{ $app.Name }}.servers.server-{{ getInstanceID $instance }}]
url = "{{ getProtocol $instance }}://{{ .IpAddr }}:{{ getPort $instance }}"
weight = {{ getWeight $instance }}
{{end}}
{{end}}
[frontends]
{{range $app := .Applications }}
[frontends.frontend-{{ $app.Name }}]
backend = "backend-{{ $app.Name }}"
entryPoints = ["http"]
[frontends.frontend-{{ $app.Name }}.routes.route-host{{ $app.Name }}]
rule = "Host:{{ $app.Name | tolower }}"
[frontends]{{range .Applications}}
[frontends.frontend{{.Name}}]
backend = "backend{{.Name}}"
entryPoints = ["http"]
[frontends.frontend{{.Name }}.routes.route-host{{.Name}}]
rule = "Host:{{ .Name | tolower }}"
{{end}} {{end}}