Merge pull request #52 from EmileVauge/emilevauge-add-traefik-domain

Add traefik.domain label #51
This commit is contained in:
Vincent Demeester 2015-10-09 23:40:31 +02:00
commit adca5dc55b
6 changed files with 89 additions and 73 deletions

View file

@ -22,40 +22,6 @@ type DockerProvider struct {
Domain string Domain string
} }
var DockerFuncMap = template.FuncMap{
"getBackend": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if key == "traefik.backend" {
return value
}
}
return getHost(container)
},
"getPort": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if key == "traefik.port" {
return value
}
}
for key := range container.NetworkSettings.Ports {
return key.Port()
}
return ""
},
"getWeight": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if key == "traefik.weight" {
return value
}
}
return "0"
},
"replace": func(s1 string, s2 string, s3 string) string {
return strings.Replace(s3, s1, s2, -1)
},
"getHost": getHost,
}
func (provider *DockerProvider) Provide(configurationChan chan<- configMessage) error { func (provider *DockerProvider) Provide(configurationChan chan<- configMessage) error {
if dockerClient, err := docker.NewClient(provider.Endpoint); err != nil { if dockerClient, err := docker.NewClient(provider.Endpoint); err != nil {
log.Errorf("Failed to create a client for docker, error: %s", err) log.Errorf("Failed to create a client for docker, error: %s", err)
@ -105,6 +71,47 @@ func (provider *DockerProvider) Provide(configurationChan chan<- configMessage)
} }
func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *Configuration { func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *Configuration {
var DockerFuncMap = template.FuncMap{
"getBackend": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if key == "traefik.backend" {
return value
}
}
return getHost(container)
},
"getPort": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if key == "traefik.port" {
return value
}
}
for key := range container.NetworkSettings.Ports {
return key.Port()
}
return ""
},
"getWeight": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if key == "traefik.weight" {
return value
}
}
return "0"
},
"getDomain": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if key == "traefik.domain" {
return value
}
}
return provider.Domain
},
"replace": func(s1 string, s2 string, s3 string) string {
return strings.Replace(s3, s1, s2, -1)
},
"getHost": getHost,
}
configuration := new(Configuration) configuration := new(Configuration)
containerList, _ := dockerClient.ListContainers(docker.ListContainersOptions{}) containerList, _ := dockerClient.ListContainers(docker.ListContainersOptions{})
containersInspected := []docker.Container{} containersInspected := []docker.Container{}

View file

@ -402,6 +402,7 @@ Labels can be used on containers to override default behaviour:
* `traefik.weight=10`: assign this weight to the container * `traefik.weight=10`: assign this weight to the container
* `traefik.enable=false`: disable this container in Træfɪk * `traefik.enable=false`: disable this container in Træfɪk
* `traefik.host=bar`: override the default routing from {containerName}.{domain} to bar.{domain} * `traefik.host=bar`: override the default routing from {containerName}.{domain} to bar.{domain}
* `traefik.domain=traefik.localhost`: override the default domain
## <a id="marathon"></a> Marathon backend ## <a id="marathon"></a> Marathon backend
@ -461,6 +462,7 @@ Labels can be used on containers to override default behaviour:
* `traefik.enable=false`: disable this application in Træfɪk * `traefik.enable=false`: disable this application in Træfɪk
* `traefik.host=bar`: override the default routing from {appName}.{domain} to bar.{domain} * `traefik.host=bar`: override the default routing from {appName}.{domain} to bar.{domain}
* `traefik.prefixes=pf1,pf2`: use PathPrefix(es) instead of hostname for routing, use filename="providerTemplates/marathon-prefix.tmpl" with this option * `traefik.prefixes=pf1,pf2`: use PathPrefix(es) instead of hostname for routing, use filename="providerTemplates/marathon-prefix.tmpl" with this option
* `traefik.domain=traefik.localhost`: override the default domain
## <a id="consul"></a> Consul backend ## <a id="consul"></a> Consul backend

View file

