Fix GroupsAsSubDomains option for Mesos and Marathon (#868)

* Fix GroupsAsSubDomains option for Mesos and Marathon
* Refactor reverseStringSlice function
This commit is contained in:
Ryan Leary 2016-11-28 08:59:08 -05:00 committed by Emile Vauge
parent e34c364d5e
commit 055cd01bb7
5 changed files with 73 additions and 7 deletions

View file

@ -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
}

View file

@ -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)
}
}
}

View file

@ -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
}

View file

@ -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 (

View file

@ -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 {