Skip to content
Open
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
82 changes: 82 additions & 0 deletions cmd/nerdctl/container/container_inspect_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ import (
"slices"
"strings"
"testing"
"time"

"github.com/docker/go-connections/nat"
"gotest.tools/v3/assert"

"github.com/containerd/continuity/testutil/loopback"
"github.com/containerd/nerdctl/mod/tigron/expect"
"github.com/containerd/nerdctl/mod/tigron/require"
"github.com/containerd/nerdctl/mod/tigron/test"
"github.com/containerd/nerdctl/mod/tigron/tig"

"github.com/containerd/nerdctl/v2/pkg/infoutil"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
inspecttypenative "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
"github.com/containerd/nerdctl/v2/pkg/labels"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
Expand Down Expand Up @@ -586,6 +589,85 @@ USER test
testCase.Run(t)
}

func TestContainerInspectGateway(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Have you already checked CI failures related to this fix?

testCase := nerdtest.Setup()

// This test validates nerdctl's inspect conversion path
// Running this against Docker would not use this code path
testCase.Require = require.All(
require.Not(require.Windows),
require.Not(nerdtest.Docker),
nerdtest.Rootful,
)

// isolated test
testCase.NoParallel = true

testCase.Setup = func(data test.Data, helpers test.Helpers) {
helpers.Ensure("network", "create", data.Identifier("net"))

helpers.Ensure("run", "-d",
"--name", data.Identifier("ctr"),
"--network", data.Identifier("net"),
testutil.CommonImage, "sleep", nerdtest.Infinity)

nerdtest.EnsureContainerStarted(helpers, data.Identifier("ctr"))
}

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier("ctr"))
helpers.Anyhow("network", "rm", data.Identifier("net"))
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("container", "inspect", data.Identifier("ctr"))
}

testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Output: func(_ string, t tig.T) {
const (
maxAttempts = 60
wait = 500 * time.Millisecond
)

lastDockerGateway := ""
lastNativeGateway := ""
for i := 0; i < maxAttempts; i++ {
dockerInspect := nerdtest.InspectContainer(helpers, data.Identifier("ctr"))
assert.Assert(t, dockerInspect.NetworkSettings != nil)
lastDockerGateway = dockerInspect.NetworkSettings.Gateway

var nativeInspect []inspecttypenative.Container
helpers.Command("container", "inspect", "--mode", "native", data.Identifier("ctr")).Run(&test.Expected{
Output: expect.JSON([]inspecttypenative.Container{}, func(got []inspecttypenative.Container, t tig.T) {
assert.Equal(t, 1, len(got), "Unexpectedly got multiple results")
nativeInspect = got
}),
})

if len(nativeInspect) == 1 && nativeInspect[0].Process != nil && nativeInspect[0].Process.NetNS != nil {
lastNativeGateway = nativeInspect[0].Process.NetNS.Gateway
}

if lastDockerGateway != "" && lastNativeGateway != "" && lastDockerGateway == lastNativeGateway {
return
}

time.Sleep(wait)
}

assert.Assert(t, lastNativeGateway != "")
assert.Assert(t, lastDockerGateway != "")
assert.Equal(t, lastDockerGateway, lastNativeGateway)
},
}
}

testCase.Run(t)
}

type hostConfigValues struct {
Driver string
ShmSize int64
Expand Down
29 changes: 29 additions & 0 deletions pkg/containerinspector/containerinspector_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/vishvananda/netlink"

"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
)
Expand Down Expand Up @@ -55,6 +56,10 @@ func InspectNetNS(ctx context.Context, pid int) (*native.NetNS, error) {
res.Interfaces[i] = x
}
res.PrimaryInterface = determinePrimaryInterface(res.Interfaces)
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err == nil {
res.Gateway = selectDefaultGateway(routes, res.PrimaryInterface)
}
return nil
}
if err := ns.WithNetNSPath(nsPath, fn); err != nil {
Expand All @@ -73,3 +78,27 @@ func determinePrimaryInterface(interfaces []native.NetInterface) int {
}
return 0
}

func selectDefaultGateway(routes []netlink.Route, primaryIfIndex int) string {
for _, route := range routes {
if route.Dst != nil || route.Gw == nil {
continue
}
if route.Gw.To4() == nil {
continue
}
if route.LinkIndex == primaryIfIndex {
return route.Gw.String()
}
}
for _, route := range routes {
if route.Dst != nil || route.Gw == nil {
continue
}
if route.Gw.To4() == nil {
continue
}
return route.Gw.String()
}
return ""
}
3 changes: 2 additions & 1 deletion pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ type CPUSettings struct {
// DefaultNetworkSettings is from https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L405-L414
type DefaultNetworkSettings struct {
// TODO EndpointID string // EndpointID uniquely represents a service endpoint in a Sandbox
// TODO Gateway string // Gateway holds the gateway address for the network
Gateway string // Gateway holds the gateway address for the network
GlobalIPv6Address string // GlobalIPv6Address holds network's global IPv6 address
GlobalIPv6PrefixLen int // GlobalIPv6PrefixLen represents mask length of network's global IPv6 address
IPAddress string // IPAddress holds the IPv4 address for the network
Expand Down Expand Up @@ -743,6 +743,7 @@ func networkSettingsFromNative(n *native.NetNS, _ *specs.Spec) (*NetworkSettings
}

}
res.DefaultNetworkSettings.Gateway = n.Gateway
if primary != nil {
res.DefaultNetworkSettings.MacAddress = primary.MacAddress
res.DefaultNetworkSettings.IPAddress = primary.IPAddress
Expand Down
8 changes: 8 additions & 0 deletions pkg/inspecttypes/dockercompat/dockercompat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func TestNetworkSettingsFromNative(t *testing.T) {
{
name: "Given NetNS with single Interface with Port Annotation, Return populated NetworkSettings",
n: &native.NetNS{
Gateway: "10.0.4.1",
Interfaces: []native.NetInterface{
{
Interface: net.Interface{
Expand All @@ -427,6 +428,9 @@ func TestNetworkSettingsFromNative(t *testing.T) {
Annotations: map[string]string{},
},
expected: &NetworkSettings{
DefaultNetworkSettings: DefaultNetworkSettings{
Gateway: "10.0.4.1",
},
Ports: &nat.PortMap{
nat.Port("77/tcp"): []nat.PortBinding{
{
Expand All @@ -449,6 +453,7 @@ func TestNetworkSettingsFromNative(t *testing.T) {
{
name: "Given NetNS with single Interface without Port Annotations, Return valid NetworkSettings w/ empty Ports",
n: &native.NetNS{
Gateway: "10.0.4.1",
Interfaces: []native.NetInterface{
{
Interface: net.Interface{
Expand All @@ -467,6 +472,9 @@ func TestNetworkSettingsFromNative(t *testing.T) {
Annotations: map[string]string{},
},
expected: &NetworkSettings{
DefaultNetworkSettings: DefaultNetworkSettings{
Gateway: "10.0.4.1",
},
Ports: &nat.PortMap{},
Networks: map[string]*NetworkEndpointSettings{
"unknown-eth0.100": {
Expand Down
1 change: 1 addition & 0 deletions pkg/inspecttypes/native/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type NetNS struct {
// Zero means unset.
PrimaryInterface int `json:"PrimaryInterface,omitempty"`
Interfaces []NetInterface `json:"Interfaces,omitempty"`
Gateway string `json:"Gateway,omitempty"`
PortMappings []cni.PortMapping
}

Expand Down
Loading