docker override port, host, backend

reuse loadbalancer
This commit is contained in:
emile 2015-09-09 16:49:51 +02:00
parent 0af872e661
commit 730fbfe9c5
5 changed files with 76 additions and 45 deletions

View file

@ -5,8 +5,31 @@ import (
"bytes"
"github.com/BurntSushi/toml"
"log"
"text/template"
"strings"
)
var DockerFuncMap = template.FuncMap{
"getBackend": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.backend") {
return value
}
}
return container.Config.Hostname
},
"getPort": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.port") {
return value
}
}
for key, _ := range container.NetworkSettings.Ports {
return key.Port()
}
return ""
},
"getHost": getHost,
}
type DockerProvider struct {
Watch bool
Endpoint string
@ -38,16 +61,22 @@ func (provider *DockerProvider) loadDockerConfig() *Configuration {
configuration := new(Configuration)
containerList, _ := provider.dockerClient.ListContainers(docker.ListContainersOptions{})
containersInspected := []docker.Container{}
hosts := map[string][]docker.Container{}
for _, container := range containerList {
containerInspected, _ := provider.dockerClient.InspectContainer(container.ID)
containersInspected = append(containersInspected, *containerInspected)
hosts[getHost(*containerInspected)] = append(hosts[getHost(*containerInspected)], *containerInspected)
}
containers := struct {
templateObjects := struct {
Containers []docker.Container
Hosts map[string][]docker.Container
}{
containersInspected,
hosts,
}
tmpl, err := gtf.New("docker.tmpl").ParseFiles("docker.tmpl")
gtf.Inject(DockerFuncMap)
tmpl, err := template.New("docker.tmpl").Funcs(DockerFuncMap).ParseFiles("docker.tmpl")
if err != nil {
log.Println("Error reading file:", err)
return nil
@ -55,7 +84,7 @@ func (provider *DockerProvider) loadDockerConfig() *Configuration {
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, containers)
err = tmpl.Execute(&buffer, templateObjects)
if err != nil {
log.Println("Error with docker template:", err)
return nil
@ -66,4 +95,13 @@ func (provider *DockerProvider) loadDockerConfig() *Configuration {
return nil
}
return configuration
}
func getHost(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.host") {
return value
}
}
return strings.TrimPrefix(container.Name, "/")
}