Skip to content

Commit ec66442

Browse files
committed
Add gateway to docker inspect output
Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> add test TestContainerInspectGateway Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> Fix test issues Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> Remove container.test Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> Resolve review and tests Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> feature: populate NetworkSettings.Gateway and add e2e coverage Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> Resolve test issues Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> feature: populate NetworkSettings.Gateway and add e2e coverage Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> drop unrelated hostsstore changes Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev> Change testing feature: populate NetworkSettings.Gateway and add e2e coverage Resolve breaking tests Add gateway to docker inspect output Signed-off-by: Mustaeen Ahmed <contact@mustaeen.dev>
1 parent 1f6e874 commit ec66442

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import (
2323
"slices"
2424
"strings"
2525
"testing"
26+
"time"
2627

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

3031
"github.com/containerd/continuity/testutil/loopback"
3132
"github.com/containerd/nerdctl/mod/tigron/expect"
33+
"github.com/containerd/nerdctl/mod/tigron/require"
3234
"github.com/containerd/nerdctl/mod/tigron/test"
3335
"github.com/containerd/nerdctl/mod/tigron/tig"
3436

@@ -586,6 +588,83 @@ USER test
586588
testCase.Run(t)
587589
}
588590

591+
func TestContainerInspectGateway(t *testing.T) {
592+
testCase := nerdtest.Setup()
593+
594+
// This test validates nerdctl's inspect conversion path
595+
// Running this against Docker would not use this code path
596+
testCase.Require = require.All(
597+
require.Not(require.Windows),
598+
require.Not(nerdtest.Docker),
599+
nerdtest.Rootful,
600+
)
601+
602+
// isolated test
603+
testCase.NoParallel = true
604+
605+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
606+
helpers.Ensure("network", "create", data.Identifier("net"))
607+
608+
helpers.Ensure("run", "-d",
609+
"--name", data.Identifier("ctr"),
610+
"--network", data.Identifier("net"),
611+
testutil.CommonImage, "sleep", nerdtest.Infinity)
612+
613+
nerdtest.EnsureContainerStarted(helpers, data.Identifier("ctr"))
614+
}
615+
616+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
617+
helpers.Anyhow("rm", "-f", data.Identifier("ctr"))
618+
helpers.Anyhow("network", "rm", data.Identifier("net"))
619+
}
620+
621+
testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
622+
return helpers.Command("container", "inspect", data.Identifier("ctr"))
623+
}
624+
625+
testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected {
626+
return &test.Expected{
627+
ExitCode: 0,
628+
Output: func(_ string, t tig.T) {
629+
const (
630+
maxAttempts = 60
631+
wait = 500 * time.Millisecond
632+
)
633+
634+
// if network metadata lags, resolve expected gateway with retries
635+
expectedGateway := ""
636+
for i := 0; i < maxAttempts; i++ {
637+
netInspect := nerdtest.InspectNetwork(helpers, data.Identifier("net"))
638+
if len(netInspect.IPAM.Config) > 0 && netInspect.IPAM.Config[0].Gateway != "" {
639+
expectedGateway = netInspect.IPAM.Config[0].Gateway
640+
break
641+
}
642+
643+
time.Sleep(wait)
644+
}
645+
assert.Assert(t, expectedGateway != "")
646+
647+
lastGateway := ""
648+
for i := 0; i < maxAttempts; i++ {
649+
ctrInspect := nerdtest.InspectContainer(helpers, data.Identifier("ctr"))
650+
assert.Assert(t, ctrInspect.NetworkSettings != nil)
651+
652+
lastGateway = ctrInspect.NetworkSettings.Gateway
653+
if lastGateway == expectedGateway {
654+
return
655+
}
656+
657+
time.Sleep(wait)
658+
}
659+
660+
assert.Equal(t, lastGateway, expectedGateway)
661+
},
662+
}
663+
}
664+
665+
testCase.Run(t)
666+
}
667+
589668
type hostConfigValues struct {
590669
Driver string
591670
ShmSize int64

pkg/containerinspector/containerinspector_linux.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/containernetworking/plugins/pkg/ns"
26+
"github.com/vishvananda/netlink"
2627

2728
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
2829
)
@@ -55,6 +56,10 @@ func InspectNetNS(ctx context.Context, pid int) (*native.NetNS, error) {
5556
res.Interfaces[i] = x
5657
}
5758
res.PrimaryInterface = determinePrimaryInterface(res.Interfaces)
59+
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
60+
if err == nil {
61+
res.Gateway = selectDefaultGateway(routes, res.PrimaryInterface)
62+
}
5863
return nil
5964
}
6065
if err := ns.WithNetNSPath(nsPath, fn); err != nil {
@@ -73,3 +78,27 @@ func determinePrimaryInterface(interfaces []native.NetInterface) int {
7378
}
7479
return 0
7580
}
81+
82+
func selectDefaultGateway(routes []netlink.Route, primaryIfIndex int) string {
83+
for _, route := range routes {
84+
if route.Dst != nil || route.Gw == nil {
85+
continue
86+
}
87+
if route.Gw.To4() == nil {
88+
continue
89+
}
90+
if route.LinkIndex == primaryIfIndex {
91+
return route.Gw.String()
92+
}
93+
}
94+
for _, route := range routes {
95+
if route.Dst != nil || route.Gw == nil {
96+
continue
97+
}
98+
if route.Gw.To4() == nil {
99+
continue
100+
}
101+
return route.Gw.String()
102+
}
103+
return ""
104+
}

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ type CPUSettings struct {
281281
// DefaultNetworkSettings is from https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L405-L414
282282
type DefaultNetworkSettings struct {
283283
// TODO EndpointID string // EndpointID uniquely represents a service endpoint in a Sandbox
284-
// TODO Gateway string // Gateway holds the gateway address for the network
284+
Gateway string // Gateway holds the gateway address for the network
285285
GlobalIPv6Address string // GlobalIPv6Address holds network's global IPv6 address
286286
GlobalIPv6PrefixLen int // GlobalIPv6PrefixLen represents mask length of network's global IPv6 address
287287
IPAddress string // IPAddress holds the IPv4 address for the network
@@ -743,6 +743,7 @@ func networkSettingsFromNative(n *native.NetNS, _ *specs.Spec) (*NetworkSettings
743743
}
744744

745745
}
746+
res.DefaultNetworkSettings.Gateway = n.Gateway
746747
if primary != nil {
747748
res.DefaultNetworkSettings.MacAddress = primary.MacAddress
748749
res.DefaultNetworkSettings.IPAddress = primary.IPAddress

pkg/inspecttypes/dockercompat/dockercompat_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ func TestNetworkSettingsFromNative(t *testing.T) {
401401
{
402402
name: "Given NetNS with single Interface with Port Annotation, Return populated NetworkSettings",
403403
n: &native.NetNS{
404+
Gateway: "10.0.4.1",
404405
Interfaces: []native.NetInterface{
405406
{
406407
Interface: net.Interface{
@@ -427,6 +428,9 @@ func TestNetworkSettingsFromNative(t *testing.T) {
427428
Annotations: map[string]string{},
428429
},
429430
expected: &NetworkSettings{
431+
DefaultNetworkSettings: DefaultNetworkSettings{
432+
Gateway: "10.0.4.1",
433+
},
430434
Ports: &nat.PortMap{
431435
nat.Port("77/tcp"): []nat.PortBinding{
432436
{
@@ -449,6 +453,7 @@ func TestNetworkSettingsFromNative(t *testing.T) {
449453
{
450454
name: "Given NetNS with single Interface without Port Annotations, Return valid NetworkSettings w/ empty Ports",
451455
n: &native.NetNS{
456+
Gateway: "10.0.4.1",
452457
Interfaces: []native.NetInterface{
453458
{
454459
Interface: net.Interface{
@@ -467,6 +472,9 @@ func TestNetworkSettingsFromNative(t *testing.T) {
467472
Annotations: map[string]string{},
468473
},
469474
expected: &NetworkSettings{
475+
DefaultNetworkSettings: DefaultNetworkSettings{
476+
Gateway: "10.0.4.1",
477+
},
470478
Ports: &nat.PortMap{},
471479
Networks: map[string]*NetworkEndpointSettings{
472480
"unknown-eth0.100": {

pkg/inspecttypes/native/container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type NetNS struct {
4444
// Zero means unset.
4545
PrimaryInterface int `json:"PrimaryInterface,omitempty"`
4646
Interfaces []NetInterface `json:"Interfaces,omitempty"`
47+
Gateway string `json:"Gateway,omitempty"`
4748
PortMappings []cni.PortMapping
4849
}
4950

0 commit comments

Comments
 (0)