Fix Labels/annotation logs and values.

This commit is contained in:
Ludovic Fernandez 2017-11-30 09:26:03 +01:00 committed by Traefiker
parent 077b39d7c6
commit f084d2a28b
6 changed files with 27 additions and 37 deletions

View file

@ -121,7 +121,6 @@ func (p Provider) createClient() (client.APIClient, error) {
} }
return client.NewClient(p.Endpoint, apiVersion, httpClient, httpHeaders) return client.NewClient(p.Endpoint, apiVersion, httpClient, httpHeaders)
} }
// Provide allows the docker provider to provide configurations to traefik // Provide allows the docker provider to provide configurations to traefik

View file

@ -2,12 +2,10 @@ package docker
import ( import (
"fmt" "fmt"
"math"
"strconv" "strconv"
"strings" "strings"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/containous/traefik/provider"
"github.com/containous/traefik/types" "github.com/containous/traefik/types"
) )
@ -26,13 +24,12 @@ type labelServiceProperties map[string]map[string]string
func getFuncInt64Label(labelName string, defaultValue int64) func(container dockerData) int64 { func getFuncInt64Label(labelName string, defaultValue int64) func(container dockerData) int64 {
return func(container dockerData) int64 { return func(container dockerData) int64 {
if label, err := getLabel(container, labelName); err == nil { if rawValue, err := getLabel(container, labelName); err == nil {
i, errConv := strconv.ParseInt(label, 10, 64) value, errConv := strconv.ParseInt(rawValue, 10, 64)
if errConv != nil { if errConv == nil {
log.Errorf("Unable to parse traefik.backend.maxconn.amount %s", label) return value
return math.MaxInt64
} }
return i log.Errorf("Unable to parse %q: %q", labelName, rawValue)
} }
return defaultValue return defaultValue
} }
@ -96,7 +93,7 @@ func getSliceStringLabel(container dockerData, labelName string) []string {
var value []string var value []string
if label, err := getLabel(container, labelName); err == nil { if label, err := getLabel(container, labelName); err == nil {
value = provider.SplitAndTrimString(label) value = types.SplitAndTrimString(label)
} }
if len(value) == 0 { if len(value) == 0 {
@ -173,10 +170,8 @@ func hasLabel(label string) func(container dockerData) bool {
} }
func getLabel(container dockerData, label string) (string, error) { func getLabel(container dockerData, label string) (string, error) {
for key, value := range container.Labels { if value, ok := container.Labels[label]; ok {
if key == label { return value, nil
return value, nil
}
} }
return "", fmt.Errorf("label not found: %s", label) return "", fmt.Errorf("label not found: %s", label)
} }

View file

@ -4,7 +4,6 @@ import (
"strings" "strings"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/containous/traefik/provider"
"github.com/containous/traefik/types" "github.com/containous/traefik/types"
"k8s.io/client-go/pkg/apis/extensions/v1beta1" "k8s.io/client-go/pkg/apis/extensions/v1beta1"
) )
@ -20,7 +19,7 @@ func getBoolAnnotation(meta *v1beta1.Ingress, name string, defaultValue bool) bo
case annotationStringValue == "true": case annotationStringValue == "true":
annotationValue = true annotationValue = true
default: default:
log.Warnf("Unknown value '%s' for %s, falling back to %s", name, types.LabelFrontendPassTLSCert, defaultValue) log.Warnf("Unknown value %q for %q, falling back to %q", annotationStringValue, name, defaultValue)
} }
return annotationValue return annotationValue
} }
@ -33,7 +32,7 @@ func getStringAnnotation(meta *v1beta1.Ingress, name string) string {
func getSliceAnnotation(meta *v1beta1.Ingress, name string) []string { func getSliceAnnotation(meta *v1beta1.Ingress, name string) []string {
var value []string var value []string
if annotation, ok := meta.Annotations[name]; ok && annotation != "" { if annotation, ok := meta.Annotations[name]; ok && annotation != "" {
value = provider.SplitAndTrimString(annotation) value = types.SplitAndTrimString(annotation)
} }
if len(value) == 0 { if len(value) == 0 {
log.Debugf("Could not load %v annotation, skipping...", name) log.Debugf("Could not load %v annotation, skipping...", name)

View file

@ -1,19 +0,0 @@
package provider
import "strings"
// SplitAndTrimString splits separatedString at the comma character and trims each
// piece, filtering out empty pieces. Returns the list of pieces or nil if the input
// did not contain a non-empty piece.
func SplitAndTrimString(base string) []string {
var trimmedStrings []string
for _, s := range strings.Split(base, ",") {
s = strings.TrimSpace(s)
if len(s) > 0 {
trimmedStrings = append(trimmedStrings, s)
}
}
return trimmedStrings
}

View file

@ -75,3 +75,19 @@ func ServiceLabel(key, serviceName string) string {
} }
return key return key
} }
// SplitAndTrimString splits separatedString at the comma character and trims each
// piece, filtering out empty pieces. Returns the list of pieces or nil if the input
// did not contain a non-empty piece.
func SplitAndTrimString(base string) []string {
var trimmedStrings []string
for _, s := range strings.Split(base, ",") {
s = strings.TrimSpace(s)
if len(s) > 0 {
trimmedStrings = append(trimmedStrings, s)
}
}
return trimmedStrings
}

View file

@ -1,4 +1,4 @@
package provider package types
import ( import (
"testing" "testing"