2017-01-28 23:01:56 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2017-02-06 15:28:12 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-02-05 21:54:24 +00:00
|
|
|
"github.com/BurntSushi/ty/fun"
|
2017-01-28 23:01:56 +00:00
|
|
|
"github.com/cenk/backoff"
|
|
|
|
"github.com/containous/traefik/job"
|
2017-02-05 21:54:24 +00:00
|
|
|
"github.com/containous/traefik/log"
|
|
|
|
"github.com/containous/traefik/safe"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
rancher "github.com/rancher/go-rancher/client"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-01-28 23:01:56 +00:00
|
|
|
"text/template"
|
2017-02-06 15:28:12 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-06 15:30:21 +00:00
|
|
|
// RancherDefaultWatchTime is the duration of the interval when polling rancher
|
2017-02-06 15:28:12 +00:00
|
|
|
RancherDefaultWatchTime = 15 * time.Second
|
2017-01-28 23:01:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Provider = (*Rancher)(nil)
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
// Rancher holds configurations of the Rancher provider.
|
2017-01-28 23:01:56 +00:00
|
|
|
type Rancher struct {
|
|
|
|
BaseProvider `mapstructure:",squash"`
|
2017-02-05 21:54:24 +00:00
|
|
|
Endpoint string `description:"Rancher server HTTP(S) endpoint."`
|
|
|
|
AccessKey string `description:"Rancher server access key."`
|
|
|
|
SecretKey string `description:"Rancher server Secret Key."`
|
|
|
|
ExposedByDefault bool `description:"Expose Services by default"`
|
|
|
|
Domain string `description:"Default domain used"`
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rancherData struct {
|
2017-02-05 21:54:24 +00:00
|
|
|
Name string
|
|
|
|
Labels map[string]string // List of labels set to container or service
|
|
|
|
Containers []string
|
|
|
|
Health string
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
func (r rancherData) String() string {
|
2017-01-28 23:01:56 +00:00
|
|
|
return fmt.Sprintf("{name:%s, labels:%v, containers: %v, health: %s}", r.Name, r.Labels, r.Containers, r.Health)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Frontend Labels
|
|
|
|
func (provider *Rancher) getPassHostHeader(service rancherData) string {
|
|
|
|
if passHostHeader, err := getServiceLabel(service, "traefik.frontend.passHostHeader"); err == nil {
|
|
|
|
return passHostHeader
|
|
|
|
}
|
|
|
|
return "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getPriority(service rancherData) string {
|
|
|
|
if priority, err := getServiceLabel(service, "traefik.frontend.priority"); err == nil {
|
|
|
|
return priority
|
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getEntryPoints(service rancherData) []string {
|
|
|
|
if entryPoints, err := getServiceLabel(service, "traefik.frontend.entryPoints"); err == nil {
|
|
|
|
return strings.Split(entryPoints, ",")
|
|
|
|
}
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getFrontendName(service rancherData) string {
|
|
|
|
// Replace '.' with '-' in quoted keys because of this issue https://github.com/BurntSushi/toml/issues/78
|
|
|
|
return normalize(provider.getFrontendRule(service))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backend Labels
|
|
|
|
func (provider *Rancher) getLoadBalancerMethod(service rancherData) string {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.backend.loadbalancer.method"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "wrr"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) hasLoadBalancerLabel(service rancherData) bool {
|
|
|
|
_, errMethod := getServiceLabel(service, "traefik.backend.loadbalancer.method")
|
|
|
|
_, errSticky := getServiceLabel(service, "traefik.backend.loadbalancer.sticky")
|
|
|
|
if errMethod != nil && errSticky != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) hasCircuitBreakerLabel(service rancherData) bool {
|
|
|
|
if _, err := getServiceLabel(service, "traefik.backend.circuitbreaker.expression"); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getCircuitBreakerExpression(service rancherData) string {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.backend.circuitbreaker.expression"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "NetworkErrorRatio() > 1"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getSticky(service rancherData) string {
|
|
|
|
if _, err := getServiceLabel(service, "traefik.backend.loadbalancer.sticky"); err == nil {
|
|
|
|
return "true"
|
|
|
|
}
|
|
|
|
return "false"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getBackend(service rancherData) string {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.backend"); err == nil {
|
|
|
|
return normalize(label)
|
|
|
|
}
|
|
|
|
return normalize(service.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generall Application Stuff
|
|
|
|
func (provider *Rancher) getPort(service rancherData) string {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.port"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getProtocol(service rancherData) string {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.protocol"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getWeight(service rancherData) string {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.weight"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getDomain(service rancherData) string {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.domain"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
2017-02-05 23:58:05 +00:00
|
|
|
return provider.Domain
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) hasMaxConnLabels(service rancherData) bool {
|
|
|
|
if _, err := getServiceLabel(service, "traefik.backend.maxconn.amount"); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, err := getServiceLabel(service, "traefik.backend.maxconn.extractorfunc"); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getMaxConnAmount(service rancherData) int64 {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.backend.maxconn.amount"); err == nil {
|
|
|
|
i, errConv := strconv.ParseInt(label, 10, 64)
|
|
|
|
if errConv != nil {
|
|
|
|
log.Errorf("Unable to parse traefik.backend.maxconn.amount %s", label)
|
|
|
|
return math.MaxInt64
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return math.MaxInt64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) getMaxConnExtractorFunc(service rancherData) string {
|
|
|
|
if label, err := getServiceLabel(service, "traefik.backend.maxconn.extractorfunc"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "request.host"
|
|
|
|
}
|
|
|
|
|
|
|
|
func getServiceLabel(service rancherData, label string) (string, error) {
|
|
|
|
for key, value := range service.Labels {
|
|
|
|
if key == label {
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("Label not found:" + label)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) createClient() (*rancher.RancherClient, error) {
|
|
|
|
return rancher.NewRancherClient(&rancher.ClientOpts{
|
|
|
|
Url: provider.Endpoint,
|
|
|
|
AccessKey: provider.AccessKey,
|
|
|
|
SecretKey: provider.SecretKey,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
// Provide allows the provider to provide configurations to traefik
|
|
|
|
// using the given configuration channel.
|
2017-01-28 23:01:56 +00:00
|
|
|
func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
|
|
|
|
|
|
|
safe.Go(func() {
|
|
|
|
operation := func() error {
|
|
|
|
rancherClient, err := provider.createClient()
|
2017-02-06 15:28:12 +00:00
|
|
|
ctx := context.Background()
|
2017-01-28 23:01:56 +00:00
|
|
|
var environments = listRancherEnvironments(rancherClient)
|
|
|
|
var services = listRancherServices(rancherClient)
|
|
|
|
var container = listRancherContainer(rancherClient)
|
|
|
|
|
|
|
|
var rancherData = parseRancherData(environments, services, container)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-02-05 21:54:24 +00:00
|
|
|
log.Errorf("Failed to create a client for rancher, error: %s", err)
|
2017-01-28 23:01:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configuration := provider.loadRancherConfig(rancherData)
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "rancher",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
|
2017-02-06 15:28:12 +00:00
|
|
|
if provider.Watch {
|
|
|
|
_, cancel := context.WithCancel(ctx)
|
|
|
|
ticker := time.NewTicker(RancherDefaultWatchTime)
|
|
|
|
pool.Go(func(stop chan bool) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
|
|
|
|
log.Debugf("Refreshing new Data from Rancher API")
|
|
|
|
var environments = listRancherEnvironments(rancherClient)
|
|
|
|
var services = listRancherServices(rancherClient)
|
|
|
|
var container = listRancherContainer(rancherClient)
|
|
|
|
|
|
|
|
rancherData := parseRancherData(environments, services, container)
|
|
|
|
|
|
|
|
configuration := provider.loadRancherConfig(rancherData)
|
|
|
|
if configuration != nil {
|
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "rancher",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-stop:
|
|
|
|
ticker.Stop()
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-28 23:01:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
notify := func(err error, time time.Duration) {
|
|
|
|
log.Errorf("Rancher 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 Rancher Endpoint %+v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
func listRancherEnvironments(client *rancher.RancherClient) []*rancher.Environment {
|
2017-01-28 23:01:56 +00:00
|
|
|
|
|
|
|
var environmentList = []*rancher.Environment{}
|
|
|
|
|
|
|
|
environments, err := client.Environment.List(nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot get Rancher Environments %+v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-06 15:30:21 +00:00
|
|
|
for k := range environments.Data {
|
2017-01-28 23:01:56 +00:00
|
|
|
environmentList = append(environmentList, &environments.Data[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
return environmentList
|
|
|
|
}
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
func listRancherServices(client *rancher.RancherClient) []*rancher.Service {
|
2017-01-28 23:01:56 +00:00
|
|
|
|
|
|
|
var servicesList = []*rancher.Service{}
|
|
|
|
|
|
|
|
services, err := client.Service.List(nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot get Rancher Services %+v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-06 15:30:21 +00:00
|
|
|
for k := range services.Data {
|
2017-01-28 23:01:56 +00:00
|
|
|
servicesList = append(servicesList, &services.Data[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
return servicesList
|
|
|
|
}
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
func listRancherContainer(client *rancher.RancherClient) []*rancher.Container {
|
2017-01-28 23:01:56 +00:00
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
containerList := []*rancher.Container{}
|
2017-01-28 23:01:56 +00:00
|
|
|
|
|
|
|
container, err := client.Container.List(nil)
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
log.Debugf("first container len: %i", len(container.Data))
|
|
|
|
|
2017-01-28 23:01:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot get Rancher Services %+v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
valid := true
|
|
|
|
|
|
|
|
for valid {
|
2017-02-06 15:28:12 +00:00
|
|
|
for k := range container.Data {
|
2017-02-05 21:54:24 +00:00
|
|
|
containerList = append(containerList, &container.Data[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err = container.Next()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if container == nil || len(container.Data) == 0 {
|
|
|
|
valid = false
|
|
|
|
}
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return containerList
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRancherData(environments []*rancher.Environment, services []*rancher.Service, containers []*rancher.Container) []rancherData {
|
|
|
|
var rancherDataList []rancherData
|
|
|
|
|
|
|
|
for _, environment := range environments {
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
if service.EnvironmentId != environment.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rancherData := rancherData{
|
2017-02-05 21:54:24 +00:00
|
|
|
Name: environment.Name + "/" + service.Name,
|
|
|
|
Health: service.HealthState,
|
|
|
|
Labels: make(map[string]string),
|
|
|
|
Containers: []string{},
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range service.LaunchConfig.Labels {
|
|
|
|
rancherData.Labels[key] = value.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, container := range containers {
|
|
|
|
for key, value := range container.Labels {
|
|
|
|
|
|
|
|
if key == "io.rancher.stack_service.name" && value == rancherData.Name {
|
|
|
|
rancherData.Containers = append(rancherData.Containers, container.PrimaryIpAddress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rancherDataList = append(rancherDataList, rancherData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rancherDataList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) loadRancherConfig(services []rancherData) *types.Configuration {
|
|
|
|
|
|
|
|
var RancherFuncMap = template.FuncMap{
|
|
|
|
"getPort": provider.getPort,
|
|
|
|
"getBackend": provider.getBackend,
|
|
|
|
"getWeight": provider.getWeight,
|
|
|
|
"getDomain": provider.getDomain,
|
|
|
|
"getProtocol": provider.getProtocol,
|
|
|
|
"getPassHostHeader": provider.getPassHostHeader,
|
|
|
|
"getPriority": provider.getPriority,
|
|
|
|
"getEntryPoints": provider.getEntryPoints,
|
|
|
|
"getFrontendRule": provider.getFrontendRule,
|
|
|
|
"hasCircuitBreakerLabel": provider.hasCircuitBreakerLabel,
|
|
|
|
"getCircuitBreakerExpression": provider.getCircuitBreakerExpression,
|
|
|
|
"hasLoadBalancerLabel": provider.hasLoadBalancerLabel,
|
|
|
|
"getLoadBalancerMethod": provider.getLoadBalancerMethod,
|
|
|
|
"hasMaxConnLabels": provider.hasMaxConnLabels,
|
|
|
|
"getMaxConnAmount": provider.getMaxConnAmount,
|
|
|
|
"getMaxConnExtractorFunc": provider.getMaxConnExtractorFunc,
|
|
|
|
"getSticky": provider.getSticky,
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter services
|
|
|
|
filteredServices := fun.Filter(func(service rancherData) bool {
|
|
|
|
return provider.serviceFilter(service)
|
|
|
|
}, services).([]rancherData)
|
|
|
|
|
|
|
|
frontends := map[string]rancherData{}
|
|
|
|
backends := map[string]rancherData{}
|
|
|
|
|
|
|
|
for _, service := range filteredServices {
|
|
|
|
frontendName := provider.getFrontendName(service)
|
|
|
|
frontends[frontendName] = service
|
|
|
|
backendName := provider.getBackend(service)
|
|
|
|
backends[backendName] = service
|
|
|
|
}
|
|
|
|
|
|
|
|
templateObjects := struct {
|
2017-02-05 21:54:24 +00:00
|
|
|
Frontends map[string]rancherData
|
|
|
|
Backends map[string]rancherData
|
|
|
|
Domain string
|
2017-01-28 23:01:56 +00:00
|
|
|
}{
|
|
|
|
frontends,
|
|
|
|
backends,
|
|
|
|
provider.Domain,
|
|
|
|
}
|
|
|
|
|
|
|
|
configuration, err := provider.getConfiguration("templates/rancher.tmpl", RancherFuncMap, templateObjects)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return configuration
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *Rancher) serviceFilter(service rancherData) bool {
|
|
|
|
|
|
|
|
if service.Labels["traefik.port"] == "" {
|
|
|
|
log.Debugf("Filtering service %s without traefik.port label", service.Name)
|
2017-02-05 21:54:24 +00:00
|
|
|
return false
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isServiceEnabled(service, provider.ExposedByDefault) {
|
|
|
|
log.Debugf("Filtering disabled service %s", service.Name)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if service.Health != "" && service.Health != "healthy" {
|
|
|
|
log.Debugf("Filtering unhealthy or starting service %s", service.Name)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func isServiceEnabled(service rancherData, exposedByDefault bool) bool {
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
if service.Labels["traefik.enable"] != "" {
|
2017-01-28 23:01:56 +00:00
|
|
|
var v = service.Labels["traefik.enable"]
|
|
|
|
return exposedByDefault && v != "false" || v == "true"
|
|
|
|
}
|
2017-02-05 23:58:05 +00:00
|
|
|
return exposedByDefault
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|