From f7d9dfafd0c486b31957e0eefa0bf792bfba9b10 Mon Sep 17 00:00:00 2001 From: Timo Reimann Date: Thu, 18 May 2017 23:27:10 +0200 Subject: [PATCH] [k8s] Remove rule type path list. Instead of doing sanity checks in the Kubernetes provider, we just accept any non-empty value from the annotation and rely on the server part to filter out unknown rules. This allows us to automatically stay in sync with the currently supported Path matchers/modifiers. --- provider/kubernetes/kubernetes.go | 32 +---------- provider/kubernetes/kubernetes_test.go | 73 ++------------------------ 2 files changed, 6 insertions(+), 99 deletions(-) diff --git a/provider/kubernetes/kubernetes.go b/provider/kubernetes/kubernetes.go index 61e8bc842..1d81c736a 100644 --- a/provider/kubernetes/kubernetes.go +++ b/provider/kubernetes/kubernetes.go @@ -27,9 +27,6 @@ var _ provider.Provider = (*Provider)(nil) const ( annotationFrontendRuleType = "traefik.frontend.rule.type" - ruleTypePathPrefixStrip = "PathPrefixStrip" - ruleTypePathStrip = "PathStrip" - ruleTypePath = "Path" ruleTypePathPrefix = "PathPrefix" ) @@ -199,12 +196,8 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error) } if len(pa.Path) > 0 { - ruleType, unknown := getRuleTypeFromAnnotation(i.Annotations) - switch { - case unknown: - log.Warnf("Unknown RuleType '%s' for Ingress %s/%s, falling back to PathPrefix", ruleType, i.ObjectMeta.Namespace, i.ObjectMeta.Name) - fallthrough - case ruleType == "": + ruleType := i.Annotations[annotationFrontendRuleType] + if ruleType == "" { ruleType = ruleTypePathPrefix } @@ -392,24 +385,3 @@ func (p *Provider) loadConfig(templateObjects types.Configuration) *types.Config } return configuration } - -func getRuleTypeFromAnnotation(annotations map[string]string) (ruleType string, unknown bool) { - ruleType = annotations[annotationFrontendRuleType] - for _, knownRuleType := range []string{ - ruleTypePathPrefixStrip, - ruleTypePathStrip, - ruleTypePath, - ruleTypePathPrefix, - } { - if strings.ToLower(ruleType) == strings.ToLower(knownRuleType) { - return knownRuleType, false - } - } - - if ruleType != "" { - // Annotation is set but does not match anything we know. - unknown = true - } - - return ruleType, unknown -} diff --git a/provider/kubernetes/kubernetes_test.go b/provider/kubernetes/kubernetes_test.go index 5cb0219ae..d3fe45d88 100644 --- a/provider/kubernetes/kubernetes_test.go +++ b/provider/kubernetes/kubernetes_test.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "reflect" - "strings" "testing" "github.com/containous/traefik/types" @@ -333,19 +332,14 @@ func TestRuleType(t *testing.T) { frontendRuleType string }{ { - desc: "implicit default", + desc: "rule type annotation missing", ingressRuleType: "", frontendRuleType: ruleTypePathPrefix, }, { - desc: "unknown ingress / explicit default", - ingressRuleType: "unknown", - frontendRuleType: ruleTypePathPrefix, - }, - { - desc: "explicit ingress", - ingressRuleType: ruleTypePath, - frontendRuleType: ruleTypePath, + desc: "rule type annotation set", + ingressRuleType: "Path", + frontendRuleType: "Path", }, } @@ -1773,65 +1767,6 @@ func TestInvalidPassHostHeaderValue(t *testing.T) { } } -func TestGetRuleTypeFromAnnotation(t *testing.T) { - tests := []struct { - in string - wantedUnknown bool - }{ - { - in: ruleTypePathPrefixStrip, - wantedUnknown: false, - }, - { - in: ruleTypePathStrip, - wantedUnknown: false, - }, - { - in: ruleTypePath, - wantedUnknown: false, - }, - { - in: ruleTypePathPrefix, - wantedUnknown: false, - }, - { - wantedUnknown: false, - }, - { - in: "Unknown", - wantedUnknown: true, - }, - } - - for _, test := range tests { - test := test - inputs := []string{test.in, strings.ToLower(test.in)} - if inputs[0] == inputs[1] { - // Lower-casing makes no difference -- truncating to single case. - inputs = inputs[:1] - } - for _, input := range inputs { - t.Run(fmt.Sprintf("in='%s'", input), func(t *testing.T) { - t.Parallel() - annotations := map[string]string{} - if test.in != "" { - annotations[annotationFrontendRuleType] = test.in - } - - gotRuleType, gotUnknown := getRuleTypeFromAnnotation(annotations) - - if gotUnknown != test.wantedUnknown { - t.Errorf("got unknown '%t', wanted '%t'", gotUnknown, test.wantedUnknown) - } - - if gotRuleType != test.in { - t.Errorf("got rule type '%s', wanted '%s'", gotRuleType, test.in) - } - }) - } - } -} - func TestKubeAPIErrors(t *testing.T) { ingresses := []*v1beta1.Ingress{{ ObjectMeta: v1.ObjectMeta{