mirror of
https://github.com/containers/podman.git
synced 2025-06-23 10:38:20 +08:00
Merge pull request #17386 from rhatdan/network
podman inspect list network when using --net=host or none
This commit is contained in:
@ -240,18 +240,26 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e
|
|||||||
return nil, err
|
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 c.state.NetNS == "" {
|
||||||
if networkNSPath := c.joinedNetworkNSPath(); networkNSPath != "" {
|
if networkNSPath := c.joinedNetworkNSPath(); networkNSPath != "" {
|
||||||
if result, err := c.inspectJoinedNetworkNS(networkNSPath); err == nil {
|
if result, err := c.inspectJoinedNetworkNS(networkNSPath); err == nil {
|
||||||
// fallback to dummy configuration
|
// fallback to dummy configuration
|
||||||
settings.InspectBasicNetworkConfig = resultToBasicNetworkConfig(result)
|
settings.InspectBasicNetworkConfig = resultToBasicNetworkConfig(result)
|
||||||
return settings, nil
|
} else {
|
||||||
}
|
|
||||||
// do not propagate error inspecting a joined network ns
|
// do not propagate error inspecting a joined network ns
|
||||||
logrus.Errorf("Inspecting network namespace: %s of container %s: %v", networkNSPath, c.ID(), err)
|
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 can't do more if the network is down.
|
||||||
|
|
||||||
// We still want to make dummy configurations for each network
|
// We still want to make dummy configurations for each network
|
||||||
// the container joined.
|
// the container joined.
|
||||||
if len(networks) > 0 {
|
if len(networks) > 0 {
|
||||||
@ -262,6 +270,8 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e
|
|||||||
cniNet.Aliases = opts.Aliases
|
cniNet.Aliases = opts.Aliases
|
||||||
settings.Networks[net] = cniNet
|
settings.Networks[net] = cniNet
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
setDefaultNetworks()
|
||||||
}
|
}
|
||||||
|
|
||||||
return settings, nil
|
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)
|
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 {
|
for name, opts := range networks {
|
||||||
result := netStatus[name]
|
result := netStatus[name]
|
||||||
@ -300,6 +310,8 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e
|
|||||||
if !(len(networks) == 1 && isDefaultNet) {
|
if !(len(networks) == 1 && isDefaultNet) {
|
||||||
return settings, nil
|
return settings, nil
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
setDefaultNetworks()
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not joining networks, we should have at most 1 result
|
// If not joining networks, we should have at most 1 result
|
||||||
|
@ -51,7 +51,7 @@ t GET libpod/containers/json?all=true 200 \
|
|||||||
.[0].IsInfra=false
|
.[0].IsInfra=false
|
||||||
|
|
||||||
# Test compat API for Network Settings (.Network is N/A when rootless)
|
# 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
|
if root; then
|
||||||
network_expect="Networks.podman.NetworkID=podman"
|
network_expect="Networks.podman.NetworkID=podman"
|
||||||
fi
|
fi
|
||||||
|
@ -806,4 +806,35 @@ EOF
|
|||||||
assert "$output" =~ "eth0"
|
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
|
# vim: filetype=sh
|
||||||
|
Reference in New Issue
Block a user