Extract providers to their own package
This is just doing that and making it compile :) Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
2d00758b2e
commit
542c3673e4
36 changed files with 861 additions and 833 deletions
145
provider/eureka/eureka.go
Normal file
145
provider/eureka/eureka.go
Normal file
|
@ -0,0 +1,145 @@
|
|||
package eureka
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/cenk/backoff"
|
||||
"github.com/containous/traefik/job"
|
||||
"github.com/containous/traefik/provider"
|
||||
"github.com/containous/traefik/safe"
|
||||
"github.com/containous/traefik/types"
|
||||
)
|
||||
|
||||
// Provider holds configuration of the Provider provider.
|
||||
type Provider struct {
|
||||
provider.BaseProvider `mapstructure:",squash"`
|
||||
Endpoint string
|
||||
Delay string
|
||||
}
|
||||
|
||||
// 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, _ types.Constraints) error {
|
||||
|
||||
operation := func() error {
|
||||
configuration, err := p.buildConfiguration()
|
||||
if err != nil {
|
||||
log.Errorf("Failed to build configuration for Provider, error: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
configurationChan <- types.ConfigMessage{
|
||||
ProviderName: "eureka",
|
||||
Configuration: configuration,
|
||||
}
|
||||
|
||||
var delay time.Duration
|
||||
if len(p.Delay) > 0 {
|
||||
var err error
|
||||
delay, err = time.ParseDuration(p.Delay)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to parse delay for Provider, error: %s", err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
delay = time.Second * 30
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(delay)
|
||||
go func() {
|
||||
for t := range ticker.C {
|
||||
|
||||
log.Debug("Refreshing Provider " + t.String())
|
||||
|
||||
configuration, err := p.buildConfiguration()
|
||||
if err != nil {
|
||||
log.Errorf("Failed to refresh Provider configuration, error: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
configurationChan <- types.ConfigMessage{
|
||||
ProviderName: "eureka",
|
||||
Configuration: configuration,
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
notify := func(err error, time time.Duration) {
|
||||
log.Errorf("Provider connection error %+v, retrying in %s", err, time)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 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["traefik.weight"]; ok {
|
||||
return val
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
func (p *Provider) getInstanceID(instance eureka.InstanceInfo) string {
|
||||
if val, ok := instance.Metadata.Map["traefik.backend.id"]; ok {
|
||||
return val
|
||||
}
|
||||
return strings.Replace(instance.IpAddr, ".", "-", -1) + "-" + p.getPort(instance)
|
||||
}
|
171
provider/eureka/eureka_test.go
Normal file
171
provider/eureka/eureka_test.go
Normal file
|
@ -0,0 +1,171 @@
|
|||
package eureka
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ArthurHlt/go-eureka-client/eureka"
|
||||
)
|
||||
|
||||
func TestEurekaGetPort(t *testing.T) {
|
||||
cases := []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,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
eurekaProvider := &Provider{}
|
||||
for _, c := range cases {
|
||||
port := eurekaProvider.getPort(c.instanceInfo)
|
||||
if port != c.expectedPort {
|
||||
t.Fatalf("Should have been %s, got %s", c.expectedPort, port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEurekaGetProtocol(t *testing.T) {
|
||||
cases := []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,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
eurekaProvider := &Provider{}
|
||||
for _, c := range cases {
|
||||
protocol := eurekaProvider.getProtocol(c.instanceInfo)
|
||||
if protocol != c.expectedProtocol {
|
||||
t.Fatalf("Should have been %s, got %s", c.expectedProtocol, protocol)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEurekaGetWeight(t *testing.T) {
|
||||
cases := []struct {
|
||||
expectedWeight string
|
||||
instanceInfo eureka.InstanceInfo
|
||||
}{
|
||||
{
|
||||
expectedWeight: "0",
|
||||
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{
|
||||
"traefik.weight": "10",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
eurekaProvider := &Provider{}
|
||||
for _, c := range cases {
|
||||
weight := eurekaProvider.getWeight(c.instanceInfo)
|
||||
if weight != c.expectedWeight {
|
||||
t.Fatalf("Should have been %s, got %s", c.expectedWeight, weight)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEurekaGetInstanceId(t *testing.T) {
|
||||
cases := []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{
|
||||
"traefik.backend.id": "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{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
eurekaProvider := &Provider{}
|
||||
for _, c := range cases {
|
||||
id := eurekaProvider.getInstanceID(c.instanceInfo)
|
||||
if id != c.expectedID {
|
||||
t.Fatalf("Should have been %s, got %s", c.expectedID, id)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue