2015-11-01 15:35:01 +00:00
|
|
|
package provider
|
2015-09-12 13:10:03 +00:00
|
|
|
|
2015-09-07 22:15:14 +00:00
|
|
|
import (
|
2015-09-07 08:38:58 +00:00
|
|
|
"bytes"
|
2015-09-15 20:32:09 +00:00
|
|
|
"errors"
|
2015-11-01 15:35:01 +00:00
|
|
|
"fmt"
|
2015-09-24 15:16:13 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
2015-09-07 08:38:58 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2015-09-10 20:54:37 +00:00
|
|
|
"github.com/BurntSushi/ty/fun"
|
2015-09-24 12:32:37 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-09-15 20:32:09 +00:00
|
|
|
"github.com/cenkalti/backoff"
|
2015-11-01 15:35:01 +00:00
|
|
|
"github.com/emilevauge/traefik/autogen"
|
|
|
|
"github.com/emilevauge/traefik/types"
|
2015-09-12 13:10:03 +00:00
|
|
|
"github.com/fsouza/go-dockerclient"
|
2015-09-07 08:38:58 +00:00
|
|
|
)
|
2015-09-09 20:39:08 +00:00
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// Docker holds configurations of the Docker provider.
|
2015-11-02 18:48:34 +00:00
|
|
|
type Docker struct {
|
2015-09-15 20:32:09 +00:00
|
|
|
Watch bool
|
|
|
|
Endpoint string
|
|
|
|
Filename string
|
|
|
|
Domain string
|
2015-09-09 20:39:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// Provide allows the provider to provide configurations to traefik
|
|
|
|
// using the given configuration channel.
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage) error {
|
2015-11-01 18:29:47 +00:00
|
|
|
|
|
|
|
dockerClient, err := docker.NewClient(provider.Endpoint)
|
|
|
|
if err != nil {
|
2015-10-01 10:04:25 +00:00
|
|
|
log.Errorf("Failed to create a client for docker, error: %s", err)
|
|
|
|
return err
|
2015-11-01 18:29:47 +00:00
|
|
|
}
|
|
|
|
err = dockerClient.Ping()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Docker connection error %+v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("Docker connection established")
|
|
|
|
if provider.Watch {
|
|
|
|
dockerEvents := make(chan *docker.APIEvents)
|
|
|
|
dockerClient.AddEventListener(dockerEvents)
|
|
|
|
log.Debug("Docker listening")
|
|
|
|
go func() {
|
|
|
|
operation := func() error {
|
|
|
|
for {
|
|
|
|
event := <-dockerEvents
|
|
|
|
if event == nil {
|
|
|
|
return errors.New("Docker event nil")
|
|
|
|
// log.Fatalf("Docker connection error")
|
|
|
|
}
|
|
|
|
if event.Status == "start" || event.Status == "die" {
|
|
|
|
log.Debugf("Docker event receveived %+v", event)
|
|
|
|
configuration := provider.loadDockerConfig(dockerClient)
|
|
|
|
if configuration != nil {
|
|
|
|
configurationChan <- types.ConfigMessage{"docker", configuration}
|
2015-09-10 20:54:37 +00:00
|
|
|
}
|
2015-09-10 07:06:37 +00:00
|
|
|
}
|
2015-09-09 20:39:08 +00:00
|
|
|
}
|
2015-11-01 18:29:47 +00:00
|
|
|
}
|
|
|
|
notify := func(err error, time time.Duration) {
|
|
|
|
log.Errorf("Docker connection error %+v, retrying in %s", err, time)
|
|
|
|
}
|
|
|
|
err := backoff.RetryNotify(operation, backoff.NewExponentialBackOff(), notify)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot connect to docker server %+v", err)
|
|
|
|
}
|
|
|
|
}()
|
2015-09-09 20:39:08 +00:00
|
|
|
}
|
2015-11-01 18:29:47 +00:00
|
|
|
|
|
|
|
configuration := provider.loadDockerConfig(dockerClient)
|
|
|
|
configurationChan <- types.ConfigMessage{"docker", configuration}
|
2015-10-01 10:04:25 +00:00
|
|
|
return nil
|
2015-09-07 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Docker) loadDockerConfig(dockerClient *docker.Client) *types.Configuration {
|
2015-10-08 19:21:51 +00:00
|
|
|
var DockerFuncMap = template.FuncMap{
|
|
|
|
"getBackend": func(container docker.Container) string {
|
2015-10-23 07:49:19 +00:00
|
|
|
if label, err := provider.getLabel(container, "traefik.backend"); err == nil {
|
|
|
|
return label
|
2015-10-08 19:21:51 +00:00
|
|
|
}
|
2015-10-23 07:49:19 +00:00
|
|
|
return provider.getEscapedName(container.Name)
|
2015-10-08 19:21:51 +00:00
|
|
|
},
|
|
|
|
"getPort": func(container docker.Container) string {
|
2015-10-23 07:49:19 +00:00
|
|
|
if label, err := provider.getLabel(container, "traefik.port"); err == nil {
|
|
|
|
return label
|
2015-10-08 19:21:51 +00:00
|
|
|
}
|
|
|
|
for key := range container.NetworkSettings.Ports {
|
|
|
|
return key.Port()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
"getWeight": func(container docker.Container) string {
|
2015-10-23 07:49:19 +00:00
|
|
|
if label, err := provider.getLabel(container, "traefik.weight"); err == nil {
|
|
|
|
return label
|
2015-10-08 19:21:51 +00:00
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
},
|
|
|
|
"getDomain": func(container docker.Container) string {
|
2015-10-23 07:49:19 +00:00
|
|
|
if label, err := provider.getLabel(container, "traefik.domain"); err == nil {
|
|
|
|
return label
|
2015-10-08 19:21:51 +00:00
|
|
|
}
|
|
|
|
return provider.Domain
|
|
|
|
},
|
2015-10-23 16:59:08 +00:00
|
|
|
"getProtocol": func(container docker.Container) string {
|
|
|
|
if label, err := provider.getLabel(container, "traefik.protocol"); err == nil {
|
2015-10-23 07:49:19 +00:00
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "http"
|
|
|
|
},
|
2015-10-30 10:33:41 +00:00
|
|
|
"getPassHostHeader": func(container docker.Container) string {
|
|
|
|
if passHostHeader, err := provider.getLabel(container, "traefik.frontend.passHostHeader"); err == nil {
|
|
|
|
return passHostHeader
|
|
|
|
}
|
|
|
|
return "false"
|
|
|
|
},
|
2015-10-23 07:49:19 +00:00
|
|
|
"getFrontendValue": provider.GetFrontendValue,
|
|
|
|
"getFrontendRule": provider.GetFrontendRule,
|
2015-10-08 19:21:51 +00:00
|
|
|
"replace": func(s1 string, s2 string, s3 string) string {
|
|
|
|
return strings.Replace(s3, s1, s2, -1)
|
|
|
|
},
|
|
|
|
}
|
2015-11-01 15:35:01 +00:00
|
|
|
configuration := new(types.Configuration)
|
2015-09-12 17:22:44 +00:00
|
|
|
containerList, _ := dockerClient.ListContainers(docker.ListContainersOptions{})
|
2015-09-07 08:38:58 +00:00
|
|
|
containersInspected := []docker.Container{}
|
2015-10-23 07:49:19 +00:00
|
|
|
frontends := map[string][]docker.Container{}
|
2015-09-10 20:54:37 +00:00
|
|
|
|
|
|
|
// get inspect containers
|
2015-09-07 08:38:58 +00:00
|
|
|
for _, container := range containerList {
|
2015-09-12 17:22:44 +00:00
|
|
|
containerInspected, _ := dockerClient.InspectContainer(container.ID)
|
2015-09-10 20:54:37 +00:00
|
|
|
containersInspected = append(containersInspected, *containerInspected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter containers
|
|
|
|
filteredContainers := fun.Filter(func(container docker.Container) bool {
|
2015-09-12 13:10:03 +00:00
|
|
|
if len(container.NetworkSettings.Ports) == 0 {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Debugf("Filtering container without port %s", container.Name)
|
2015-09-10 20:54:37 +00:00
|
|
|
return false
|
2015-09-10 15:31:52 +00:00
|
|
|
}
|
2015-09-10 20:54:37 +00:00
|
|
|
_, err := strconv.Atoi(container.Config.Labels["traefik.port"])
|
2015-09-12 13:10:03 +00:00
|
|
|
if len(container.NetworkSettings.Ports) > 1 && err != nil {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Debugf("Filtering container with more than 1 port and no traefik.port label %s", container.Name)
|
2015-09-10 20:54:37 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-09-12 13:10:03 +00:00
|
|
|
if container.Config.Labels["traefik.enable"] == "false" {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Debugf("Filtering disabled container %s", container.Name)
|
2015-09-10 20:54:37 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-10-23 07:49:19 +00:00
|
|
|
|
2015-11-05 14:14:25 +00:00
|
|
|
labels, err := provider.getLabels(container, []string{"traefik.frontend.rule", "traefik.frontend.value"})
|
|
|
|
if len(labels) != 0 && err != nil {
|
2015-10-23 07:49:19 +00:00
|
|
|
log.Debugf("Filtering bad labeled container %s", container.Name)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-10 20:54:37 +00:00
|
|
|
return true
|
|
|
|
}, containersInspected).([]docker.Container)
|
|
|
|
|
|
|
|
for _, container := range filteredContainers {
|
2015-10-23 07:49:19 +00:00
|
|
|
frontends[provider.getFrontendName(container)] = append(frontends[provider.getFrontendName(container)], container)
|
2015-09-07 08:38:58 +00:00
|
|
|
}
|
2015-09-09 14:49:51 +00:00
|
|
|
|
|
|
|
templateObjects := struct {
|
2015-09-07 08:38:58 +00:00
|
|
|
Containers []docker.Container
|
2015-10-23 07:49:19 +00:00
|
|
|
Frontends map[string][]docker.Container
|
2015-09-09 15:50:02 +00:00
|
|
|
Domain string
|
2015-09-07 08:38:58 +00:00
|
|
|
}{
|
2015-09-10 20:54:37 +00:00
|
|
|
filteredContainers,
|
2015-10-23 07:49:19 +00:00
|
|
|
frontends,
|
2015-09-09 15:10:43 +00:00
|
|
|
provider.Domain,
|
2015-09-07 08:38:58 +00:00
|
|
|
}
|
2015-09-11 17:25:49 +00:00
|
|
|
tmpl := template.New(provider.Filename).Funcs(DockerFuncMap)
|
2015-09-12 13:10:03 +00:00
|
|
|
if len(provider.Filename) > 0 {
|
2015-09-11 17:25:49 +00:00
|
|
|
_, err := tmpl.ParseFiles(provider.Filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error reading file", err)
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-12 13:10:03 +00:00
|
|
|
} else {
|
2015-11-01 15:35:01 +00:00
|
|
|
buf, err := autogen.Asset("templates/docker.tmpl")
|
2015-09-11 17:25:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error reading file", err)
|
|
|
|
}
|
|
|
|
_, err = tmpl.Parse(string(buf))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error reading file", err)
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-07 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
2015-09-11 17:25:49 +00:00
|
|
|
err := tmpl.Execute(&buffer, templateObjects)
|
2015-09-07 08:38:58 +00:00
|
|
|
if err != nil {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Error with docker template", err)
|
2015-09-07 13:25:13 +00:00
|
|
|
return nil
|
2015-09-07 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 22:15:14 +00:00
|
|
|
if _, err := toml.Decode(buffer.String(), configuration); err != nil {
|
2015-10-23 07:49:19 +00:00
|
|
|
log.Error("Error creating docker configuration ", err)
|
2015-09-07 08:38:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-09-07 22:15:14 +00:00
|
|
|
return configuration
|
2015-09-09 14:49:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Docker) getFrontendName(container docker.Container) string {
|
2015-10-23 07:49:19 +00:00
|
|
|
// Replace '.' with '-' in quoted keys because of this issue https://github.com/BurntSushi/toml/issues/78
|
2015-10-26 23:26:35 +00:00
|
|
|
frontendName := fmt.Sprintf("%s-%s", provider.GetFrontendRule(container), provider.GetFrontendValue(container))
|
2015-11-13 00:04:19 +00:00
|
|
|
frontendName = strings.Replace(frontendName, "[", "", -1)
|
|
|
|
frontendName = strings.Replace(frontendName, "]", "", -1)
|
|
|
|
|
2015-10-26 23:26:35 +00:00
|
|
|
return strings.Replace(frontendName, ".", "-", -1)
|
2015-10-23 07:49:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Docker) getEscapedName(name string) string {
|
2015-10-23 07:49:19 +00:00
|
|
|
return strings.Replace(name, "/", "", -1)
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Docker) getLabel(container docker.Container, label string) (string, error) {
|
2015-09-09 14:49:51 +00:00
|
|
|
for key, value := range container.Config.Labels {
|
2015-10-23 07:49:19 +00:00
|
|
|
if key == label {
|
|
|
|
return value, nil
|
2015-09-09 14:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-23 07:49:19 +00:00
|
|
|
return "", errors.New("Label not found:" + label)
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Docker) getLabels(container docker.Container, labels []string) (map[string]string, error) {
|
2015-11-05 14:14:25 +00:00
|
|
|
var globalErr error
|
2015-10-26 23:26:35 +00:00
|
|
|
foundLabels := map[string]string{}
|
|
|
|
for _, label := range labels {
|
2015-11-01 18:29:47 +00:00
|
|
|
foundLabel, err := provider.getLabel(container, label)
|
2015-11-05 14:14:25 +00:00
|
|
|
// Error out only if one of them is defined.
|
2015-11-01 18:29:47 +00:00
|
|
|
if err != nil {
|
2015-11-05 14:14:25 +00:00
|
|
|
globalErr = errors.New("Label not found: " + label)
|
|
|
|
continue
|
2015-10-26 23:26:35 +00:00
|
|
|
}
|
2015-11-01 18:29:47 +00:00
|
|
|
foundLabels[label] = foundLabel
|
2015-11-05 14:14:25 +00:00
|
|
|
|
2015-10-26 23:26:35 +00:00
|
|
|
}
|
2015-11-05 14:14:25 +00:00
|
|
|
return foundLabels, globalErr
|
2015-10-26 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// GetFrontendValue returns the frontend value for the specified container, using
|
|
|
|
// it's label. It returns a default one if the label is not present.
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Docker) GetFrontendValue(container docker.Container) string {
|
2015-10-23 07:49:19 +00:00
|
|
|
if label, err := provider.getLabel(container, "traefik.frontend.value"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
return provider.getEscapedName(container.Name) + "." + provider.Domain
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// GetFrontendRule returns the frontend rule for the specified container, using
|
|
|
|
// it's label. It returns a default one (Host) if the label is not present.
|
2015-11-02 18:48:34 +00:00
|
|
|
func (provider *Docker) GetFrontendRule(container docker.Container) string {
|
2015-10-23 07:49:19 +00:00
|
|
|
if label, err := provider.getLabel(container, "traefik.frontend.rule"); err == nil {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
return "Host"
|
2015-09-12 13:10:03 +00:00
|
|
|
}
|