Add error checking for hostPort range

This fix catches the case where there is a single container port
and a dynamic host port and will fail out gracefully
Example docker-compose.yml snippet:
    port:
        ports:
            - "8091-8093:8091"
            - "80:8080"

Signed-off-by: Tony Abboud <tdabboud@hotmail.com>
This commit is contained in:
Tony Abboud 2017-01-12 12:01:29 -05:00 committed by Vincent Demeester
parent 52e9a69df9
commit 438279688c
1 changed files with 11 additions and 4 deletions

View File

@ -98,7 +98,11 @@ func (p *PortOpt) Set(value string) error {
ports, portBindings, _ := nat.ParsePortSpecs([]string{value}) ports, portBindings, _ := nat.ParsePortSpecs([]string{value})
for port := range ports { for port := range ports {
portConfigs = append(portConfigs, ConvertPortToPortConfig(port, portBindings)...) portConfig, err := ConvertPortToPortConfig(port, portBindings)
if err != nil {
return err
}
portConfigs = append(portConfigs, portConfig...)
} }
p.ports = append(p.ports, portConfigs...) p.ports = append(p.ports, portConfigs...)
} }
@ -129,11 +133,14 @@ func (p *PortOpt) Value() []swarm.PortConfig {
func ConvertPortToPortConfig( func ConvertPortToPortConfig(
port nat.Port, port nat.Port,
portBindings map[nat.Port][]nat.PortBinding, portBindings map[nat.Port][]nat.PortBinding,
) []swarm.PortConfig { ) ([]swarm.PortConfig, error) {
ports := []swarm.PortConfig{} ports := []swarm.PortConfig{}
for _, binding := range portBindings[port] { for _, binding := range portBindings[port] {
hostPort, _ := strconv.ParseUint(binding.HostPort, 10, 16) hostPort, err := strconv.ParseUint(binding.HostPort, 10, 16)
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{ ports = append(ports, swarm.PortConfig{
//TODO Name: ? //TODO Name: ?
Protocol: swarm.PortConfigProtocol(strings.ToLower(port.Proto())), Protocol: swarm.PortConfigProtocol(strings.ToLower(port.Proto())),
@ -142,5 +149,5 @@ func ConvertPortToPortConfig(
PublishMode: swarm.PortConfigPublishModeIngress, PublishMode: swarm.PortConfigPublishModeIngress,
}) })
} }
return ports return ports, nil
} }