2017-04-17 10:50:02 +00:00
|
|
|
package eureka
|
2016-08-31 20:43:05 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-30 08:21:13 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
2016-08-31 20:43:05 +00:00
|
|
|
"github.com/ArthurHlt/go-eureka-client/eureka"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/cenk/backoff"
|
|
|
|
"github.com/containous/traefik/job"
|
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 {
|
|
|
|
provider.BaseProvider `mapstructure:",squash"`
|
2017-05-11 17:07:45 +00:00
|
|
|
Endpoint string `description:"Eureka server endpoint"`
|
|
|
|
Delay string `description:"Override default configuration time between refresh"`
|
2016-08-31 20:43:05 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, _ types.Constraints) error {
|
2016-08-31 20:43:05 +00:00
|
|
|
|
|
|
|
operation := func() error {
|
2017-04-17 10:50:02 +00:00
|
|
|
configuration, err := p.buildConfiguration()
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
var delay time.Duration
|
2017-04-17 10:50:02 +00:00
|
|
|
if len(p.Delay) > 0 {
|
2016-08-31 20:43:05 +00:00
|
|
|
var err error
|
2017-04-17 10:50:02 +00:00
|
|
|
delay, err = time.ParseDuration(p.Delay)
|
2016-08-31 20:43:05 +00:00
|
|
|
if err != nil {
|
2017-04-17 10:50:02 +00:00
|
|
|
log.Errorf("Failed to parse delay for Provider, error: %s", err)
|
2016-08-31 20:43:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delay = time.Second * 30
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(delay)
|
|
|
|
go func() {
|
|
|
|
for t := range ticker.C {
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
log.Debug("Refreshing Provider " + t.String())
|
2016-08-31 20:43:05 +00:00
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
configuration, err := p.buildConfiguration()
|
2016-08-31 20:43:05 +00:00
|
|
|
if err != nil {
|
2017-04-17 10:50:02 +00:00
|
|
|
log.Errorf("Failed to refresh Provider configuration, error: %s", err)
|
2016-08-31 20:43:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "eureka",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
notify := func(err error, time time.Duration) {
|
2017-04-17 10:50:02 +00:00
|
|
|
log.Errorf("Provider connection error %+v, retrying in %s", err, time)
|
2016-08-31 20:43:05 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Build the configuration from Provider server
|
|
|
|
func (p *Provider) buildConfiguration() (*types.Configuration, error) {
|
2016-08-31 20:43:05 +00:00
|
|
|
var EurekaFuncMap = template.FuncMap{
|
2017-04-17 10:50:02 +00:00
|
|
|
"getPort": p.getPort,
|
|
|
|
"getProtocol": p.getProtocol,
|
|
|
|
"getWeight": p.getWeight,
|
|
|
|
"getInstanceID": p.getInstanceID,
|
2016-08-31 20:43:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eureka.GetLogger().SetOutput(ioutil.Discard)
|
|
|
|
|
|
|
|
client := eureka.NewClient([]string{
|
2017-04-17 10:50:02 +00:00
|
|
|
p.Endpoint,
|
2016-08-31 20:43:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
applications, err := client.GetApplications()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
templateObjects := struct {
|
|
|
|
Applications []eureka.Application
|
|
|
|
}{
|
|
|
|
applications.Applications,
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
configuration, err := p.GetConfiguration("templates/eureka.tmpl", EurekaFuncMap, templateObjects)
|
2016-08-31 20:43:05 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return configuration, nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getPort(instance eureka.InstanceInfo) string {
|
2016-08-31 20:43:05 +00:00
|
|
|
if instance.SecurePort.Enabled {
|
|
|
|
return strconv.Itoa(instance.SecurePort.Port)
|
|
|
|
}
|
|
|
|
return strconv.Itoa(instance.Port.Port)
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getProtocol(instance eureka.InstanceInfo) string {
|
2016-08-31 20:43:05 +00:00
|
|
|
if instance.SecurePort.Enabled {
|
|
|
|
return "https"
|
|
|
|
}
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getWeight(instance eureka.InstanceInfo) string {
|
2016-08-31 20:43:05 +00:00
|
|
|
if val, ok := instance.Metadata.Map["traefik.weight"]; ok {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) getInstanceID(instance eureka.InstanceInfo) string {
|
2016-08-31 20:43:05 +00:00
|
|
|
if val, ok := instance.Metadata.Map["traefik.backend.id"]; ok {
|
|
|
|
return val
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
return strings.Replace(instance.IpAddr, ".", "-", -1) + "-" + p.getPort(instance)
|
2016-08-31 20:43:05 +00:00
|
|
|
}
|