Skip to content

Commit

Permalink
Add support for several ECS backends
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatur authored and traefiker committed Aug 22, 2017
1 parent 05665f4 commit 8765494
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 79 deletions.
2 changes: 2 additions & 0 deletions cmd/traefik/traefik.go
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/containous/traefik/acme"
"github.com/containous/traefik/cluster"
"github.com/containous/traefik/log"
"github.com/containous/traefik/provider/ecs"
"github.com/containous/traefik/provider/kubernetes"
"github.com/containous/traefik/provider/rancher"
"github.com/containous/traefik/safe"
Expand Down Expand Up @@ -144,6 +145,7 @@ Complete documentation is available at https://traefik.io`,
f.AddParser(reflect.TypeOf(server.RootCAs{}), &server.RootCAs{})
f.AddParser(reflect.TypeOf(types.Constraints{}), &types.Constraints{})
f.AddParser(reflect.TypeOf(kubernetes.Namespaces{}), &kubernetes.Namespaces{})
f.AddParser(reflect.TypeOf(ecs.Clusters{}), &ecs.Clusters{})
f.AddParser(reflect.TypeOf([]acme.Domain{}), &acme.Domains{})
f.AddParser(reflect.TypeOf(types.Buckets{}), &types.Buckets{})

Expand Down
19 changes: 17 additions & 2 deletions docs/toml.md
Expand Up @@ -1708,10 +1708,16 @@ Træfik can be configured to use Amazon ECS as a backend configuration:
# ECS Cluster Name
#
# Deprecated - Please use Clusters
#
# Cluster = "default"
# ECS Clusters Name
#
# Optional
# Default: "default"
# Default: ["default"]
#
Cluster = "default"
Clusters = ["default"]
# Enable watch ECS changes
#
Expand All @@ -1720,6 +1726,13 @@ Cluster = "default"
#
Watch = true
# Enable auto discover ECS clusters
#
# Optional
# Default: false
#
AutoDiscoverClusters = false
# Polling interval (in seconds)
#
# Optional
Expand Down Expand Up @@ -1780,6 +1793,8 @@ Træfik needs the following policy to read ECS information:
"Sid": "Traefik ECS read access",
"Effect": "Allow",
"Action": [
"ecs:ListClusters",
"ecs:DescribeClusters",
"ecs:ListTasks",
"ecs:DescribeTasks",
"ecs:DescribeContainerInstances",
Expand Down
32 changes: 32 additions & 0 deletions provider/ecs/cluster.go
@@ -0,0 +1,32 @@
package ecs

import (
"fmt"
"strings"
)

// Clusters holds ecs clusters name
type Clusters []string

// Set adds strings elem into the the parser
// it splits str on , and ;
func (c *Clusters) Set(str string) error {
fargs := func(c rune) bool {
return c == ',' || c == ';'
}
// get function
slice := strings.FieldsFunc(str, fargs)
*c = append(*c, slice...)
return nil
}

// Get Clusters
func (c *Clusters) Get() interface{} { return Clusters(*c) }

// String return slice in a string
func (c *Clusters) String() string { return fmt.Sprintf("%v", *c) }

// SetValue sets Clusters into the parser
func (c *Clusters) SetValue(val interface{}) {
*c = Clusters(val.(Clusters))
}
80 changes: 80 additions & 0 deletions provider/ecs/cluster_test.go
@@ -0,0 +1,80 @@
package ecs

import (
"reflect"
"testing"
)

func TestClustersSet(t *testing.T) {
checkMap := map[string]Clusters{
"cluster": {"cluster"},
"cluster1,cluster2": {"cluster1", "cluster2"},
"cluster1;cluster2": {"cluster1", "cluster2"},
"cluster1,cluster2;cluster3": {"cluster1", "cluster2", "cluster3"},
}
for str, check := range checkMap {
var clusters Clusters
if err := clusters.Set(str); err != nil {
t.Fatalf("Error :%s", err)
}
if !reflect.DeepEqual(clusters, check) {
t.Fatalf("Expected:%s\ngot:%s", check, clusters)
}
}
}

func TestClustersGet(t *testing.T) {
slices := []Clusters{
{"cluster"},
{"cluster1", "cluster2"},
{"cluster1", "cluster2", "cluster3"},
}
check := []Clusters{
{"cluster"},
{"cluster1", "cluster2"},
{"cluster1", "cluster2", "cluster3"},
}
for i, slice := range slices {
if !reflect.DeepEqual(slice.Get(), check[i]) {
t.Fatalf("Expected:%s\ngot:%s", check[i], slice)
}
}
}

func TestClustersString(t *testing.T) {
slices := []Clusters{
{"cluster"},
{"cluster1", "cluster2"},
{"cluster1", "cluster2", "cluster3"},
}
check := []string{
"[cluster]",
"[cluster1 cluster2]",
"[cluster1 cluster2 cluster3]",
}
for i, slice := range slices {
if !reflect.DeepEqual(slice.String(), check[i]) {
t.Fatalf("Expected:%s\ngot:%s", check[i], slice)
}
}
}

func TestClustersSetValue(t *testing.T) {
check := []Clusters{
{"cluster"},
{"cluster1", "cluster2"},
{"cluster1", "cluster2", "cluster3"},
}
slices := []Clusters{
{"cluster"},
{"cluster1", "cluster2"},
{"cluster1", "cluster2", "cluster3"},
}
for i, s := range slices {
var slice Clusters
slice.SetValue(s)
if !reflect.DeepEqual(slice, check[i]) {
t.Fatalf("Expected:%s\ngot:%s", check[i], slice)
}
}
}

0 comments on commit 8765494

Please sign in to comment.