Skip to content

Commit

Permalink
Include init NetworkSettings within inspect response
Browse files Browse the repository at this point in the history
Related to
[#3310](#3310)

New behavior will always initialize a NetworkSettings entity
for the inspect response, including Ports child member.

If inspecting a running container with published ports,
then all information will be correctly returned.
If inspecting a running container without published ports,
then NetworkSettings will include an initialized Ports member.
If inspecting a stopped/exited container,
then an entirely initialized, "empty-value" NetworkSettings
is returned.

Signed-off-by: Sam Chew <[email protected]>
  • Loading branch information
chews93319 authored and Shubhranshu153 committed Sep 8, 2024
1 parent d97ca7d commit d302774
Show file tree
Hide file tree
Showing 2 changed files with 374 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type ContainerState struct {
}

type NetworkSettings struct {
Ports *nat.PortMap `json:",omitempty"`
Ports *nat.PortMap
DefaultNetworkSettings
Networks map[string]*NetworkEndpointSettings
}
Expand Down Expand Up @@ -337,12 +337,15 @@ func statusFromNative(x containerd.Status, labels map[string]string) string {
}

func networkSettingsFromNative(n *native.NetNS, sp *specs.Spec) (*NetworkSettings, error) {
if n == nil {
return nil, nil
}
res := &NetworkSettings{
Networks: make(map[string]*NetworkEndpointSettings),
}
resPortMap := make(nat.PortMap)
res.Ports = &resPortMap
if n == nil {
return res, nil
}

var primary *NetworkEndpointSettings
for _, x := range n.Interfaces {
if x.Interface.Flags&net.FlagLoopback != 0 {
Expand Down Expand Up @@ -386,8 +389,11 @@ func networkSettingsFromNative(n *native.NetNS, sp *specs.Spec) (*NetworkSetting
if err != nil {
return nil, err
}
res.Ports = nports
for portLabel, portBindings := range *nports {
resPortMap[portLabel] = portBindings
}
}

if x.Index == n.PrimaryInterface {
primary = nes
}
Expand Down
Loading

0 comments on commit d302774

Please sign in to comment.