Skip to content

Commit

Permalink
Fix GroupsAsSubDomains option for Mesos and Marathon
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Leary committed Nov 22, 2016
1 parent fe1b982 commit 41ca450
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
5 changes: 3 additions & 2 deletions provider/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"errors"
"net/url"
"sort"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -419,7 +418,9 @@ 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)))
for i, j := 0, len(splitedName)-1; i < j; i, j = i+1, j-1 {
splitedName[i], splitedName[j] = splitedName[j], splitedName[i]
}
reverseName := strings.Join(splitedName, ".")
return reverseName
}
Expand Down
30 changes: 30 additions & 0 deletions provider/marathon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
8 changes: 5 additions & 3 deletions provider/mesos.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"text/template"

"fmt"
"time"

"github.com/BurntSushi/ty/fun"
"github.com/cenk/backoff"
"github.com/containous/traefik/job"
Expand All @@ -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)
Expand Down Expand Up @@ -435,7 +435,9 @@ 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)))
for i, j := 0, len(splitedName)-1; i < j; i, j = i+1, j-1 {
splitedName[i], splitedName[j] = splitedName[j], splitedName[i]
}
reverseName := strings.Join(splitedName, ".")
return reverseName
}
Expand Down
35 changes: 33 additions & 2 deletions provider/mesos_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 41ca450

Please sign in to comment.