refactor(eureka): rewrite configuration system.
This commit is contained in:
parent
04dd63da1c
commit
5f71a43758
3 changed files with 81 additions and 83 deletions
69
provider/eureka/config.go
Normal file
69
provider/eureka/config.go
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package eureka
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"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
|
||||||
|
func (p *Provider) buildConfiguration() (*types.Configuration, error) {
|
||||||
|
var EurekaFuncMap = template.FuncMap{
|
||||||
|
"getPort": getPort,
|
||||||
|
"getProtocol": getProtocol,
|
||||||
|
"getWeight": getWeight,
|
||||||
|
"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 {
|
||||||
|
Applications []eureka.Application
|
||||||
|
}{
|
||||||
|
applications.Applications,
|
||||||
|
}
|
||||||
|
|
||||||
|
configuration, err := p.GetConfiguration("templates/eureka.tmpl", EurekaFuncMap, templateObjects)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func getWeight(instance eureka.InstanceInfo) string {
|
||||||
|
return label.GetStringValue(instance.Metadata.Map, label.TraefikWeight, label.DefaultWeight)
|
||||||
|
}
|
|
@ -4,10 +4,10 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
"github.com/ArthurHlt/go-eureka-client/eureka"
|
||||||
"github.com/containous/traefik/types"
|
"github.com/containous/traefik/provider/label"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEurekaGetPort(t *testing.T) {
|
func TestGetPort(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
expectedPort string
|
expectedPort string
|
||||||
instanceInfo eureka.InstanceInfo
|
instanceInfo eureka.InstanceInfo
|
||||||
|
@ -36,16 +36,15 @@ func TestEurekaGetPort(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
eurekaProvider := &Provider{}
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
port := eurekaProvider.getPort(c.instanceInfo)
|
port := getPort(c.instanceInfo)
|
||||||
if port != c.expectedPort {
|
if port != c.expectedPort {
|
||||||
t.Fatalf("Should have been %s, got %s", c.expectedPort, port)
|
t.Fatalf("Should have been %s, got %s", c.expectedPort, port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEurekaGetProtocol(t *testing.T) {
|
func TestGetProtocol(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
expectedProtocol string
|
expectedProtocol string
|
||||||
instanceInfo eureka.InstanceInfo
|
instanceInfo eureka.InstanceInfo
|
||||||
|
@ -73,17 +72,15 @@ func TestEurekaGetProtocol(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
eurekaProvider := &Provider{}
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
protocol := eurekaProvider.getProtocol(c.instanceInfo)
|
protocol := getProtocol(c.instanceInfo)
|
||||||
if protocol != c.expectedProtocol {
|
if protocol != c.expectedProtocol {
|
||||||
t.Fatalf("Should have been %s, got %s", c.expectedProtocol, protocol)
|
t.Fatalf("Should have been %s, got %s", c.expectedProtocol, protocol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEurekaGetWeight(t *testing.T) {
|
func TestGetWeight(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
expectedWeight string
|
expectedWeight string
|
||||||
instanceInfo eureka.InstanceInfo
|
instanceInfo eureka.InstanceInfo
|
||||||
|
@ -107,23 +104,22 @@ func TestEurekaGetWeight(t *testing.T) {
|
||||||
},
|
},
|
||||||
Metadata: &eureka.MetaData{
|
Metadata: &eureka.MetaData{
|
||||||
Map: map[string]string{
|
Map: map[string]string{
|
||||||
types.LabelWeight: "10",
|
label.TraefikWeight: "10",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
eurekaProvider := &Provider{}
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
weight := eurekaProvider.getWeight(c.instanceInfo)
|
weight := getWeight(c.instanceInfo)
|
||||||
if weight != c.expectedWeight {
|
if weight != c.expectedWeight {
|
||||||
t.Fatalf("Should have been %s, got %s", c.expectedWeight, weight)
|
t.Fatalf("Should have been %s, got %s", c.expectedWeight, weight)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEurekaGetInstanceId(t *testing.T) {
|
func TestGetInstanceId(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
expectedID string
|
expectedID string
|
||||||
instanceInfo eureka.InstanceInfo
|
instanceInfo eureka.InstanceInfo
|
||||||
|
@ -140,7 +136,7 @@ func TestEurekaGetInstanceId(t *testing.T) {
|
||||||
},
|
},
|
||||||
Metadata: &eureka.MetaData{
|
Metadata: &eureka.MetaData{
|
||||||
Map: map[string]string{
|
Map: map[string]string{
|
||||||
types.LabelBackendID: "MyInstanceId",
|
label.TraefikBackendID: "MyInstanceId",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -162,9 +158,8 @@ func TestEurekaGetInstanceId(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
eurekaProvider := &Provider{}
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
id := eurekaProvider.getInstanceID(c.instanceInfo)
|
id := getInstanceID(c.instanceInfo)
|
||||||
if id != c.expectedID {
|
if id != c.expectedID {
|
||||||
t.Fatalf("Should have been %s, got %s", c.expectedID, id)
|
t.Fatalf("Should have been %s, got %s", c.expectedID, id)
|
||||||
}
|
}
|
|
@ -1,13 +1,8 @@
|
||||||
package eureka
|
package eureka
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"text/template"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
|
||||||
"github.com/cenk/backoff"
|
"github.com/cenk/backoff"
|
||||||
"github.com/containous/traefik/job"
|
"github.com/containous/traefik/job"
|
||||||
"github.com/containous/traefik/log"
|
"github.com/containous/traefik/log"
|
||||||
|
@ -55,7 +50,7 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
|
||||||
safe.Go(func() {
|
safe.Go(func() {
|
||||||
for t := range ticker.C {
|
for t := range ticker.C {
|
||||||
|
|
||||||
log.Debug("Refreshing Provider " + t.String())
|
log.Debugf("Refreshing Provider %s", t.String())
|
||||||
|
|
||||||
configuration, err := p.buildConfiguration()
|
configuration, err := p.buildConfiguration()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -82,64 +77,3 @@ func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *s
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the configuration from Provider server
|
|
||||||
func (p *Provider) buildConfiguration() (*types.Configuration, error) {
|
|
||||||
var EurekaFuncMap = template.FuncMap{
|
|
||||||
"getPort": p.getPort,
|
|
||||||
"getProtocol": p.getProtocol,
|
|
||||||
"getWeight": p.getWeight,
|
|
||||||
"getInstanceID": p.getInstanceID,
|
|
||||||
}
|
|
||||||
|
|
||||||
eureka.GetLogger().SetOutput(ioutil.Discard)
|
|
||||||
|
|
||||||
client := eureka.NewClient([]string{
|
|
||||||
p.Endpoint,
|
|
||||||
})
|
|
||||||
|
|
||||||
applications, err := client.GetApplications()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
templateObjects := struct {
|
|
||||||
Applications []eureka.Application
|
|
||||||
}{
|
|
||||||
applications.Applications,
|
|
||||||
}
|
|
||||||
|
|
||||||
configuration, err := p.GetConfiguration("templates/eureka.tmpl", EurekaFuncMap, templateObjects)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
}
|
|
||||||
return configuration, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Provider) getPort(instance eureka.InstanceInfo) string {
|
|
||||||
if instance.SecurePort.Enabled {
|
|
||||||
return strconv.Itoa(instance.SecurePort.Port)
|
|
||||||
}
|
|
||||||
return strconv.Itoa(instance.Port.Port)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Provider) getProtocol(instance eureka.InstanceInfo) string {
|
|
||||||
if instance.SecurePort.Enabled {
|
|
||||||
return "https"
|
|
||||||
}
|
|
||||||
return "http"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Provider) getWeight(instance eureka.InstanceInfo) string {
|
|
||||||
if val, ok := instance.Metadata.Map[types.LabelWeight]; ok {
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
return "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Provider) getInstanceID(instance eureka.InstanceInfo) string {
|
|
||||||
if val, ok := instance.Metadata.Map[types.LabelBackendID]; ok {
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
return strings.Replace(instance.IpAddr, ".", "-", -1) + "-" + p.getPort(instance)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue