Small fixes and improvments

This commit is contained in:
Manuel Laufenberg 2017-02-20 20:41:28 +01:00 committed by Emile Vauge
parent cc0733a4fa
commit eb1ffae01b
No known key found for this signature in database
GPG key ID: D808B4C167352E59
5 changed files with 163 additions and 16 deletions

View file

@ -4,6 +4,13 @@ import (
"context"
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
"text/template"
"time"
"github.com/BurntSushi/ty/fun"
"github.com/cenk/backoff"
"github.com/containous/traefik/job"
@ -11,11 +18,6 @@ import (
"github.com/containous/traefik/safe"
"github.com/containous/traefik/types"
rancher "github.com/rancher/go-rancher/client"
"math"
"strconv"
"strings"
"text/template"
"time"
)
const (
@ -72,7 +74,7 @@ func (provider *Rancher) getFrontendRule(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.frontend.rule"); err == nil {
return label
}
return "Host:" + strings.ToLower(strings.Replace(service.Name, "/", "_", -1)) + "." + provider.Domain
return "Host:" + strings.ToLower(strings.Replace(service.Name, "/", ".", -1)) + "." + provider.Domain
}
func (provider *Rancher) getFrontendName(service rancherData) string {
@ -193,13 +195,26 @@ func getServiceLabel(service rancherData, label string) (string, error) {
}
func (provider *Rancher) createClient() (*rancher.RancherClient, error) {
rancherURL := getenv("CATTLE_URL", provider.Endpoint)
accessKey := getenv("CATTLE_ACCESS_KEY", provider.AccessKey)
secretKey := getenv("CATTLE_SECRET_KEY", provider.SecretKey)
return rancher.NewRancherClient(&rancher.ClientOpts{
Url: provider.Endpoint,
AccessKey: provider.AccessKey,
SecretKey: provider.SecretKey,
Url: rancherURL,
AccessKey: accessKey,
SecretKey: secretKey,
})
}
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
@ -207,6 +222,12 @@ func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, p
safe.Go(func() {
operation := func() error {
rancherClient, err := provider.createClient()
if err != nil {
log.Errorf("Failed to create a client for rancher, error: %s", err)
return err
}
ctx := context.Background()
var environments = listRancherEnvironments(rancherClient)
var services = listRancherServices(rancherClient)
@ -214,11 +235,6 @@ func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, p
var rancherData = parseRancherData(environments, services, container)
if err != nil {
log.Errorf("Failed to create a client for rancher, error: %s", err)
return err
}
configuration := provider.loadRancherConfig(rancherData)
configurationChan <- types.ConfigMessage{
ProviderName: "rancher",

View file

@ -87,6 +87,12 @@ func TestRancherGetFrontendRule(t *testing.T) {
},
expected: "Host:foo.rancher.localhost",
},
{
service: rancherData{
Name: "foo/bar",
},
expected: "Host:foo.bar.rancher.localhost",
},
{
service: rancherData{
Name: "test-service",
@ -388,7 +394,7 @@ func TestRancherLoadRancherConfig(t *testing.T) {
{
services: []rancherData{
{
Name: "test-service",
Name: "test/service",
Labels: map[string]string{
"traefik.port": "80",
},
@ -405,7 +411,7 @@ func TestRancherLoadRancherConfig(t *testing.T) {
Routes: map[string]types.Route{
"route-frontend-Host-test-service-rancher-localhost": {
Rule: "Host:test-service.rancher.localhost",
Rule: "Host:test.service.rancher.localhost",
},
},
},