From 055cd01bb7dd19fd08567472a9fdcaab122c41c8 Mon Sep 17 00:00:00 2001 From: Ryan Leary Date: Mon, 28 Nov 2016 08:59:08 -0500 Subject: [PATCH] Fix GroupsAsSubDomains option for Mesos and Marathon (#868) * Fix GroupsAsSubDomains option for Mesos and Marathon * Refactor reverseStringSlice function --- provider/marathon.go | 3 +-- provider/marathon_test.go | 30 ++++++++++++++++++++++++++++++ provider/mesos.go | 6 +++--- provider/mesos_test.go | 35 +++++++++++++++++++++++++++++++++-- provider/provider.go | 6 ++++++ 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/provider/marathon.go b/provider/marathon.go index e2b3b7386..9b98f0d72 100644 --- a/provider/marathon.go +++ b/provider/marathon.go @@ -3,7 +3,6 @@ package provider import ( "errors" "net/url" - "sort" "strconv" "strings" "text/template" @@ -419,7 +418,7 @@ func (provider *Marathon) getFrontendBackend(application marathon.Application) s func (provider *Marathon) getSubDomain(name string) string { if provider.GroupsAsSubDomains { splitedName := strings.Split(strings.TrimPrefix(name, "/"), "/") - sort.Sort(sort.Reverse(sort.StringSlice(splitedName))) + reverseStringSlice(&splitedName) reverseName := strings.Join(splitedName, ".") return reverseName } diff --git a/provider/marathon_test.go b/provider/marathon_test.go index bcc05404e..1fa1dbe9a 100644 --- a/provider/marathon_test.go +++ b/provider/marathon_test.go @@ -1280,3 +1280,33 @@ func TestMarathonGetBackend(t *testing.T) { } } } + +func TestMarathonGetSubDomain(t *testing.T) { + providerGroups := &Marathon{GroupsAsSubDomains: true} + providerNoGroups := &Marathon{GroupsAsSubDomains: false} + + apps := []struct { + path string + expected string + provider *Marathon + }{ + {"/test", "test", providerNoGroups}, + {"/test", "test", providerGroups}, + {"/a/b/c/d", "d.c.b.a", providerGroups}, + {"/b/a/d/c", "c.d.a.b", providerGroups}, + {"/d/c/b/a", "a.b.c.d", providerGroups}, + {"/c/d/a/b", "b.a.d.c", providerGroups}, + {"/a/b/c/d", "a-b-c-d", providerNoGroups}, + {"/b/a/d/c", "b-a-d-c", providerNoGroups}, + {"/d/c/b/a", "d-c-b-a", providerNoGroups}, + {"/c/d/a/b", "c-d-a-b", providerNoGroups}, + } + + for _, a := range apps { + actual := a.provider.getSubDomain(a.path) + + if actual != a.expected { + t.Errorf("expected %q, got %q", a.expected, actual) + } + } +} diff --git a/provider/mesos.go b/provider/mesos.go index fab03d422..1822a3e60 100644 --- a/provider/mesos.go +++ b/provider/mesos.go @@ -7,6 +7,8 @@ import ( "text/template" "fmt" + "time" + "github.com/BurntSushi/ty/fun" "github.com/cenk/backoff" "github.com/containous/traefik/job" @@ -20,8 +22,6 @@ import ( "github.com/mesosphere/mesos-dns/records" "github.com/mesosphere/mesos-dns/records/state" "github.com/mesosphere/mesos-dns/util" - "sort" - "time" ) var _ Provider = (*Mesos)(nil) @@ -435,7 +435,7 @@ func Ignore(f ErrorFunction) { func (provider *Mesos) getSubDomain(name string) string { if provider.GroupsAsSubDomains { splitedName := strings.Split(strings.TrimPrefix(name, "/"), "/") - sort.Sort(sort.Reverse(sort.StringSlice(splitedName))) + reverseStringSlice(&splitedName) reverseName := strings.Join(splitedName, ".") return reverseName } diff --git a/provider/mesos_test.go b/provider/mesos_test.go index 5434915b3..ba8ccd4db 100644 --- a/provider/mesos_test.go +++ b/provider/mesos_test.go @@ -1,11 +1,12 @@ package provider import ( + "reflect" + "testing" + "github.com/containous/traefik/log" "github.com/containous/traefik/types" "github.com/mesosphere/mesos-dns/records/state" - "reflect" - "testing" ) func TestMesosTaskFilter(t *testing.T) { @@ -244,6 +245,36 @@ func TestMesosLoadConfig(t *testing.T) { } } +func TestMesosGetSubDomain(t *testing.T) { + providerGroups := &Mesos{GroupsAsSubDomains: true} + providerNoGroups := &Mesos{GroupsAsSubDomains: false} + + apps := []struct { + path string + expected string + provider *Mesos + }{ + {"/test", "test", providerNoGroups}, + {"/test", "test", providerGroups}, + {"/a/b/c/d", "d.c.b.a", providerGroups}, + {"/b/a/d/c", "c.d.a.b", providerGroups}, + {"/d/c/b/a", "a.b.c.d", providerGroups}, + {"/c/d/a/b", "b.a.d.c", providerGroups}, + {"/a/b/c/d", "a-b-c-d", providerNoGroups}, + {"/b/a/d/c", "b-a-d-c", providerNoGroups}, + {"/d/c/b/a", "d-c-b-a", providerNoGroups}, + {"/c/d/a/b", "c-d-a-b", providerNoGroups}, + } + + for _, a := range apps { + actual := a.provider.getSubDomain(a.path) + + if actual != a.expected { + t.Errorf("expected %q, got %q", a.expected, actual) + } + } +} + // test helpers type ( diff --git a/provider/provider.go b/provider/provider.go index a7ce0dad4..09982bbec 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -101,6 +101,12 @@ func normalize(name string) string { return strings.Join(strings.FieldsFunc(name, fargs), "-") } +func reverseStringSlice(slice *[]string) { + for i, j := 0, len(*slice)-1; i < j; i, j = i+1, j-1 { + (*slice)[i], (*slice)[j] = (*slice)[j], (*slice)[i] + } +} + // ClientTLS holds TLS specific configurations as client // CA, Cert and Key can be either path or file contents type ClientTLS struct {