From 13c9db98d453b6912608e1f6c1867124b6e0adbb Mon Sep 17 00:00:00 2001 From: baude Date: Mon, 25 Jan 2021 13:58:04 -0600 Subject: [PATCH] Add default net info in container inspect when inspecting a container that is only connected to the default network, we should populate the default network in the container inspect information. Fixes: #6618 Signed-off-by: baude --- libpod/networking_linux.go | 9 ++++++++- test/e2e/inspect_test.go | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/libpod/networking_linux.go b/libpod/networking_linux.go index ef2f034abc..76ae342dae 100644 --- a/libpod/networking_linux.go +++ b/libpod/networking_linux.go @@ -973,6 +973,13 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e return nil, err } + // If this a container with a netns and on the default network + if c.state.NetNS != nil && isDefault { + settings.Networks = make(map[string]*define.InspectAdditionalNetwork, len(networks)) + cniNet := new(define.InspectAdditionalNetwork) + cniNet.NetworkID = c.runtime.netPlugin.GetDefaultNetworkName() + settings.Networks[c.runtime.netPlugin.GetDefaultNetworkName()] = cniNet + } // We can't do more if the network is down. if c.state.NetNS == nil { // We still want to make dummy configurations for each CNI net @@ -998,7 +1005,7 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e } // If we have CNI networks - handle that here - if len(networks) > 0 && !isDefault { + if len(networks) > 0 { if len(networks) != len(c.state.NetworkStatus) { return nil, errors.Wrapf(define.ErrInternal, "network inspection mismatch: asked to join %d CNI network(s) %v, but have information on %d network(s)", len(networks), networks, len(c.state.NetworkStatus)) } diff --git a/test/e2e/inspect_test.go b/test/e2e/inspect_test.go index 97f77414eb..1f54e2a369 100644 --- a/test/e2e/inspect_test.go +++ b/test/e2e/inspect_test.go @@ -443,4 +443,16 @@ var _ = Describe("Podman inspect", func() { Expect(inspect.OutputToString()).To(Equal(`"{"80/tcp":[{"HostIp":"","HostPort":"8080"}]}"`)) }) + It("Verify container inspect has default network", func() { + session := podmanTest.Podman([]string{"run", "-dt", ALPINE, "top"}) + session.WaitWithDefaultTimeout() + Expect(session.ExitCode()).To(BeZero()) + cid := session.OutputToString() + inspect := podmanTest.Podman([]string{"inspect", cid}) + inspect.WaitWithDefaultTimeout() + Expect(inspect.ExitCode()).To(BeZero()) + data := inspect.InspectContainerToJSON() + Expect(len(data[0].NetworkSettings.Networks)).To(BeNumerically(">", 0)) + }) + })