Skip to content

Commit

Permalink
Add default net info in container inspect
Browse files Browse the repository at this point in the history
when inspecting a container that is only connected to the default
network, we should populate the default network in the container inspect
information.

Fixes: containers#6618

Signed-off-by: baude <[email protected]>
  • Loading branch information
baude committed Jan 25, 2021
1 parent b4b7838 commit 13c9db9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion libpod/networking_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

})

0 comments on commit 13c9db9

Please sign in to comment.