@@ -19,14 +19,17 @@ package namespace
1919import (
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
3235func 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+ }
0 commit comments