-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Fix mapping a range of host ports to a single container port #1102
Conversation
opts/port.go
Outdated
@@ -148,20 +148,31 @@ func ConvertPortToPortConfig( | |||
ports := []swarm.PortConfig{} | |||
|
|||
for _, binding := range portBindings[port] { | |||
hostPorts := []uint64{} |
There was a problem hiding this comment.
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?
94c7ffe
to
9d44556
Compare
Hello 😄 ! I just removed the for loop as you suggested and added test cases for this behavior. |
opts/port_test.go
Outdated
}, | ||
}, | ||
{ | ||
value: "81-83:9999/tcp", |
There was a problem hiding this comment.
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").
9d44556
to
35380a9
Compare
I just removed the redundant test ^^ |
There was a problem hiding this 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] { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remove it 👍
35380a9
to
a71fc9e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Just thinking of this (haven't tried), but does this also work for @sfluor could you squash your commits? I think its ok to have the tests and code-changes in the same commit. |
Oh, and we should check if we can / want to support this notation for the long syntax; moby/moby#32551 |
Signed-off-by: Sami Tabet <[email protected]>
a71fc9e
to
63e5c29
Compare
Hello ! I just checked and the change also work for I did the following:
With docker-compose.yml being: version: "3.5"
services:
nginx:
image: nginx:alpine
ports:
- "8080-8081:80"
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. |
Signed-off-by: Sami Tabet <[email protected]>
aaba3fd
to
29612cc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🐯
- 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)