@ -21,42 +21,6 @@ type MarathonProvider struct {
NetworkInterface string NetworkInterface string
} }
var MarathonFuncMap = template.FuncMap{
"getPort": func(task marathon.Task) string {
for _, port := range task.Ports {
return strconv.Itoa(port)
}
return ""
},
"getHost": func(application marathon.Application) string {
for key, value := range application.Labels {
if key == "traefik.host" {
return value
}
}
return strings.Replace(application.ID, "/", "", 1)
},
"getWeight": func(application marathon.Application) string {
for key, value := range application.Labels {
if key == "traefik.weight" {
return value
}
}
return "0"
},
"getPrefixes": func(application marathon.Application) ([]string, error) {
for key, value := range application.Labels {
if key == "traefik.prefixes" {
return strings.Split(value, ","), nil
}
}
return []string{}, nil
},
"replace": func(s1 string, s2 string, s3 string) string {
return strings.Replace(s3, s1, s2, -1)
},
}
func (provider *MarathonProvider) Provide(configurationChan chan<- configMessage) error { func (provider *MarathonProvider) Provide(configurationChan chan<- configMessage) error {
config := marathon.NewDefaultConfig() config := marathon.NewDefaultConfig()
config.URL = provider.Endpoint config.URL = provider.Endpoint
@ -91,6 +55,49 @@ func (provider *MarathonProvider) Provide(configurationChan chan<- configMessage
} }
func (provider *MarathonProvider) loadMarathonConfig() *Configuration { func (provider *MarathonProvider) loadMarathonConfig() *Configuration {
var MarathonFuncMap = template.FuncMap{
"getPort": func(task marathon.Task) string {
for _, port := range task.Ports {
return strconv.Itoa(port)
}
return ""
},
"getHost": func(application marathon.Application) string {
for key, value := range application.Labels {
if key == "traefik.host" {
return value
}
}
return strings.Replace(application.ID, "/", "", 1)
},
"getWeight": func(application marathon.Application) string {
for key, value := range application.Labels {
if key == "traefik.weight" {
return value
}
}
return "0"
},
"getDomain": func(application marathon.Application) string {
for key, value := range application.Labels {
if key == "traefik.domain" {
return value
}
}
return provider.Domain
},
"getPrefixes": func(application marathon.Application) ([]string, error) {
for key, value := range application.Labels {
if key == "traefik.prefixes" {
return strings.Split(value, ","), nil
}
}
return []string{}, nil
},
"replace": func(s1 string, s2 string, s3 string) string {
return strings.Replace(s3, s1, s2, -1)
},
}
configuration := new(Configuration) configuration := new(Configuration)
applications, err := provider.marathonClient.Applications(nil) applications, err := provider.marathonClient.Applications(nil)

View file

@ -10,5 +10,5 @@
backend = "backend-{{getBackend $container}}" backend = "backend-{{getBackend $container}}"
[frontends.frontend-{{$host}}.routes.route-host-{{$host}}] [frontends.frontend-{{$host}}.routes.route-host-{{$host}}]
rule = "Host" rule = "Host"
value = "{{$host}}.{{$.Domain}}" value = "{{$host}}.{{getDomain $container}}"
{{end}} {{end}}

View file

@ -22,6 +22,6 @@
backend = "backend{{.ID | replace "/" "-"}}" backend = "backend{{.ID | replace "/" "-"}}"
[frontends.frontend-{{getHost $app | replace "/" "-"}}.routes.route-host-{{getHost $app | replace "/" "-"}}] [frontends.frontend-{{getHost $app | replace "/" "-"}}.routes.route-host-{{getHost $app | replace "/" "-"}}]
rule = "Host" rule = "Host"
value = "{{getHost $app | replace "/" "-"}}.{{$.Domain}}" value = "{{getHost $app | replace "/" "-"}}.{{getDomain .}}"
{{end}} {{end}}
{{end}} {{end}}

View file

@ -15,5 +15,5 @@
backend = "backend{{.ID | replace "/" "-"}}" backend = "backend{{.ID | replace "/" "-"}}"
[frontends.frontend-{{getHost . | replace "/" "-"}}.routes.route-host-{{getHost . | replace "/" "-"}}] [frontends.frontend-{{getHost . | replace "/" "-"}}.routes.route-host-{{getHost . | replace "/" "-"}}]
rule = "Host" rule = "Host"
value = "{{getHost . | replace "/" "-"}}.{{$.Domain}}" value = "{{getHost . | replace "/" "-"}}.{{getDomain .}}"
{{end}} {{end}}