From 21651706e3277cbf4d646f7f5eddcf2d9e0e25b6 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Mon, 6 Feb 2023 16:10:20 -0500 Subject: [PATCH] podman inspect list network when using --net=host or none This will match Docker behaviour. Fixes: https://github.com/containers/podman/issues/17385 Signed-off-by: Daniel J Walsh --- libpod/networking_common.go | 22 +++++++++++++++++----- test/apiv2/20-containers.at | 2 +- test/system/500-networking.bats | 31 +++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/libpod/networking_common.go b/libpod/networking_common.go index 816e1d1c08..a569e9eaed 100644 --- a/libpod/networking_common.go +++ b/libpod/networking_common.go @@ -240,18 +240,26 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e return nil, err } + setDefaultNetworks := func() { + settings.Networks = make(map[string]*define.InspectAdditionalNetwork, 1) + name := c.NetworkMode() + addedNet := new(define.InspectAdditionalNetwork) + addedNet.NetworkID = name + settings.Networks[name] = addedNet + } + if c.state.NetNS == "" { if networkNSPath := c.joinedNetworkNSPath(); networkNSPath != "" { if result, err := c.inspectJoinedNetworkNS(networkNSPath); err == nil { // fallback to dummy configuration settings.InspectBasicNetworkConfig = resultToBasicNetworkConfig(result) - return settings, nil + } else { + // do not propagate error inspecting a joined network ns + logrus.Errorf("Inspecting network namespace: %s of container %s: %v", networkNSPath, c.ID(), err) } - // do not propagate error inspecting a joined network ns - logrus.Errorf("Inspecting network namespace: %s of container %s: %v", networkNSPath, c.ID(), err) + return settings, nil } // We can't do more if the network is down. - // We still want to make dummy configurations for each network // the container joined. if len(networks) > 0 { @@ -262,6 +270,8 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e cniNet.Aliases = opts.Aliases settings.Networks[net] = cniNet } + } else { + setDefaultNetworks() } return settings, nil @@ -282,7 +292,7 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e return nil, fmt.Errorf("network inspection mismatch: asked to join %d network(s) %v, but have information on %d network(s): %w", len(networks), networks, len(netStatus), define.ErrInternal) } - settings.Networks = make(map[string]*define.InspectAdditionalNetwork) + settings.Networks = make(map[string]*define.InspectAdditionalNetwork, len(networks)) for name, opts := range networks { result := netStatus[name] @@ -300,6 +310,8 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e if !(len(networks) == 1 && isDefaultNet) { return settings, nil } + } else { + setDefaultNetworks() } // If not joining networks, we should have at most 1 result diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 57adb02ae1..e77a8b09ac 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -51,7 +51,7 @@ t GET libpod/containers/json?all=true 200 \ .[0].IsInfra=false # Test compat API for Network Settings (.Network is N/A when rootless) -network_expect="Networks=null" +network_expect="Networks.slirp4netns.NetworkID=slirp4netns" if root; then network_expect="Networks.podman.NetworkID=podman" fi diff --git a/test/system/500-networking.bats b/test/system/500-networking.bats index 01a30973c2..e20965fd25 100644 --- a/test/system/500-networking.bats +++ b/test/system/500-networking.bats @@ -806,4 +806,35 @@ EOF assert "$output" =~ "eth0" } +@test "podman inspect list networks " { + run_podman create $IMAGE + cid=${output} + run_podman inspect --format '{{ .NetworkSettings.Networks }}' $cid + if is_rootless; then + is "$output" "map\[slirp4netns:.*" "NeworkSettings should contain one network named slirp4netns" + else + is "$output" "map\[podman:.*" "NeworkSettings should contain one network named podman" + fi + run_podman rm $cid + + for network in "host" "none"; do + run_podman create --network=$network $IMAGE + cid=${output} + run_podman inspect --format '{{ .NetworkSettings.Networks }}' $cid + is "$output" "map\[$network:.*" "NeworkSettincs should contain one network named $network" + run_podman rm $cid + done + + # Check with ns:/PATH + if ! is_rootless; then + netns=netns$(random_string) + ip netns add $netns + run_podman create --network=ns:/var/run/netns/$netns $IMAGE + cid=${output} + run_podman inspect --format '{{ .NetworkSettings.Networks }}' $cid + is "$output" 'map[]' "NeworkSettings should be empty" + run_podman rm $cid + fi +} + # vim: filetype=sh