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

@ -1453,3 +1453,72 @@ Træfɪk needs the following policy to read ECS information:
] ]
} }
``` ```
# Rancher backend
Træfɪk can be configured to use Rancher as a backend configuration:
```toml
################################################################
# Rancher configuration backend
################################################################
# Enable Rancher configuration backend
#
# Optional
#
[rancher]
# Default domain used.
# Can be overridden by setting the "traefik.domain" label on an service.
#
# Required
#
domain = "rancher.localhost"
# Enable watch Rancher changes
#
# Optional
# Default: true
#
Watch = true
# Expose Rancher services by default in traefik
#
# Optional
# Default: true
#
ExposedByDefault = false
# Endpoint to use when connecting to Rancher
#
# Optional
# Endpoint = "http://rancherserver.example.com"
# AccessKey to use when connecting to Rancher
#
# Optional
# AccessKey = "XXXXXXXXX"
# SecretKey to use when connecting to Rancher
#
# Optional
# SecretKey = "XXXXXXXXXXX"
```
If you're deploying traefik as a service within rancher, you can alternatively set these labels on the service to let it only fetch data of its current environment. The settings `endpoint`, `accesskey` and `secretkey` can be omitted then.
- `io.rancher.container.create_agent=true`
- `io.rancher.container.agent.role=environment`
Labels can be used on task containers to override default behaviour:
- `traefik.protocol=https`: override the default `http` protocol
- `traefik.weight=10`: assign this weight to the container
- `traefik.enable=false`: disable this container in Træfɪk
- `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}`).
- `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend.
- `traefik.frontend.priority=10`: override default frontend priority
- `traefik.frontend.entryPoints=http,https`: assign this frontend to entry points `http` and `https`. Overrides `defaultEntryPoints`.

View file

@ -0,0 +1,10 @@
traefik:
image: traefik
command: --web --rancher --rancher.domain=rancher.localhost --logLevel=DEBUG
labels:
io.rancher.container.agent.role: environment
io.rancher.container.create_agent: 'true'
ports:
- "80:80"
- "443:443"
- "8080:8080"

View file

@ -4,6 +4,13 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"math"
"os"
"strconv"
"strings"
"text/template"
"time"
"github.com/BurntSushi/ty/fun" "github.com/BurntSushi/ty/fun"
"github.com/cenk/backoff" "github.com/cenk/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
@ -11,11 +18,6 @@ import (
"github.com/containous/traefik/safe" "github.com/containous/traefik/safe"
"github.com/containous/traefik/types" "github.com/containous/traefik/types"
rancher "github.com/rancher/go-rancher/client" rancher "github.com/rancher/go-rancher/client"
"math"
"strconv"
"strings"
"text/template"
"time"
) )
const ( const (
@ -72,7 +74,7 @@ func (provider *Rancher) getFrontendRule(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.frontend.rule"); err == nil { if label, err := getServiceLabel(service, "traefik.frontend.rule"); err == nil {
return label 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 { 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) { 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{ return rancher.NewRancherClient(&rancher.ClientOpts{
Url: provider.Endpoint, Url: rancherURL,
AccessKey: provider.AccessKey, AccessKey: accessKey,
SecretKey: provider.SecretKey, 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 // Provide allows the provider to provide configurations to traefik
// using the given configuration channel. // using the given configuration channel.
func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error { 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() { safe.Go(func() {
operation := func() error { operation := func() error {
rancherClient, err := provider.createClient() rancherClient, err := provider.createClient()
if err != nil {
log.Errorf("Failed to create a client for rancher, error: %s", err)
return err
}
ctx := context.Background() ctx := context.Background()
var environments = listRancherEnvironments(rancherClient) var environments = listRancherEnvironments(rancherClient)
var services = listRancherServices(rancherClient) var services = listRancherServices(rancherClient)
@ -214,11 +235,6 @@ func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, p
var rancherData = parseRancherData(environments, services, container) 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) configuration := provider.loadRancherConfig(rancherData)
configurationChan <- types.ConfigMessage{ configurationChan <- types.ConfigMessage{
ProviderName: "rancher", ProviderName: "rancher",

View file

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

View file

@ -935,6 +935,52 @@
# #
# filename = "ecs.tmpl" # filename = "ecs.tmpl"
################################################################
# Rancher configuration backend
################################################################
# Enable Rancher configuration backend
#
# Optional
#
[rancher]
# Default domain used.
# Can be overridden by setting the "traefik.domain" label on an service.
#
# Required
#
domain = "rancher.localhost"
# Enable watch Rancher changes
#
# Optional
# Default: true
#
Watch = true
# Expose Rancher services by default in traefik
#
# Optional
# Default: true
#
ExposedByDefault = false
# Endpoint to use when connecting to Rancher
#
# Optional
# Endpoint = "http://rancherserver.example.com"
# AccessKey to use when connecting to Rancher
#
# Optional
# AccessKey = "XXXXXXXXX"
# SecretKey to use when connecting to Rancher
#
# Optional
# SecretKey = "XXXXXXXXXXX"
################################################################ ################################################################
# Sample rules # Sample rules