2017-04-17 10:50:02 +00:00
|
|
|
package eureka
|
2016-08-31 20:43:05 +00:00
|
|
|
|
|
|
|
import (
|
2018-02-13 08:20:04 +00:00
|
|
|
"io/ioutil"
|
2016-12-30 08:21:13 +00:00
|
|
|
"time"
|
|
|
|
|
2018-02-13 08:20:04 +00:00
|
|
|
"github.com/ArthurHlt/go-eureka-client/eureka"
|
2016-08-31 20:43:05 +00:00
|
|
|
"github.com/cenk/backoff"
|
2018-02-13 08:20:04 +00:00
|
|
|
"github.com/containous/flaeg"
|
2016-08-31 20:43:05 +00:00
|
|
|
"github.com/containous/traefik/job"
|
2017-06-13 23:22:14 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2017-04-17 10:50:02 +00:00
|
|
|
"github.com/containous/traefik/provider"
|
2016-08-31 20:43:05 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
)
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provider holds configuration of the Provider provider.
|
|
|
|
type Provider struct {
|
2017-10-02 08:32:02 +00:00
|
|
|
provider.BaseProvider `mapstructure:",squash" export:"true"`
|
2018-02-13 08:20:04 +00:00
|
|
|
Endpoint string `description:"Eureka server endpoint"`
|
2018-03-07 09:46:04 +00:00
|
|
|
Delay flaeg.Duration `description:"Override default configuration time between refresh (Deprecated)" export:"true"` // Deprecated
|
|
|
|
RefreshSeconds flaeg.Duration `description:"Override default configuration time between refresh" export:"true"`
|
2016-08-31 20:43:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 07:08:03 +00:00
|
|
|
// Init the provider
|
|
|
|
func (p *Provider) Init(constraints types.Constraints) error {
|
|
|
|
return p.BaseProvider.Init(constraints)
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provide allows the eureka provider to provide configurations to traefik
|
2016-08-31 20:43:05 +00:00
|
|
|
// using the given configuration channel.
|
2018-07-11 07:08:03 +00:00
|
|
|
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error {
|
2018-02-13 08:20:04 +00:00
|
|
|
eureka.GetLogger().SetOutput(ioutil.Discard)
|
2016-08-31 20:43:05 +00:00
|
|
|
|
|
|
|
operation := func() error {
|
2018-02-13 08:20:04 +00:00
|
|
|
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)
|
2016-08-31 20:43:05 +00:00
|
|
|
if err != nil {
|
2017-04-17 10:50:02 +00:00
|
|
|
log.Errorf("Failed to build configuration for Provider, error: %s", err)
|
2016-08-31 20:43:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "eureka",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
|
2018-03-07 09:46:04 +00:00
|
|
|
ticker := time.NewTicker(time.Duration(p.RefreshSeconds))
|
|
|
|
pool.Go(func(stop chan bool) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case t := <-ticker.C:
|
|
|
|
log.Debugf("Refreshing Provider %s", t.String())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "eureka",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
case <-stop:
|
|
|
|
return
|
2016-08-31 20:43:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-19 12:11:45 +00:00
|
|
|
})
|
2016-08-31 20:43:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := backoff.RetryNotify(operation, job.NewBackOff(backoff.NewExponentialBackOff()), notify)
|
|
|
|
if err != nil {
|
2017-04-17 10:50:02 +00:00
|
|
|
log.Errorf("Cannot connect to Provider server %+v", err)
|
2016-08-31 20:43:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-13 08:20:04 +00:00
|
|
|
|
|
|
|
func notify(err error, time time.Duration) {
|
|
|
|
log.Errorf("Provider connection error %+v, retrying in %s", err, time)
|
|
|
|
}
|