Skip to content

Comments

add MAC, IPv4, IPv6 addresses to nework inspect#4680

Open
coderbirju wants to merge 1 commit intocontainerd:mainfrom
coderbirju:add-details-network-inspect
Open

add MAC, IPv4, IPv6 addresses to nework inspect#4680
coderbirju wants to merge 1 commit intocontainerd:mainfrom
coderbirju:add-details-network-inspect

Conversation

@coderbirju
Copy link
Contributor

@coderbirju coderbirju commented Jan 7, 2026

Current network inspect respone provides only container names
This PR adds other fields in the EndpointResource Struct

Testing:

CIDR Notation:

$ sudo nerdctl network create test-net --subnet 10.88.0.0/16
$ sudo nerdctl run -d --name test-container --network test-net nginx:alpine
$ sudo nerdctl network inspect test-net | jq -r '.[].Containers'

Result

{
  "70aaec372b1f...": {
    "Name": "test-container",
    "MacAddress": "fe:5d:85:64:d2:96",
    "IPv4Address": "10.88.0.2/16", 
    "IPv6Address": ""
  }
}

Multi-Network Container:

$ sudo nerdctl network create hoge --subnet 10.91.0.0/16
$ sudo nerdctl network create fuga --subnet 10.92.0.0/16
$ sudo nerdctl run -d --name test2 --network hoge --network fuga nginx:alpine

-hoge

$ sudo nerdctl network inspect hoge | jq -r '.[].Containers'

{
  "0d7e17a42d25...": {
    "Name": "test2",
    "MacAddress": "ca:ad:6d:42:7a:03",  ← Unique to hoge
    "IPv4Address": "10.91.0.3/16",      ← From hoge subnet
    "IPv6Address": ""
  }
}

-fuga

$ sudo nerdctl network inspect fuga | jq -r '.[].Containers'


{
  "0d7e17a42d25...": {
    "Name": "test2",
    "MacAddress": "ca:f2:87:2c:4e:df",  ← Different MAC
    "IPv4Address": "10.92.0.2/16",      ← Different IP from fuga subnet
    "IPv6Address": ""
  }
}

@coderbirju coderbirju closed this Jan 7, 2026
@coderbirju coderbirju reopened this Jan 7, 2026
@coderbirju coderbirju force-pushed the add-details-network-inspect branch from 2385db1 to eb356cf Compare January 9, 2026 00:27
@coderbirju coderbirju force-pushed the add-details-network-inspect branch from eb356cf to 365205b Compare January 21, 2026 22:35
@coderbirju
Copy link
Contributor Author

Apologies for the late reply, you're right we can skip the assert statements as the field existence is already validated through struct unmarshaling.

@haytok

@coderbirju coderbirju closed this Jan 29, 2026
@coderbirju coderbirju reopened this Jan 29, 2026
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("network", "inspect", data.Identifier("test-network"))
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
Copy link
Member

Choose a reason for hiding this comment

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

There are no tests for the items added in this PR's changes. Should add assertions for IPv4Address, MacAddress, and IPv6Address.

@AkihiroSuda AkihiroSuda added this to the v2.3.0 milestone Feb 4, 2026
Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com>
@coderbirju coderbirju force-pushed the add-details-network-inspect branch from 365205b to 0756765 Compare February 9, 2026 23:01
@coderbirju coderbirju requested a review from haytok February 10, 2026 20:06
@coderbirju
Copy link
Contributor Author

Failures seem to be unrelated to the changes
PTAL
@haytok @Shubhranshu153

@haytok
Copy link
Member

haytok commented Feb 18, 2026

Thanks for fixes. I'll reply within this week 🙏

Copy link
Member

@haytok haytok left a comment

Choose a reason for hiding this comment

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

I've added some comments but, can we add a test for when a container belongs to two networks?

(There were a few points I wasn’t able to comment on in my previous review. I apologize for the inconvenience 🙏)

continue
}

if len(subnets) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

What situations can lead to an empty subnets list?


// Test IPv4Address has CIDR notation (not empty and contains '/')
assert.Assert(t, container.IPv4Address != "", "IPv4Address should not be empty")
assert.Assert(t, strings.Contains(container.IPv4Address, "/"), "IPv4Address should contain CIDR notation with /")
Copy link
Member

Choose a reason for hiding this comment

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

IMO, it would be better to verify that the container IP is within the subnet, in addition to checking that container.IPv4Address contains /.

Example:

_, subnet, _ := net.ParseCIDR(dc[0].IPAM.Config[0].Subnet)
containerIP, _, _ := net.ParseCIDR(container.IPv4Address)
assert.Assert(t, subnet.Contains(containerIP), "IPv4Address should be within the network's subnet")

Comment on lines +447 to +453
assert.Assert(t, strings.Contains(container.IPv4Address, "/"), "IPv4Address should contain CIDR notation with /")

// Test MacAddress is present and has valid format
assert.Assert(t, container.MacAddress != "", "MacAddress should not be empty")

// Test IPv6Address is empty for IPv4-only network
assert.Equal(t, "", container.IPv6Address, "IPv6Address should be empty for IPv4-only network")
Copy link
Member

Choose a reason for hiding this comment

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

When checking CI logs, there are below messages:

network_inspect_test.go:446: assertion failed: container.IPv4Address is "": IPv4Address should not be empty

This is because nerdctl network inspect on windows environments does not return NetNS information.

func InspectNetNS(ctx context.Context, pid int) (*native.NetNS, error) {
r := &native.NetNS{}
return r, nil
}

if subnet.Contains(ip) {
endpoint.MacAddress = iface.HardwareAddr
setIPAddresses(endpoint, addr)
return true
Copy link
Member

Choose a reason for hiding this comment

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

In the current implementation, when a container belongs to a dual-stack network with both IPv4 and IPv6, only one IP address is displayed. Both IPv4 and IPv6 addresses should be displayed.

sudo nerdctl network create --ipv6 --subnet 10.1.0.0/24 --subnet fd00::/64 test-dual-stack
sudo nerdctl run -d --name test-container --network test-dual-stack alpine sleep infinity
sudo nerdctl network inspect test-dual-stack | jq -r '.[].Containers'

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 here we are returning true but we are parsing a range of subnets.

return subnets
}

// isUsableInterface checks if a network interface is usable (not loopback and up)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: and interface is up.

func matchInterfaceToSubnets(endpoint *EndpointResource, iface *native.NetInterface, subnets []*net.IPNet) bool {
for _, addr := range iface.Addrs {
ip, _, err := net.ParseCIDR(addr)
if err != nil || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: handle error and other conditional separately. Not important but the purpose is different.

// IPv4Address string `json:"IPv4Address"`
// IPv6Address string `json:"IPv6Address"`
MacAddress string `json:"MacAddress"`
IPv4Address string `json:"IPv4Address"`
Copy link
Contributor

Choose a reason for hiding this comment

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

we are parsing a range of subnets but we are supposed take the first IPV4 and one IPV6 address i think for docker compatibility, how that is decided?

// IPv6Address string `json:"IPv6Address"`
MacAddress string `json:"MacAddress"`
IPv4Address string `json:"IPv4Address"`
IPv6Address string `json:"IPv6Address"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants