2017-12-02 18:28:38 +00:00
|
|
|
package eureka
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/ArthurHlt/go-eureka-client/eureka"
|
|
|
|
"github.com/containous/traefik/log"
|
|
|
|
"github.com/containous/traefik/provider"
|
|
|
|
"github.com/containous/traefik/provider/label"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Build the configuration from Provider server
|
2018-02-13 08:20:04 +00:00
|
|
|
func (p *Provider) buildConfiguration(apps *eureka.Applications) (*types.Configuration, error) {
|
|
|
|
var eurekaFuncMap = template.FuncMap{
|
2017-12-02 18:28:38 +00:00
|
|
|
"getPort": getPort,
|
|
|
|
"getProtocol": getProtocol,
|
|
|
|
"getWeight": getWeight,
|
|
|
|
"getInstanceID": getInstanceID,
|
|
|
|
}
|
|
|
|
|
|
|
|
templateObjects := struct {
|
|
|
|
Applications []eureka.Application
|
|
|
|
}{
|
2018-02-13 08:20:04 +00:00
|
|
|
Applications: apps.Applications,
|
2017-12-02 18:28:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 08:20:04 +00:00
|
|
|
configuration, err := p.GetConfiguration("templates/eureka.tmpl", eurekaFuncMap, templateObjects)
|
2017-12-02 18:28:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return configuration, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInstanceID(instance eureka.InstanceInfo) string {
|
|
|
|
defaultID := provider.Normalize(instance.IpAddr) + "-" + getPort(instance)
|
|
|
|
return label.GetStringValue(instance.Metadata.Map, label.TraefikBackendID, defaultID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPort(instance eureka.InstanceInfo) string {
|
|
|
|
if instance.SecurePort.Enabled {
|
|
|
|
return strconv.Itoa(instance.SecurePort.Port)
|
|
|
|
}
|
|
|
|
return strconv.Itoa(instance.Port.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProtocol(instance eureka.InstanceInfo) string {
|
|
|
|
if instance.SecurePort.Enabled {
|
|
|
|
return "https"
|
|
|
|
}
|
|
|
|
return label.DefaultProtocol
|
|
|
|
}
|
|
|
|
|
2018-04-11 14:30:04 +00:00
|
|
|
func getWeight(instance eureka.InstanceInfo) int {
|
|
|
|
return label.GetIntValue(instance.Metadata.Map, label.TraefikWeight, label.DefaultWeight)
|
2017-12-02 18:28:38 +00:00
|
|
|
}
|