Skip to content

Commit

Permalink
Always include Ports within inspect response
Browse files Browse the repository at this point in the history
Related to containerd#3310

New behavior will always initialize a NetworkSettings entity within the inspect response.
If the target container does not exist, the NetworkSettings will be full
of "zero-value" members.
If the target container is active, but ports were not published, then
the Ports key of NetworkSettings will have an empty structure value.
If the target container is active and ports were published, then the
Ports key of NetworkSettings will have populated values.

Signed-off-by: Sam Chew <stchew@amazon.com>
  • Loading branch information
chews93319 committed Aug 15, 2024
1 parent 74f2755 commit ddb910d
Showing 1 changed file with 11 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 @@ -186,7 +186,7 @@ type ContainerState struct {
}

type NetworkSettings struct {
Ports *nat.PortMap `json:",omitempty"`
Ports *nat.PortMap
DefaultNetworkSettings
Networks map[string]*NetworkEndpointSettings
}
Expand Down Expand Up @@ -398,12 +398,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 @@ -447,8 +450,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

0 comments on commit ddb910d

Please sign in to comment.