Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Host NetworkMode for ECS provider #2320

Merged
merged 4 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/configuration/backends/ecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Labels can be used on task containers to override default behaviour:
| `traefik.protocol=https` | override the default `http` protocol |
| `traefik.weight=10` | assign this weight to the container |
| `traefik.enable=false` | disable this container in Træfik |
| `traefik.port=80` | override the default `port` value. Overrides `NetworkBindings` from Docker Container |
| `traefik.backend.loadbalancer.method=drr` | override the default `wrr` load balancer algorithm |
| `traefik.backend.loadbalancer.stickiness=true` | enable backend sticky sessions |
| `traefik.backend.loadbalancer.stickiness.cookieName=NAME` | Manually set the cookie name for sticky sessions |
Expand Down
5 changes: 4 additions & 1 deletion provider/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (p *Provider) label(i ecsInstance, k string) string {
}

func (p *Provider) filterInstance(i ecsInstance) bool {
if len(i.container.NetworkBindings) == 0 {
if labelPort := p.label(i, types.LabelPort); len(i.container.NetworkBindings) == 0 && labelPort == "" {
log.Debugf("Filtering ecs instance without port %s (%s)", i.Name, i.ID)
return false
}
Expand Down Expand Up @@ -554,6 +554,9 @@ func (p *Provider) getHost(i ecsInstance) string {
}

func (p *Provider) getPort(i ecsInstance) string {
if port := p.label(i, types.LabelPort); port != "" {
return port
}
return strconv.FormatInt(*i.container.NetworkBindings[0].HostPort, 10)
}

Expand Down
65 changes: 65 additions & 0 deletions provider/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func simpleEcsInstance(labels map[string]*string) ecsInstance {
})
}

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

func TestEcsProtocol(t *testing.T) {
tests := []struct {
desc string
Expand Down Expand Up @@ -337,6 +345,12 @@ func TestFilterInstance(t *testing.T) {
invalidMachineState := simpleEcsInstance(map[string]*string{})
invalidMachineState.machine.State.Name = aws.String(ec2.InstanceStateNameStopped)

noNetwork := simpleEcsInstanceNoNetwork(map[string]*string{})

noNetworkWithLabel := simpleEcsInstanceNoNetwork(map[string]*string{
types.LabelPort: aws.String("80"),
})

tests := []struct {
desc string
expected bool
Expand Down Expand Up @@ -419,6 +433,22 @@ func TestFilterInstance(t *testing.T) {
ExposedByDefault: true,
},
},
{
desc: "Instance with no port mappings should be filtered",
expected: false,
instanceInfo: noNetwork,
provider: &Provider{
ExposedByDefault: true,
},
},
{
desc: "Instance with no port mapping and with label should not be filtered",
expected: true,
instanceInfo: noNetworkWithLabel,
provider: &Provider{
ExposedByDefault: true,
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -623,3 +653,38 @@ func TestGenerateECSConfig(t *testing.T) {
})
}
}

func TestEcsWithoutPort(t *testing.T) {
tests := []struct {
desc string
expected string
instanceInfo ecsInstance
provider *Provider
}{
{
desc: "Label should override network port",
expected: "4242",
instanceInfo: simpleEcsInstance(map[string]*string{
types.LabelPort: aws.String("4242"),
}),
provider: &Provider{},
},
{
desc: "Label should provide exposed port",
expected: "80",
instanceInfo: simpleEcsInstanceNoNetwork(map[string]*string{
types.LabelPort: aws.String("80"),
}),
provider: &Provider{},
},
}

for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
actual := test.provider.getPort(test.instanceInfo)
assert.Equal(t, test.expected, actual)
})
}
}