Skip to content

Commit

Permalink
podman inspect list network when using --net=host or none
Browse files Browse the repository at this point in the history
This will match Docker behaviour.

Fixes: #17385

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Mar 8, 2023
1 parent 747369c commit 2165170
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
22 changes: 17 additions & 5 deletions libpod/networking_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -262,6 +270,8 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e
cniNet.Aliases = opts.Aliases
settings.Networks[net] = cniNet
}
} else {
setDefaultNetworks()
}

return settings, nil
Expand All @@ -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]
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/system/500-networking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2165170

Please sign in to comment.