Skip to content

Commit

Permalink
AWS ECS Fargate
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatur authored and traefiker committed May 28, 2018
1 parent e76836b commit a7200a2
Show file tree
Hide file tree
Showing 39 changed files with 6,294 additions and 1,583 deletions.
7 changes: 4 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

[[constraint]]
name = "github.com/aws/aws-sdk-go"
version = "1.13.1"
version = "1.13.11"

[[constraint]]
branch = "master"
Expand Down
19 changes: 9 additions & 10 deletions provider/ecs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"text/template"

"github.com/BurntSushi/ty/fun"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/containous/traefik/log"
"github.com/containous/traefik/provider"
Expand Down Expand Up @@ -62,22 +61,22 @@ func (p *Provider) buildConfigurationV2(instances []ecsInstance) (*types.Configu
}

func (p *Provider) filterInstance(i ecsInstance) bool {
if labelPort := label.GetStringValue(i.TraefikLabels, label.TraefikPort, ""); len(i.container.NetworkBindings) == 0 && labelPort == "" {
log.Debugf("Filtering ecs instance without port %s (%s)", i.Name, i.ID)
if i.machine == nil {
log.Debug("Filtering ecs instance with nil machine")
return false
}

if i.machine == nil || i.machine.State == nil || i.machine.State.Name == nil {
log.Debugf("Filtering ecs instance with missing ec2 information %s (%s)", i.Name, i.ID)
if labelPort := label.GetStringValue(i.TraefikLabels, label.TraefikPort, ""); i.machine.port == 0 && labelPort == "" {
log.Debugf("Filtering ecs instance without port %s (%s)", i.Name, i.ID)
return false
}

if aws.StringValue(i.machine.State.Name) != ec2.InstanceStateNameRunning {
log.Debugf("Filtering ecs instance with an incorrect state %s (%s) (state = %s)", i.Name, i.ID, aws.StringValue(i.machine.State.Name))
if strings.ToLower(i.machine.state) != ec2.InstanceStateNameRunning {
log.Debugf("Filtering ecs instance with an incorrect state %s (%s) (state = %s)", i.Name, i.ID, i.machine.state)
return false
}

if i.machine.PrivateIpAddress == nil {
if len(i.machine.privateIP) == 0 {
log.Debugf("Filtering ecs instance without an ip address %s (%s)", i.Name, i.ID)
return false
}
Expand All @@ -98,14 +97,14 @@ func (p *Provider) getFrontendRule(i ecsInstance) string {
}

func getHost(i ecsInstance) string {
return aws.StringValue(i.machine.PrivateIpAddress)
return i.machine.privateIP
}

func getPort(i ecsInstance) string {
if value := label.GetStringValue(i.TraefikLabels, label.TraefikPort, ""); len(value) > 0 {
return value
}
return strconv.FormatInt(aws.Int64Value(i.container.NetworkBindings[0].HostPort), 10)
return strconv.FormatInt(i.machine.port, 10)
}

func filterFrontends(instances []ecsInstance) []ecsInstance {
Expand Down
98 changes: 30 additions & 68 deletions provider/ecs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,10 @@ func TestBuildConfiguration(t *testing.T) {
containerDefinition: &ecs.ContainerDefinition{
DockerLabels: map[string]*string{},
},
machine: &ec2.Instance{
State: &ec2.InstanceState{
Name: aws.String(ec2.InstanceStateNameRunning),
},
PrivateIpAddress: aws.String("10.0.0.1"),
},
container: &ecs.Container{
NetworkBindings: []*ecs.NetworkBinding{{
HostPort: aws.Int64(1337),
}},
machine: &machine{
state: ec2.InstanceStateNameRunning,
privateIP: "10.0.0.1",
port: 1337,
},
},
},
Expand Down Expand Up @@ -78,16 +72,10 @@ func TestBuildConfiguration(t *testing.T) {
label.TraefikBackendHealthCheckPath: aws.String("/health"),
label.TraefikBackendHealthCheckInterval: aws.String("1s"),
}},
machine: &ec2.Instance{
State: &ec2.InstanceState{
Name: aws.String(ec2.InstanceStateNameRunning),
},
PrivateIpAddress: aws.String("10.0.0.1"),
},
container: &ecs.Container{
NetworkBindings: []*ecs.NetworkBinding{{
HostPort: aws.Int64(1337),
}},
machine: &machine{
state: ec2.InstanceStateNameRunning,
privateIP: "10.0.0.1",
port: 1337,
},
},
},
Expand Down Expand Up @@ -204,16 +192,10 @@ func TestBuildConfiguration(t *testing.T) {
label.Prefix + label.BaseFrontendRateLimit + "bar." + label.SuffixRateLimitAverage: aws.String("6"),
label.Prefix + label.BaseFrontendRateLimit + "bar." + label.SuffixRateLimitBurst: aws.String("9"),
}},
machine: &ec2.Instance{
State: &ec2.InstanceState{
Name: aws.String(ec2.InstanceStateNameRunning),
},
PrivateIpAddress: aws.String("10.0.0.1"),
},
container: &ecs.Container{
NetworkBindings: []*ecs.NetworkBinding{{
HostPort: aws.Int64(1337),
}},
machine: &machine{
state: ec2.InstanceStateNameRunning,
privateIP: "10.0.0.1",
port: 1337,
},
},
},
Expand Down Expand Up @@ -420,10 +402,10 @@ func TestFilterInstance(t *testing.T) {
expected: true,
},
{
desc: "Instance with nil private ip and exposed by default enabled should be filtered",
desc: "Instance with empty private ip and exposed by default enabled should be filtered",
instanceInfo: func() ecsInstance {
nilPrivateIP := simpleEcsInstance(map[string]*string{})
nilPrivateIP.machine.PrivateIpAddress = nil
nilPrivateIP.machine.privateIP = ""
return nilPrivateIP
}(),
exposedByDefault: true,
Expand All @@ -440,30 +422,20 @@ func TestFilterInstance(t *testing.T) {
expected: false,
},
{
desc: "Instance with nil machine state and exposed by default enabled should be filtered",
desc: "Instance with empty machine state and exposed by default enabled should be filtered",
instanceInfo: func() ecsInstance {
nilMachineState := simpleEcsInstance(map[string]*string{})
nilMachineState.machine.State = nil
nilMachineState.machine.state = ""
return nilMachineState
}(),
exposedByDefault: true,
expected: false,
},
{
desc: "Instance with nil machine state name and exposed by default enabled should be filtered",
instanceInfo: func() ecsInstance {
nilMachineStateName := simpleEcsInstance(map[string]*string{})
nilMachineStateName.machine.State.Name = nil
return nilMachineStateName
}(),
exposedByDefault: true,
expected: false,
},
{
desc: "Instance with invalid machine state and exposed by default enabled should be filtered",
instanceInfo: func() ecsInstance {
invalidMachineState := simpleEcsInstance(map[string]*string{})
invalidMachineState.machine.State.Name = aws.String(ec2.InstanceStateNameStopped)
invalidMachineState.machine.state = ec2.InstanceStateNameStopped
return invalidMachineState
}(),
exposedByDefault: true,
Expand Down Expand Up @@ -735,21 +707,13 @@ func makeEcsInstance(containerDef *ecs.ContainerDefinition) ecsInstance {
}

instance := ecsInstance{
Name: "foo-http",
ID: "123456789abc",
task: &ecs.Task{
Containers: []*ecs.Container{container},
},
taskDefinition: &ecs.TaskDefinition{
ContainerDefinitions: []*ecs.ContainerDefinition{containerDef},
},
container: container,
Name: "foo-http",
ID: "123456789abc",
containerDefinition: containerDef,
machine: &ec2.Instance{
PrivateIpAddress: aws.String("10.0.0.0"),
State: &ec2.InstanceState{
Name: aws.String(ec2.InstanceStateNameRunning),
},
machine: &machine{
state: ec2.InstanceStateNameRunning,
privateIP: "10.0.0.0",
port: 1337,
},
}

Expand All @@ -761,23 +725,21 @@ func makeEcsInstance(containerDef *ecs.ContainerDefinition) ecsInstance {
}

func simpleEcsInstance(labels map[string]*string) ecsInstance {
return makeEcsInstance(&ecs.ContainerDefinition{
Name: aws.String("http"),
PortMappings: []*ecs.PortMapping{{
HostPort: aws.Int64(80),
ContainerPort: aws.Int64(80),
Protocol: aws.String("tcp"),
}},
instance := makeEcsInstance(&ecs.ContainerDefinition{
Name: aws.String("http"),
DockerLabels: labels,
})
instance.machine.port = 80
return instance
}

func simpleEcsInstanceNoNetwork(labels map[string]*string) ecsInstance {
return makeEcsInstance(&ecs.ContainerDefinition{
instance := makeEcsInstance(&ecs.ContainerDefinition{
Name: aws.String("http"),
PortMappings: []*ecs.PortMapping{},
DockerLabels: labels,
})
instance.machine.port = 0
return instance
}

func fakeLoadTraefikLabels(instances []ecsInstance) []ecsInstance {
Expand Down
15 changes: 8 additions & 7 deletions provider/ecs/deprecated_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ecs

import (
"strconv"
"strings"
"text/template"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -58,22 +59,22 @@ func (p *Provider) buildConfigurationV1(instances []ecsInstance) (*types.Configu
}

func (p *Provider) filterInstanceV1(i ecsInstance) bool {
if labelPort := getStringValueV1(i, label.TraefikPort, ""); len(i.container.NetworkBindings) == 0 && labelPort == "" {
log.Debugf("Filtering ecs instance without port %s (%s)", i.Name, i.ID)
if i.machine == nil {
log.Debug("Filtering ecs instance with nil machine")
return false
}

if i.machine == nil || i.machine.State == nil || i.machine.State.Name == nil {
log.Debugf("Filtering ecs instance in an missing ec2 information %s (%s)", i.Name, i.ID)
if labelPort := getStringValueV1(i, label.TraefikPort, ""); i.machine.port == 0 && labelPort == "" {
log.Debugf("Filtering ecs instance without port %s (%s)", i.Name, i.ID)
return false
}

if aws.StringValue(i.machine.State.Name) != ec2.InstanceStateNameRunning {
log.Debugf("Filtering ecs instance in an incorrect state %s (%s) (state = %s)", i.Name, i.ID, aws.StringValue(i.machine.State.Name))
if strings.ToLower(i.machine.state) != ec2.InstanceStateNameRunning {
log.Debugf("Filtering ecs instance in an incorrect state %s (%s) (state = %s)", i.Name, i.ID, i.machine.state)
return false
}

if i.machine.PrivateIpAddress == nil {
if len(i.machine.privateIP) == 0 {
log.Debugf("Filtering ecs instance without an ip address %s (%s)", i.Name, i.ID)
return false
}
Expand Down
42 changes: 12 additions & 30 deletions provider/ecs/deprecated_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ func TestBuildConfigurationV1(t *testing.T) {
containerDefinition: &ecs.ContainerDefinition{
DockerLabels: map[string]*string{},
},
machine: &ec2.Instance{
State: &ec2.InstanceState{
Name: aws.String(ec2.InstanceStateNameRunning),
},
PrivateIpAddress: aws.String("10.0.0.1"),
},
container: &ecs.Container{
NetworkBindings: []*ecs.NetworkBinding{{
HostPort: aws.Int64(1337),
}},
machine: &machine{
state: ec2.InstanceStateNameRunning,
privateIP: "10.0.0.1",
port: 1337,
},
},
},
Expand Down Expand Up @@ -79,16 +73,10 @@ func TestBuildConfigurationV1(t *testing.T) {
label.TraefikBackendHealthCheckPath: aws.String("/health"),
label.TraefikBackendHealthCheckInterval: aws.String("1s"),
}},
machine: &ec2.Instance{
State: &ec2.InstanceState{
Name: aws.String(ec2.InstanceStateNameRunning),
},
PrivateIpAddress: aws.String("10.0.0.1"),
},
container: &ecs.Container{
NetworkBindings: []*ecs.NetworkBinding{{
HostPort: aws.Int64(1337),
}},
machine: &machine{
state: ec2.InstanceStateNameRunning,
privateIP: "10.0.0.1",
port: 1337,
},
},
},
Expand Down Expand Up @@ -151,16 +139,10 @@ func TestBuildConfigurationV1(t *testing.T) {
label.TraefikFrontendPriority: aws.String("666"),
label.TraefikFrontendRule: aws.String("Host:traefik.io"),
}},
machine: &ec2.Instance{
State: &ec2.InstanceState{
Name: aws.String(ec2.InstanceStateNameRunning),
},
PrivateIpAddress: aws.String("10.0.0.1"),
},
container: &ecs.Container{
NetworkBindings: []*ecs.NetworkBinding{{
HostPort: aws.Int64(1337),
}},
machine: &machine{
state: ec2.InstanceStateNameRunning,
privateIP: "10.0.0.1",
port: 1337,
},
},
},
Expand Down

0 comments on commit a7200a2

Please sign in to comment.