Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

podman inspect list network when using --net=host or none #17386

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to change the comment to reflect the change

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be removed after the test.

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