Dynamic Configuration Refactoring
This commit is contained in:
parent
d3ae88f108
commit
a09dfa3ce1
452 changed files with 21023 additions and 9419 deletions
57
old/provider/eureka/config.go
Normal file
57
old/provider/eureka/config.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package eureka
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
||||
"github.com/containous/traefik/old/log"
|
||||
"github.com/containous/traefik/old/provider"
|
||||
"github.com/containous/traefik/old/provider/label"
|
||||
"github.com/containous/traefik/old/types"
|
||||
)
|
||||
|
||||
// Build the configuration from Provider server
|
||||
func (p *Provider) buildConfiguration(apps *eureka.Applications) (*types.Configuration, error) {
|
||||
var eurekaFuncMap = template.FuncMap{
|
||||
"getPort": getPort,
|
||||
"getProtocol": getProtocol,
|
||||
"getWeight": getWeight,
|
||||
"getInstanceID": getInstanceID,
|
||||
}
|
||||
|
||||
templateObjects := struct {
|
||||
Applications []eureka.Application
|
||||
}{
|
||||
Applications: apps.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) int {
|
||||
return label.GetIntValue(instance.Metadata.Map, label.TraefikWeight, label.DefaultWeight)
|
||||
}
|
182
old/provider/eureka/config_test.go
Normal file
182
old/provider/eureka/config_test.go
Normal file
|
@ -0,0 +1,182 @@
|
|||
package eureka
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
||||
"github.com/containous/traefik/old/provider/label"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetPort(t *testing.T) {
|
||||
testCases := []struct {
|
||||
expectedPort string
|
||||
instanceInfo eureka.InstanceInfo
|
||||
}{
|
||||
{
|
||||
expectedPort: "80",
|
||||
instanceInfo: eureka.InstanceInfo{
|
||||
SecurePort: &eureka.Port{
|
||||
Port: 443, Enabled: false,
|
||||
},
|
||||
Port: &eureka.Port{
|
||||
Port: 80, Enabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedPort: "443",
|
||||
instanceInfo: eureka.InstanceInfo{
|
||||
SecurePort: &eureka.Port{
|
||||
Port: 443, Enabled: true,
|
||||
},
|
||||
Port: &eureka.Port{
|
||||
Port: 80, Enabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
test := test
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
port := getPort(test.instanceInfo)
|
||||
assert.Equal(t, test.expectedPort, port)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProtocol(t *testing.T) {
|
||||
testCases := []struct {
|
||||
expectedProtocol string
|
||||
instanceInfo eureka.InstanceInfo
|
||||
}{
|
||||
{
|
||||
expectedProtocol: "http",
|
||||
instanceInfo: eureka.InstanceInfo{
|
||||
SecurePort: &eureka.Port{
|
||||
Port: 443, Enabled: false,
|
||||
},
|
||||
Port: &eureka.Port{
|
||||
Port: 80, Enabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedProtocol: "https",
|
||||
instanceInfo: eureka.InstanceInfo{
|
||||
SecurePort: &eureka.Port{
|
||||
Port: 443, Enabled: true,
|
||||
},
|
||||
Port: &eureka.Port{
|
||||
Port: 80, Enabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
test := test
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
protocol := getProtocol(test.instanceInfo)
|
||||
assert.Equal(t, test.expectedProtocol, protocol)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetWeight(t *testing.T) {
|
||||
testCases := []struct {
|
||||
expectedWeight int
|
||||
instanceInfo eureka.InstanceInfo
|
||||
}{
|
||||
{
|
||||
expectedWeight: label.DefaultWeight,
|
||||
instanceInfo: eureka.InstanceInfo{
|
||||
Port: &eureka.Port{
|
||||
Port: 80, Enabled: true,
|
||||
},
|
||||
Metadata: &eureka.MetaData{
|
||||
Map: map[string]string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedWeight: 10,
|
||||
instanceInfo: eureka.InstanceInfo{
|
||||
Port: &eureka.Port{
|
||||
Port: 80, Enabled: true,
|
||||
},
|
||||
Metadata: &eureka.MetaData{
|
||||
Map: map[string]string{
|
||||
label.TraefikWeight: "10",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
test := test
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
weight := getWeight(test.instanceInfo)
|
||||
assert.Equal(t, test.expectedWeight, weight)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetInstanceId(t *testing.T) {
|
||||
testCases := []struct {
|
||||
expectedID string
|
||||
instanceInfo eureka.InstanceInfo
|
||||
}{
|
||||
{
|
||||
expectedID: "MyInstanceId",
|
||||
instanceInfo: eureka.InstanceInfo{
|
||||
IpAddr: "10.11.12.13",
|
||||
SecurePort: &eureka.Port{
|
||||
Port: 443, Enabled: false,
|
||||
},
|
||||
Port: &eureka.Port{
|
||||
Port: 80, Enabled: true,
|
||||
},
|
||||
Metadata: &eureka.MetaData{
|
||||
Map: map[string]string{
|
||||
label.TraefikBackendID: "MyInstanceId",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedID: "10-11-12-13-80",
|
||||
instanceInfo: eureka.InstanceInfo{
|
||||
IpAddr: "10.11.12.13",
|
||||
SecurePort: &eureka.Port{
|
||||
Port: 443, Enabled: false,
|
||||
},
|
||||
Port: &eureka.Port{
|
||||
Port: 80, Enabled: true,
|
||||
},
|
||||
Metadata: &eureka.MetaData{
|
||||
Map: map[string]string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
test := test
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
id := getInstanceID(test.instanceInfo)
|
||||
assert.Equal(t, test.expectedID, id)
|
||||
})
|
||||
}
|
||||
}
|
92
old/provider/eureka/eureka.go
Normal file
92
old/provider/eureka/eureka.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package eureka
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
||||
"github.com/cenk/backoff"
|
||||
"github.com/containous/flaeg/parse"
|
||||
"github.com/containous/traefik/job"
|
||||
"github.com/containous/traefik/old/log"
|
||||
"github.com/containous/traefik/old/provider"
|
||||
"github.com/containous/traefik/old/types"
|
||||
"github.com/containous/traefik/safe"
|
||||
)
|
||||
|
||||
// Provider holds configuration of the Provider provider.
|
||||
type Provider struct {
|
||||
provider.BaseProvider `mapstructure:",squash" export:"true"`
|
||||
Endpoint string `description:"Eureka server endpoint"`
|
||||
RefreshSeconds parse.Duration `description:"Override default configuration time between refresh" export:"true"`
|
||||
}
|
||||
|
||||
// Init the provider
|
||||
func (p *Provider) Init(constraints types.Constraints) error {
|
||||
return p.BaseProvider.Init(constraints)
|
||||
}
|
||||
|
||||
// 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) error {
|
||||
eureka.GetLogger().SetOutput(ioutil.Discard)
|
||||
|
||||
operation := func() error {
|
||||
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
|
||||
}
|
||||
|
||||
configurationChan <- types.ConfigMessage{
|
||||
ProviderName: "eureka",
|
||||
Configuration: configuration,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
err := backoff.RetryNotify(operation, job.NewBackOff(backoff.NewExponentialBackOff()), notify)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot connect to Provider server %+v", err)
|
||||
return err
|
||||
}
|
||||
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