Skip to content

Commit

Permalink
Always include Ports within inspect response
Browse files Browse the repository at this point in the history
Related to #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 <[email protected]>
  • Loading branch information
chews93319 committed Aug 15, 2024
1 parent 74f2755 commit 054e158
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
steps:
- name: Set GO env
run: |
# Enable extended globbing features to use advanced pattern matching
shopt -s extglob
# Get latest containerd
args=(curl --proto '=https' --tlsv1.2 -fsSL -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28")
[ "${GITHUB_TOKEN:-}" == "" ] && {
Expand All @@ -59,7 +62,8 @@ jobs:
# Get latest golang version and split it in components
norm=()
while read -r line; do
norm+=("$line")
line_trimmed="${line//+([[:space:]])/}"
norm+=("$line_trimmed")
done < \
<(sed -E 's/^go([0-9]+)[.]([0-9]+)([.]([0-9]+))?(([a-z]+)([0-9]+))?/\1.\2\n\4\n\6\n\7/i' \
<(curl -fsSL "https://go.dev/dl/?mode=json&include=all" | jq -rc .[0].version) \
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ require (
github.com/coreos/go-systemd/v22 v22.5.0
github.com/cyphar/filepath-securejoin v0.3.1
github.com/distribution/reference v0.6.0
github.com/docker/cli v27.1.1+incompatible
github.com/docker/docker v27.1.1+incompatible
github.com/docker/cli v27.1.2+incompatible
github.com/docker/docker v27.1.2+incompatible
github.com/docker/go-connections v0.5.0
github.com/docker/go-units v0.5.0
github.com/fahedouch/go-logrotate v0.2.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5Qvfr
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
github.com/docker/cli v27.1.1+incompatible h1:goaZxOqs4QKxznZjjBWKONQci/MywhtRv2oNn0GkeZE=
github.com/docker/cli v27.1.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/docker v27.1.1+incompatible h1:hO/M4MtV36kzKldqnA37IWhebRA+LnqqcqDja6kVaKY=
github.com/docker/docker v27.1.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/cli v27.1.2+incompatible h1:nYviRv5Y+YAKx3dFrTvS1ErkyVVunKOhoweCTE1BsnI=
github.com/docker/cli v27.1.2+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/docker v27.1.2+incompatible h1:AhGzR1xaQIy53qCkxARaFluI00WPGtXn0AJuoQsVYTY=
github.com/docker/docker v27.1.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker-credential-helpers v0.8.2 h1:bX3YxiGzFP5sOXWc3bTPEXdEaZSeVMrFgOr3T+zrFAo=
github.com/docker/docker-credential-helpers v0.8.2/go.mod h1:P3ci7E3lwkZg6XiHdRKft1KckHiO9a2rNtyFbZ/ry9M=
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
Expand Down
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 054e158

Please sign in to comment.