1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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
parent 1eafa0f706
commit 135f8f9674
2 changed files with 16 additions and 7 deletions

View file

@ -301,9 +301,11 @@ func convertEndpointSpec(source []string) (*swarm.EndpointSpec, error) {
}
for port := range ports {
portConfigs = append(
portConfigs,
opts.ConvertPortToPortConfig(port, portBindings)...)
portConfig, err := opts.ConvertPortToPortConfig(port, portBindings)
if err != nil {
return nil, err
}
portConfigs = append(portConfigs, portConfig...)
}
return &swarm.EndpointSpec{Ports: portConfigs}, nil

View file

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