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

Network connect error if net mode is not bridge #9497

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libpod/networking_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,11 @@ func (w *logrusDebugWriter) Write(p []byte) (int, error) {

// NetworkDisconnect removes a container from the network
func (c *Container) NetworkDisconnect(nameOrID, netName string, force bool) error {
// only the bridge mode supports cni networks
if !c.config.NetMode.IsBridge() {
return errors.Errorf("network mode %q is not supported", c.config.NetMode)
}

networks, err := c.networksByNameIndex()
if err != nil {
return err
Expand Down Expand Up @@ -1190,6 +1195,11 @@ func (c *Container) NetworkDisconnect(nameOrID, netName string, force bool) erro

// ConnectNetwork connects a container to a given network
func (c *Container) NetworkConnect(nameOrID, netName string, aliases []string) error {
// only the bridge mode supports cni networks
if !c.config.NetMode.IsBridge() {
return errors.Errorf("network mode %q is not supported", c.config.NetMode)
}

networks, err := c.networksByNameIndex()
if err != nil {
return err
Expand Down
38 changes: 36 additions & 2 deletions test/e2e/network_connect_disconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var _ = Describe("Podman network connect and disconnect", func() {
dis := podmanTest.Podman([]string{"network", "disconnect", "foobar", "test"})
dis.WaitWithDefaultTimeout()
Expect(dis.ExitCode()).ToNot(BeZero())

})

It("bad container name in network disconnect should result in error", func() {
Expand All @@ -51,7 +50,25 @@ var _ = Describe("Podman network connect and disconnect", func() {
dis := podmanTest.Podman([]string{"network", "disconnect", netName, "foobar"})
dis.WaitWithDefaultTimeout()
Expect(dis.ExitCode()).ToNot(BeZero())
})

It("network disconnect with net mode slirp4netns should result in error", func() {
SkipIfRootless("network connect and disconnect are only rootful")
netName := "slirp" + stringid.GenerateNonCryptoID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(netName)

session = podmanTest.Podman([]string{"create", "--name", "test", "--network", "slirp4netns", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(netName)

con := podmanTest.Podman([]string{"network", "disconnect", netName, "test"})
con.WaitWithDefaultTimeout()
Expect(con.ExitCode()).ToNot(BeZero())
Expect(con.ErrorToString()).To(ContainSubstring(`network mode "slirp4netns" is not supported`))
})

It("podman network disconnect", func() {
Expand Down Expand Up @@ -89,7 +106,6 @@ var _ = Describe("Podman network connect and disconnect", func() {
dis := podmanTest.Podman([]string{"network", "connect", "foobar", "test"})
dis.WaitWithDefaultTimeout()
Expect(dis.ExitCode()).ToNot(BeZero())

})

It("bad container name in network connect should result in error", func() {
Expand All @@ -103,7 +119,25 @@ var _ = Describe("Podman network connect and disconnect", func() {
dis := podmanTest.Podman([]string{"network", "connect", netName, "foobar"})
dis.WaitWithDefaultTimeout()
Expect(dis.ExitCode()).ToNot(BeZero())
})

It("network connect with net mode slirp4netns should result in error", func() {
SkipIfRootless("network connect and disconnect are only rootful")
netName := "slirp" + stringid.GenerateNonCryptoID()
session := podmanTest.Podman([]string{"network", "create", netName})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(netName)

session = podmanTest.Podman([]string{"create", "--name", "test", "--network", "slirp4netns", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(netName)

con := podmanTest.Podman([]string{"network", "connect", netName, "test"})
con.WaitWithDefaultTimeout()
Expect(con.ExitCode()).ToNot(BeZero())
Expect(con.ErrorToString()).To(ContainSubstring(`network mode "slirp4netns" is not supported`))
})

It("podman connect on a container that already is connected to the network should error", func() {
Expand Down