Homogenization of templates: Eureka
This commit is contained in:
parent
17e85e31cd
commit
abdcb9e332
7 changed files with 115 additions and 88 deletions
|
@ -1,9 +1,12 @@
|
|||
package eureka
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
||||
"github.com/cenk/backoff"
|
||||
"github.com/containous/flaeg"
|
||||
"github.com/containous/traefik/job"
|
||||
"github.com/containous/traefik/log"
|
||||
"github.com/containous/traefik/provider"
|
||||
|
@ -14,16 +17,25 @@ import (
|
|||
// Provider holds configuration of the Provider provider.
|
||||
type Provider struct {
|
||||
provider.BaseProvider `mapstructure:",squash" export:"true"`
|
||||
Endpoint string `description:"Eureka server endpoint"`
|
||||
Delay string `description:"Override default configuration time between refresh" export:"true"`
|
||||
Endpoint string `description:"Eureka server endpoint"`
|
||||
Delay flaeg.Duration `description:"Override default configuration time between refresh" export:"true"`
|
||||
}
|
||||
|
||||
// Provide allows the eureka provider to provide configurations to traefik
|
||||
// using the given configuration channel.
|
||||
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ types.Constraints) error {
|
||||
eureka.GetLogger().SetOutput(ioutil.Discard)
|
||||
|
||||
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 {
|
||||
log.Errorf("Failed to build configuration for Provider, error: %s", err)
|
||||
return err
|
||||
|
@ -34,25 +46,18 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
|
|||
Configuration: configuration,
|
||||
}
|
||||
|
||||
var delay time.Duration
|
||||
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)
|
||||
ticker := time.NewTicker(time.Duration(p.Delay))
|
||||
safe.Go(func() {
|
||||
for t := range ticker.C {
|
||||
|
||||
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 {
|
||||
log.Errorf("Failed to refresh Provider configuration, error: %s", err)
|
||||
continue
|
||||
|
@ -67,9 +72,6 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
|
|||
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)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
func notify(err error, time time.Duration) {
|
||||
log.Errorf("Provider connection error %+v, retrying in %s", err, time)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue