Skip to content

Commit 2a7ac0e

Browse files
committed
feat(namespace): include containers, images, and volumes details in inspect
Signed-off-by: Park jungtae <[email protected]>
1 parent 21163f0 commit 2a7ac0e

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

pkg/cmd/namespace/inspect.go

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ package namespace
1919
import (
2020
"context"
2121
"errors"
22+
"strings"
2223

2324
containerd "github.com/containerd/containerd/v2/client"
2425
"github.com/containerd/containerd/v2/pkg/namespaces"
2526
"github.com/containerd/log"
2627

2728
"github.com/containerd/nerdctl/v2/pkg/api/types"
29+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
2830
"github.com/containerd/nerdctl/v2/pkg/formatter"
2931
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
32+
"github.com/containerd/nerdctl/v2/pkg/mountutil/volumestore"
3033
)
3134

3235
func Inspect(ctx context.Context, client *containerd.Client, inspectedNamespaces []string, options types.NamespaceInspectOptions) error {
@@ -40,13 +43,33 @@ func Inspect(ctx context.Context, client *containerd.Client, inspectedNamespaces
4043
warns = append(warns, err)
4144
continue
4245
}
46+
4347
labels, err := namespaceService.Labels(ctx, ns)
4448
if err != nil {
4549
return err
4650
}
51+
52+
containerInfo, err := containerInfo(ctx, client)
53+
if err != nil {
54+
warns = append(warns, err)
55+
}
56+
57+
imageInfo, err := imageInfo(ctx, client)
58+
if err != nil {
59+
warns = append(warns, err)
60+
}
61+
62+
volumeInfo, err := volumeInfo(ns, options)
63+
if err != nil {
64+
warns = append(warns, err)
65+
}
66+
4767
nsInspect := native.Namespace{
48-
Name: ns,
49-
Labels: &labels,
68+
Name: ns,
69+
Labels: &labels,
70+
Containers: &containerInfo,
71+
Images: &imageInfo,
72+
Volumes: &volumeInfo,
5073
}
5174
result = append(result, nsInspect)
5275
}
@@ -63,3 +86,66 @@ func Inspect(ctx context.Context, client *containerd.Client, inspectedNamespaces
6386

6487
return nil
6588
}
89+
func containerInfo(ctx context.Context, client *containerd.Client) (native.ContainerInfo, error) {
90+
info := native.ContainerInfo{}
91+
containers, err := client.Containers(ctx)
92+
if err != nil {
93+
return info, err
94+
}
95+
96+
info.Count = len(containers)
97+
ids := make([]string, info.Count)
98+
99+
info.IDs = ids
100+
for idx, container := range containers {
101+
ids[idx] = container.ID()
102+
}
103+
104+
return info, nil
105+
}
106+
func imageInfo(ctx context.Context, client *containerd.Client) (native.ImageInfo, error) {
107+
info := native.ImageInfo{}
108+
imageService := client.ImageService()
109+
images, err := imageService.List(ctx)
110+
if err != nil {
111+
return info, err
112+
}
113+
114+
info.Count = len(images)
115+
ids := make([]string, info.Count)
116+
117+
info.IDs = ids
118+
for idx, img := range images {
119+
ids[idx] = strings.Split(img.Target.Digest.String(), ":")[1]
120+
}
121+
122+
return info, nil
123+
}
124+
125+
func volumeInfo(namespace string, options types.NamespaceInspectOptions) (native.VolumeInfo, error) {
126+
info := native.VolumeInfo{}
127+
128+
dataStore, err := clientutil.DataStore(options.GOptions.DataRoot, options.GOptions.Address)
129+
if err != nil {
130+
return info, err
131+
}
132+
volStore, err := volumestore.New(dataStore, namespace)
133+
if err != nil {
134+
return info, err
135+
}
136+
137+
volumes, err := volStore.List(false)
138+
if err != nil {
139+
return info, err
140+
}
141+
142+
info.Count = len(volumes)
143+
names := make([]string, 0, info.Count)
144+
145+
for _, v := range volumes {
146+
names = append(names, v.Name)
147+
}
148+
149+
info.Names = names
150+
return info, nil
151+
}

pkg/inspecttypes/native/namespace.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,23 @@ package native
1919
type Namespace struct {
2020
Name string `json:"Name"`
2121
Labels *map[string]string `json:"Labels,omitempty"`
22+
23+
Containers *ContainerInfo `json:"Containers"`
24+
Images *ImageInfo `json:"Images"`
25+
Volumes *VolumeInfo `json:"Volumes"`
26+
}
27+
28+
type ContainerInfo struct {
29+
Count int `json:"count"`
30+
IDs []string `json:"ids"`
31+
}
32+
33+
type ImageInfo struct {
34+
Count int `json:"count"`
35+
IDs []string `json:"ids"`
36+
}
37+
38+
type VolumeInfo struct {
39+
Count int `json:"count"`
40+
Names []string `json:"names"`
2241
}

0 commit comments

Comments
 (0)