From f084d2a28b31fb8c2c54c65bb1cf7cc140631795 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 30 Nov 2017 09:26:03 +0100 Subject: [PATCH] Fix Labels/annotation logs and values. --- provider/docker/docker.go | 1 - provider/docker/labels.go | 21 +++++++------------ provider/kubernetes/annotations_parser.go | 5 ++--- provider/string_util.go | 19 ----------------- types/common_label.go | 16 ++++++++++++++ .../common_label_test.go | 2 +- 6 files changed, 27 insertions(+), 37 deletions(-) delete mode 100644 provider/string_util.go rename provider/string_util_test.go => types/common_label_test.go (98%) diff --git a/provider/docker/docker.go b/provider/docker/docker.go index ad90b39f5..063d34e37 100644 --- a/provider/docker/docker.go +++ b/provider/docker/docker.go @@ -121,7 +121,6 @@ func (p Provider) createClient() (client.APIClient, error) { } return client.NewClient(p.Endpoint, apiVersion, httpClient, httpHeaders) - } // Provide allows the docker provider to provide configurations to traefik diff --git a/provider/docker/labels.go b/provider/docker/labels.go index 070d3b55d..41f494cc1 100644 --- a/provider/docker/labels.go +++ b/provider/docker/labels.go @@ -2,12 +2,10 @@ package docker import ( "fmt" - "math" "strconv" "strings" "github.com/containous/traefik/log" - "github.com/containous/traefik/provider" "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 { return func(container dockerData) int64 { - if label, err := getLabel(container, labelName); err == nil { - i, errConv := strconv.ParseInt(label, 10, 64) - if errConv != nil { - log.Errorf("Unable to parse traefik.backend.maxconn.amount %s", label) - return math.MaxInt64 + if rawValue, err := getLabel(container, labelName); err == nil { + value, errConv := strconv.ParseInt(rawValue, 10, 64) + if errConv == nil { + return value } - return i + log.Errorf("Unable to parse %q: %q", labelName, rawValue) } return defaultValue } @@ -96,7 +93,7 @@ func getSliceStringLabel(container dockerData, labelName string) []string { var value []string if label, err := getLabel(container, labelName); err == nil { - value = provider.SplitAndTrimString(label) + value = types.SplitAndTrimString(label) } if len(value) == 0 { @@ -173,10 +170,8 @@ func hasLabel(label string) func(container dockerData) bool { } func getLabel(container dockerData, label string) (string, error) { - for key, value := range container.Labels { - if key == label { - return value, nil - } + if value, ok := container.Labels[label]; ok { + return value, nil } return "", fmt.Errorf("label not found: %s", label) } diff --git a/provider/kubernetes/annotations_parser.go b/provider/kubernetes/annotations_parser.go index 361a87f04..8897611b3 100644 --- a/provider/kubernetes/annotations_parser.go +++ b/provider/kubernetes/annotations_parser.go @@ -4,7 +4,6 @@ import ( "strings" "github.com/containous/traefik/log" - "github.com/containous/traefik/provider" "github.com/containous/traefik/types" "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": annotationValue = true 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 } @@ -33,7 +32,7 @@ func getStringAnnotation(meta *v1beta1.Ingress, name string) string { func getSliceAnnotation(meta *v1beta1.Ingress, name string) []string { var value []string if annotation, ok := meta.Annotations[name]; ok && annotation != "" { - value = provider.SplitAndTrimString(annotation) + value = types.SplitAndTrimString(annotation) } if len(value) == 0 { log.Debugf("Could not load %v annotation, skipping...", name) diff --git a/provider/string_util.go b/provider/string_util.go deleted file mode 100644 index f3877e293..000000000 --- a/provider/string_util.go +++ /dev/null @@ -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 -} diff --git a/types/common_label.go b/types/common_label.go index 52240ce17..5b2e19351 100644 --- a/types/common_label.go +++ b/types/common_label.go @@ -75,3 +75,19 @@ func ServiceLabel(key, serviceName string) string { } 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 +} diff --git a/provider/string_util_test.go b/types/common_label_test.go similarity index 98% rename from provider/string_util_test.go rename to types/common_label_test.go index 5b8c6854d..b97be2085 100644 --- a/provider/string_util_test.go +++ b/types/common_label_test.go @@ -1,4 +1,4 @@ -package provider +package types import ( "testing"