Skip to content

Commit b54de25

Browse files
authored
Merge pull request #3318 from chews93319/main
Include init NetworkSettings within inspect response
2 parents a44a1cb + adf4035 commit b54de25

File tree

2 files changed

+139
-7
lines changed

2 files changed

+139
-7
lines changed

pkg/inspecttypes/dockercompat/dockercompat.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ type ContainerState struct {
186186
}
187187

188188
type NetworkSettings struct {
189-
Ports *nat.PortMap `json:",omitempty"`
189+
Ports *nat.PortMap
190190
DefaultNetworkSettings
191191
Networks map[string]*NetworkEndpointSettings
192192
}
@@ -398,12 +398,15 @@ func statusFromNative(x containerd.Status, labels map[string]string) string {
398398
}
399399

400400
func networkSettingsFromNative(n *native.NetNS, sp *specs.Spec) (*NetworkSettings, error) {
401-
if n == nil {
402-
return nil, nil
403-
}
404401
res := &NetworkSettings{
405402
Networks: make(map[string]*NetworkEndpointSettings),
406403
}
404+
resPortMap := make(nat.PortMap)
405+
res.Ports = &resPortMap
406+
if n == nil {
407+
return res, nil
408+
}
409+
407410
var primary *NetworkEndpointSettings
408411
for _, x := range n.Interfaces {
409412
if x.Interface.Flags&net.FlagLoopback != 0 {
@@ -447,8 +450,11 @@ func networkSettingsFromNative(n *native.NetNS, sp *specs.Spec) (*NetworkSetting
447450
if err != nil {
448451
return nil, err
449452
}
450-
res.Ports = nports
453+
for portLabel, portBindings := range *nports {
454+
resPortMap[portLabel] = portBindings
455+
}
451456
}
457+
452458
if x.Index == n.PrimaryInterface {
453459
primary = nes
454460
}

pkg/inspecttypes/dockercompat/dockercompat_test.go

+128-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package dockercompat
1818

1919
import (
20+
"net"
2021
"os"
2122
"path/filepath"
2223
"runtime"
2324
"testing"
2425

26+
"github.com/docker/go-connections/nat"
2527
"github.com/opencontainers/runtime-spec/specs-go"
2628
"gotest.tools/v3/assert"
2729

@@ -91,6 +93,10 @@ func TestContainerFromNative(t *testing.T) {
9193
},
9294
Hostname: "host1",
9395
},
96+
NetworkSettings: &NetworkSettings{
97+
Ports: &nat.PortMap{},
98+
Networks: map[string]*NetworkEndpointSettings{},
99+
},
94100
},
95101
},
96102
// cri container, mount /mnt/foo:/mnt/foo:rw,rslave; mount resolv.conf and hostname; internal sysfs mount
@@ -172,6 +178,10 @@ func TestContainerFromNative(t *testing.T) {
172178
// ignore sysfs mountpoint
173179
},
174180
Config: &Config{},
181+
NetworkSettings: &NetworkSettings{
182+
Ports: &nat.PortMap{},
183+
Networks: map[string]*NetworkEndpointSettings{},
184+
},
175185
},
176186
},
177187
// ctr container, mount /mnt/foo:/mnt/foo:rw,rslave; internal sysfs mount; hostname
@@ -226,12 +236,128 @@ func TestContainerFromNative(t *testing.T) {
226236
Config: &Config{
227237
Hostname: "host1",
228238
},
239+
NetworkSettings: &NetworkSettings{
240+
Ports: &nat.PortMap{},
241+
Networks: map[string]*NetworkEndpointSettings{},
242+
},
243+
},
244+
},
245+
}
246+
247+
for _, tc := range testcase {
248+
t.Run(tc.name, func(tt *testing.T) {
249+
d, _ := ContainerFromNative(tc.n)
250+
assert.DeepEqual(tt, d, tc.expected)
251+
})
252+
}
253+
}
254+
255+
func TestNetworkSettingsFromNative(t *testing.T) {
256+
tempStateDir, err := os.MkdirTemp(t.TempDir(), "rw")
257+
if err != nil {
258+
t.Fatal(err)
259+
}
260+
os.WriteFile(filepath.Join(tempStateDir, "resolv.conf"), []byte(""), 0644)
261+
defer os.RemoveAll(tempStateDir)
262+
263+
testcase := []struct {
264+
name string
265+
n *native.NetNS
266+
s *specs.Spec
267+
expected *NetworkSettings
268+
}{
269+
// Given null native.NetNS, Return initialized NetworkSettings
270+
// UseCase: Inspect a Stopped Container
271+
{
272+
name: "Given Null NetNS, Return initialized NetworkSettings",
273+
n: nil,
274+
s: &specs.Spec{},
275+
expected: &NetworkSettings{
276+
Ports: &nat.PortMap{},
277+
Networks: map[string]*NetworkEndpointSettings{},
278+
},
279+
},
280+
// Given native.NetNS with single Interface with Port Annotations, Return populated NetworkSettings
281+
// UseCase: Inspect a Running Container with published ports
282+
{
283+
name: "Given NetNS with single Interface with Port Annotation, Return populated NetworkSettings",
284+
n: &native.NetNS{
285+
Interfaces: []native.NetInterface{
286+
{
287+
Interface: net.Interface{
288+
Index: 1,
289+
MTU: 1500,
290+
Name: "eth0.100",
291+
Flags: net.FlagUp,
292+
},
293+
HardwareAddr: "xx:xx:xx:xx:xx:xx",
294+
Flags: []string{},
295+
Addrs: []string{"10.0.4.30/24"},
296+
},
297+
},
298+
},
299+
s: &specs.Spec{
300+
Annotations: map[string]string{
301+
"nerdctl/ports": "[{\"HostPort\":8075,\"ContainerPort\":77,\"Protocol\":\"tcp\",\"HostIP\":\"127.0.0.1\"}]",
302+
},
303+
},
304+
expected: &NetworkSettings{
305+
Ports: &nat.PortMap{
306+
nat.Port("77/tcp"): []nat.PortBinding{
307+
{
308+
HostIP: "127.0.0.1",
309+
HostPort: "8075",
310+
},
311+
},
312+
},
313+
Networks: map[string]*NetworkEndpointSettings{
314+
"unknown-eth0.100": {
315+
IPAddress: "10.0.4.30",
316+
IPPrefixLen: 24,
317+
MacAddress: "xx:xx:xx:xx:xx:xx",
318+
},
319+
},
320+
},
321+
},
322+
// Given native.NetNS with single Interface without Port Annotations, Return valid NetworkSettings w/ empty Ports
323+
// UseCase: Inspect a Running Container without published ports
324+
{
325+
name: "Given NetNS with single Interface without Port Annotations, Return valid NetworkSettings w/ empty Ports",
326+
n: &native.NetNS{
327+
Interfaces: []native.NetInterface{
328+
{
329+
Interface: net.Interface{
330+
Index: 1,
331+
MTU: 1500,
332+
Name: "eth0.100",
333+
Flags: net.FlagUp,
334+
},
335+
HardwareAddr: "xx:xx:xx:xx:xx:xx",
336+
Flags: []string{},
337+
Addrs: []string{"10.0.4.30/24"},
338+
},
339+
},
340+
},
341+
s: &specs.Spec{
342+
Annotations: map[string]string{},
343+
},
344+
expected: &NetworkSettings{
345+
Ports: &nat.PortMap{},
346+
Networks: map[string]*NetworkEndpointSettings{
347+
"unknown-eth0.100": {
348+
IPAddress: "10.0.4.30",
349+
IPPrefixLen: 24,
350+
MacAddress: "xx:xx:xx:xx:xx:xx",
351+
},
352+
},
229353
},
230354
},
231355
}
232356

233357
for _, tc := range testcase {
234-
d, _ := ContainerFromNative(tc.n)
235-
assert.DeepEqual(t, d, tc.expected)
358+
t.Run(tc.name, func(tt *testing.T) {
359+
d, _ := networkSettingsFromNative(tc.n, tc.s)
360+
assert.DeepEqual(tt, d, tc.expected)
361+
})
236362
}
237363
}

0 commit comments

Comments
 (0)