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

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

Conversation

sfluor
Copy link
Contributor

@sfluor sfluor commented May 31, 2018

- What I did
fixes #1074

- How I did it
Splitting the host port range in a list of host ports

- How to verify it
run docker service create --name multi -p 8080-8081:80 nginx:alpine

- Description for the changelog
fix #1074 by splitting host port range into a list of host ports

- A picture of a cute animal (not mandatory but encouraged)
shiba

opts/port.go Outdated
@@ -148,20 +148,31 @@ func ConvertPortToPortConfig(
ports := []swarm.PortConfig{}

for _, binding := range portBindings[port] {
hostPorts := []uint64{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR!
I think this code can be simplified a little (and avoid the double iteration):

for _, binding := range portBindings[port] {
    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)
    }
    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())
    }
    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(hostPort),
            PublishMode:   swarm.PortConfigPublishModeIngress,
        })
    }
}

Does it sounds good to you?
And is it possible to add some tests on this feature in opts/port_test.go?

@sfluor sfluor force-pushed the 1074-fix-mapping-a-range-of-host-ports-to-a-single-container-port branch from 94c7ffe to 9d44556 Compare June 1, 2018 14:35
@sfluor
Copy link
Contributor Author

sfluor commented Jun 1, 2018

Hello 😄 !

I just removed the for loop as you suggested and added test cases for this behavior.

},
},
{
value: "81-83:9999/tcp",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test is quite redundant with the previous one (value: "80-82:8080/udp").

@sfluor sfluor force-pushed the 1074-fix-mapping-a-range-of-host-ports-to-a-single-container-port branch from 9d44556 to 35380a9 Compare June 1, 2018 16:40
@sfluor
Copy link
Contributor Author

sfluor commented Jun 1, 2018

I just removed the redundant test ^^

Copy link
Contributor

@silvin-lubecki silvin-lubecki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @sfluor ! LGTM
cc @vdemeester @thaJeztah

opts/port.go Outdated
@@ -148,20 +148,26 @@ func ConvertPortToPortConfig(
ports := []swarm.PortConfig{}

for _, binding := range portBindings[port] {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remove it 👍

@sfluor sfluor force-pushed the 1074-fix-mapping-a-range-of-host-ports-to-a-single-container-port branch from 35380a9 to a71fc9e Compare June 4, 2018 10:23
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@thaJeztah
Copy link
Member

Just thinking of this (haven't tried), but does this also work for docker stack deploy? (if not, we may have to add additional changes)

@sfluor could you squash your commits? I think its ok to have the tests and code-changes in the same commit.

@thaJeztah
Copy link
Member

Oh, and we should check if we can / want to support this notation for the long syntax; moby/moby#32551

@sfluor sfluor force-pushed the 1074-fix-mapping-a-range-of-host-ports-to-a-single-container-port branch from a71fc9e to 63e5c29 Compare June 12, 2018 09:54
@sfluor
Copy link
Contributor Author

sfluor commented Jun 12, 2018

Hello !

I just checked and the change also work for docker stack deploy

I did the following:

docker stack deploy -c docker-compose.yml multi

With docker-compose.yml being:

version: "3.5"
services:
  nginx:
    image: nginx:alpine
    ports: 
    - "8080-8081:80"

docker service ls then shows:

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
ra6q1sz8s1fq        multi_nginx         replicated          1/1                 nginx:alpine       *:8080-8081->80/tcp

And the redirection works as expected when doing a curl.

I also squashed both commits 👍.

I would be glad to look at the long syntax issue.

@sfluor sfluor force-pushed the 1074-fix-mapping-a-range-of-host-ports-to-a-single-container-port branch from aaba3fd to 29612cc Compare July 4, 2018 22:33
@docker docker deleted a comment from GordonTheTurtle Jul 5, 2018
Copy link
Collaborator

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🐯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation for mapping a range of host-ports to a single container-port fails
6 participants