Support Host NetworkMode for ECS provider

This commit is contained in:
Félix P 2017-10-31 11:44:03 +01:00 committed by Traefiker
parent e8d63b2a3b
commit 93a46089ce
3 changed files with 70 additions and 1 deletions

View file

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

View file

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

View file

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