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

Display a warn message when there is binding ports and net mode is host#35510

Merged
merged 1 commit into from
Feb 19, 2018
Merged

Display a warn message when there is binding ports and net mode is host #35510

merged 1 commit into from
Feb 19, 2018

Conversation

boaz0
Copy link
Member

@boaz0 boaz0 commented Nov 15, 2017

- What I did
Fixes #35500

When a container is created if --network is set to host all the ports in the container are bound to the host. Thus, when adding -p or --publish to the command line is meaningless.

Unlike docker run and docker create, docker service create returns an error message when network mode is host and port bindings are given.

- How I did it

This patch however suggests to send a warning message to the client when
such a case occurs.

By adding it to the warnings returned from verifyPlatformContainerSettings

- How to verify it

Added integration test to that so just run make test-integration.

- Description for the changelog

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

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.

Left some comments, but functionally looks to be working :)

$ docker run -dit --name foo --network=host -p 80:80 busybox
WARNING: Setting published IPs is useless when network mode is set to host
4d09a6ebc335a6c89a4c4f812d234d41c4cf6aa9040d8867bd7d852d4f01792b

return verifyPlatformContainerSettings(daemon, hostConfig, config, update)
platformWarns, err := verifyPlatformContainerSettings(daemon, hostConfig, config, update)
if hostConfig.NetworkMode.IsHost() && len(hostConfig.PortBindings) > 0 {
platformWarns = append(platformWarns, "Setting published IPs is useless when network mode is set to host")
Copy link
Member

Choose a reason for hiding this comment

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

LOL, had to laugh a bit about your choice of wording 😄

useless

I'll try to come up with an alternative 👍

@@ -330,5 +330,9 @@ func (daemon *Daemon) verifyContainerSettings(platform string, hostConfig *conta
}

// Now do platform-specific verification
return verifyPlatformContainerSettings(daemon, hostConfig, config, update)
platformWarns, err := verifyPlatformContainerSettings(daemon, hostConfig, config, update)
Copy link
Member

Choose a reason for hiding this comment

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

Can you check the error here, and return? It "looks" safe in this case, but it's best practice to handle errors;

platformWarns, err := verifyPlatformContainerSettings(daemon, hostConfig, config, update)
if err != nil {
    return nil, err
}

Now that platformWarns no longer contains "just" platform-specific warnings, we should probably rename it to something more generic (just warnings?)

@@ -330,5 +330,9 @@ func (daemon *Daemon) verifyContainerSettings(platform string, hostConfig *conta
}

// Now do platform-specific verification
return verifyPlatformContainerSettings(daemon, hostConfig, config, update)
platformWarns, err := verifyPlatformContainerSettings(daemon, hostConfig, config, update)
if hostConfig.NetworkMode.IsHost() && len(hostConfig.PortBindings) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Related to the above; I don't think host networking is supported on Windows (although with LCOW .. perhaps it's still relevant here); I'll have to check that one. If not supported on Windows we should probably move it in verifyPlatformContainerSettings()

Copy link
Member

Choose a reason for hiding this comment

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

ping @johnstep @jhowardmsft could you have a look? ^^

Copy link
Member

Choose a reason for hiding this comment

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

@msabansal Probably one for you to answer. PTAL.

Copy link
Member

@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 🐸

return warnings, err
}
if hostConfig.NetworkMode.IsHost() && len(hostConfig.PortBindings) > 0 {
warnings = append(warnings, "Setting published ports is meaningless when network mode is set to host")
Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Saw you slightly changed the message; thinking a bit more about wording; would this work perhaps;

Published ports are discarded when using host network mode

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, no problem.


// This tests that a warning is returned when setting network mode to host
// and specifying published ports.
func TestCreateWarningHostAndPublish(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

I guess this could be a unit-test instead of an integration test?

@boaz0
Copy link
Member Author

boaz0 commented Feb 15, 2018

ping @thaJeztah @jhowardmsft

@lowenna
Copy link
Member

lowenna commented Feb 15, 2018

Seems OK, but I'd like @msabansal from the networking team here to confirm.

@msabansal
Copy link
Contributor

LGTM.

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

"github.com/stretchr/testify/require"
)

// This tests that a warning is returned when setting network mode to host and specifying published ports.
Copy link
Member

Choose a reason for hiding this comment

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

nit: could use Godoc conventions: s/This tests/TestContainerWarningHostAndPublishPorts tests/

@vdemeester
Copy link
Member

daemon/container_test.go:1::warning: file is not gofmted with -s (gofmt)

@codecov
Copy link

codecov bot commented Feb 18, 2018

Codecov Report

Merging #35510 into master will increase coverage by 0.04%.
The diff coverage is 71.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #35510      +/-   ##
==========================================
+ Coverage   34.27%   34.32%   +0.04%     
==========================================
  Files         609      611       +2     
  Lines       45287    45489     +202     
==========================================
+ Hits        15523    15612      +89     
- Misses      27816    27870      +54     
- Partials     1948     2007      +59
Impacted Files Coverage Δ
daemon/container.go 25.27% <71.42%> (+5.73%) ⬆️
pkg/discovery/file/file.go 90.38% <0%> (-5.77%) ⬇️
restartmanager/restartmanager.go 33.33% <0%> (-3.18%) ⬇️
daemon/cluster/executor/container/adapter.go 9.16% <0%> (-0.84%) ⬇️
builder/fscache/fscache.go 58.84% <0%> (-0.58%) ⬇️
daemon/graphdriver/devmapper/deviceset.go 39.85% <0%> (-0.21%) ⬇️
vendor/github.com/docker/distribution/errors.go 21.42% <0%> (ø)
vendor/github.com/docker/distribution/registry.go 52.08% <0%> (ø)
daemon/daemon.go 4.5% <0%> (+0.15%) ⬆️
pkg/archive/archive.go 68.05% <0%> (+0.63%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c5e7537...6e78fdb. Read the comment docs.

When a container is created if "--network" is set to "host" all the
ports in the container are bound to the host.
Thus, adding "-p" or "--publish" to the command-line is meaningless.

Unlike "docker run" and "docker create", "docker service create" sends
an error message when network mode is host and port bindings are given

This patch however suggests to send a warning message to the client when
such a case occurs.

The warning message is added to "warnings" which are returned from
"verifyPlatformContainerSettings".

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
@vdemeester vdemeester merged commit 35d69f1 into moby:master Feb 19, 2018
@boaz0
Copy link
Member Author

boaz0 commented Feb 19, 2018

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.

7 participants