Merge pull request #17386 from rhatdan/network

podman inspect list network when using --net=host or none
This commit is contained in:
OpenShift Merge Robot
2023-03-09 14:07:37 -05:00
committed by GitHub
3 changed files with 49 additions and 6 deletions

View File

@ -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)
}
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

View File

@ -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

View File

@ -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