Fix mapping a range of host ports to a single container port

Signed-off-by: Sami Tabet <salph.tabet@gmail.com>
This commit is contained in:
Sami Tabet 2018-06-01 01:41:22 +02:00
parent 31d99ba630
commit 63e5c29e00
2 changed files with 47 additions and 8 deletions

View File

@ -151,17 +151,22 @@ func ConvertPortToPortConfig(
if binding.HostIP != "" && binding.HostIP != "0.0.0.0" {
logrus.Warnf("ignoring IP-address (%s:%s:%s) service will listen on '0.0.0.0'", binding.HostIP, binding.HostPort, port)
}
hostPort, err := strconv.ParseUint(binding.HostPort, 10, 16)
startHostPort, endHostPort, err := nat.ParsePortRange(binding.HostPort)
if err != nil && binding.HostPort != "" {
return nil, fmt.Errorf("invalid hostport binding (%s) for port (%s)", binding.HostPort, port.Port())
}
ports = append(ports, swarm.PortConfig{
//TODO Name: ?
Protocol: swarm.PortConfigProtocol(strings.ToLower(port.Proto())),
TargetPort: uint32(port.Int()),
PublishedPort: uint32(hostPort),
PublishMode: swarm.PortConfigPublishModeIngress,
})
for i := startHostPort; i <= endHostPort; i++ {
ports = append(ports, swarm.PortConfig{
//TODO Name: ?
Protocol: swarm.PortConfigProtocol(strings.ToLower(port.Proto())),
TargetPort: uint32(port.Int()),
PublishedPort: uint32(i),
PublishMode: swarm.PortConfigPublishModeIngress,
})
}
}
return ports, nil
}

View File

@ -96,6 +96,40 @@ func TestPortOptValidSimpleSyntax(t *testing.T) {
},
},
},
{
value: "80-82:8080/udp",
expected: []swarm.PortConfig{
{
Protocol: "udp",
TargetPort: 8080,
PublishedPort: 80,
PublishMode: swarm.PortConfigPublishModeIngress,
},
{
Protocol: "udp",
TargetPort: 8080,
PublishedPort: 81,
PublishMode: swarm.PortConfigPublishModeIngress,
},
{
Protocol: "udp",
TargetPort: 8080,
PublishedPort: 82,
PublishMode: swarm.PortConfigPublishModeIngress,
},
},
},
{
value: "80-80:8080/tcp",
expected: []swarm.PortConfig{
{
Protocol: "tcp",
TargetPort: 8080,
PublishedPort: 80,
PublishMode: swarm.PortConfigPublishModeIngress,
},
},
},
}
for _, tc := range testCases {
var port PortOpt