Code review corrections

This commit is contained in:
emile 2015-10-27 00:26:35 +01:00
parent aaeb7cdffd
commit d390f86de2
3 changed files with 28 additions and 16 deletions

View file

@ -8,6 +8,7 @@ import (
"text/template"
"time"
"fmt"
"github.com/BurntSushi/toml"
"github.com/BurntSushi/ty/fun"
log "github.com/Sirupsen/logrus"
@ -137,10 +138,8 @@ func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *C
log.Debugf("Filtering disabled container %s", container.Name)
return false
}
_, errRule := provider.getLabel(container, "traefik.frontend.rule")
_, errValue := provider.getLabel(container, "traefik.frontend.value")
if errRule != nil && errValue == nil || errRule == nil && errValue != nil {
if _, err := provider.getLabels(container, []string{"traefik.frontend.rule", "traefik.frontend.value"}); err != nil {
log.Debugf("Filtering bad labeled container %s", container.Name)
return false
}
@ -196,7 +195,8 @@ func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *C
func (provider *DockerProvider) getFrontendName(container docker.Container) string {
// Replace '.' with '-' in quoted keys because of this issue https://github.com/BurntSushi/toml/issues/78
return strings.Replace(provider.GetFrontendRule(container)+"-"+provider.GetFrontendValue(container), ".", "-", -1)
frontendName := fmt.Sprintf("%s-%s", provider.GetFrontendRule(container), provider.GetFrontendValue(container))
return strings.Replace(frontendName, ".", "-", -1)
}
func (provider *DockerProvider) getEscapedName(name string) string {
@ -212,6 +212,18 @@ func (provider *DockerProvider) getLabel(container docker.Container, label strin
return "", errors.New("Label not found:" + label)
}
func (provider *DockerProvider) getLabels(container docker.Container, labels []string) (map[string]string, error) {
foundLabels := map[string]string{}
for _, label := range labels {
if foundLabel, err := provider.getLabel(container, label); err != nil {
return nil, errors.New("Label not found: " + label)
} else {
foundLabels[label] = foundLabel
}
}
return foundLabels, nil
}
func (provider *DockerProvider) GetFrontendValue(container docker.Container) string {
if label, err := provider.getLabel(container, "traefik.frontend.value"); err == nil {
return label

View file

@ -64,12 +64,12 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration {
return ""
},
"getWeight": func(task marathon.Task, applications []marathon.Application) string {
application := getApplication(task, applications)
if application == nil {
application, errApp := getApplication(task, applications)
if errApp != nil {
log.Errorf("Unable to get marathon application from task %s", task.AppID)
return "0"
}
if label, err := provider.getLabel(*application, "traefik.weight"); err == nil {
if label, err := provider.getLabel(application, "traefik.weight"); err == nil {
return label
}
return "0"
@ -84,12 +84,12 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration {
return strings.Replace(s3, s1, s2, -1)
},
"getProtocol": func(task marathon.Task, applications []marathon.Application) string {
application := getApplication(task, applications)
if application == nil {
application, errApp := getApplication(task, applications)
if errApp != nil {
log.Errorf("Unable to get marathon application from task %s", task.AppID)
return "http"
}
if label, err := provider.getLabel(*application, "traefik.protocol"); err == nil {
if label, err := provider.getLabel(application, "traefik.protocol"); err == nil {
return label
}
return "http"
@ -117,8 +117,8 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration {
log.Debug("Filtering marathon task without port", task.AppID)
return false
}
application := getApplication(task, applications.Apps)
if application == nil {
application, errApp := getApplication(task, applications.Apps)
if errApp != nil {
log.Errorf("Unable to get marathon application from task %s", task.AppID)
return false
}
@ -193,13 +193,13 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration {
return configuration
}
func getApplication(task marathon.Task, apps []marathon.Application) *marathon.Application {
func getApplication(task marathon.Task, apps []marathon.Application) (marathon.Application, error) {
for _, application := range apps {
if application.ID == task.AppID {
return &application
return application, nil
}
}
return nil
return marathon.Application{}, errors.New("Application not found: " + task.AppID)
}
func (provider *MarathonProvider) getLabel(application marathon.Application, label string) (string, error) {

View file

@ -133,7 +133,7 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
connBackend, resp, err := dialer.Dial(backendURL.String(), nil)
if err != nil {
log.Errorf("Websocketproxy: couldn't dial to remote backend url %s, %s, %+v", backendURL.String(), err, resp)
http.NotFound(rw, req)
http.Error(rw, "Remote backend unreachable", http.StatusBadGateway)
return
}
defer connBackend.Close()