@@ -29,9 +29,9 @@ import (
2929 "github.com/containerd/containerd/v2/core/runtime/restart"
3030 "github.com/containerd/errdefs"
3131 "github.com/containerd/go-cni"
32- "github.com/containerd/log"
3332
3433 "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
34+ "github.com/containerd/nerdctl/v2/pkg/api/types"
3535 "github.com/containerd/nerdctl/v2/pkg/clientutil"
3636 "github.com/containerd/nerdctl/v2/pkg/cmd/compose"
3737 "github.com/containerd/nerdctl/v2/pkg/containerutil"
@@ -183,9 +183,9 @@ func psAction(cmd *cobra.Command, args []string) error {
183183 var p composeContainerPrintable
184184 var err error
185185 if format == "json" {
186- p , err = composeContainerPrintableJSON (ctx , container )
186+ p , err = composeContainerPrintableJSON (ctx , container , globalOptions )
187187 } else {
188- p , err = composeContainerPrintableTab (ctx , container )
188+ p , err = composeContainerPrintableTab (ctx , container , globalOptions )
189189 }
190190 if err != nil {
191191 return err
@@ -234,7 +234,7 @@ func psAction(cmd *cobra.Command, args []string) error {
234234
235235// composeContainerPrintableTab constructs composeContainerPrintable with fields
236236// only for console output.
237- func composeContainerPrintableTab (ctx context.Context , container containerd.Container ) (composeContainerPrintable , error ) {
237+ func composeContainerPrintableTab (ctx context.Context , container containerd.Container , gOptions types. GlobalCommandOptions ) (composeContainerPrintable , error ) {
238238 info , err := container .Info (ctx , containerd .WithoutRefreshedMetadata )
239239 if err != nil {
240240 return composeContainerPrintable {}, err
@@ -251,20 +251,32 @@ func composeContainerPrintableTab(ctx context.Context, container containerd.Cont
251251 if err != nil {
252252 return composeContainerPrintable {}, err
253253 }
254+ dataStore , err := clientutil .DataStore (gOptions .DataRoot , gOptions .Address )
255+ if err != nil {
256+ return composeContainerPrintable {}, err
257+ }
258+ containerLabels , err := container .Labels (ctx )
259+ if err != nil {
260+ return composeContainerPrintable {}, err
261+ }
262+ ports , err := portutil .LoadPortMappings (dataStore , gOptions .Namespace , info .ID , containerLabels )
263+ if err != nil {
264+ return composeContainerPrintable {}, err
265+ }
254266
255267 return composeContainerPrintable {
256268 Name : info .Labels [labels .Name ],
257269 Image : image .Metadata ().Name ,
258270 Command : formatter .InspectContainerCommandTrunc (spec ),
259271 Service : info .Labels [labels .ComposeService ],
260272 State : status ,
261- Ports : formatter .FormatPorts (info . Labels ),
273+ Ports : formatter .FormatPorts (ports ),
262274 }, nil
263275}
264276
265277// composeContainerPrintableJSON constructs composeContainerPrintable with fields
266278// only for json output and compatible docker output.
267- func composeContainerPrintableJSON (ctx context.Context , container containerd.Container ) (composeContainerPrintable , error ) {
279+ func composeContainerPrintableJSON (ctx context.Context , container containerd.Container , gOptions types. GlobalCommandOptions ) (composeContainerPrintable , error ) {
268280 info , err := container .Info (ctx , containerd .WithoutRefreshedMetadata )
269281 if err != nil {
270282 return composeContainerPrintable {}, err
@@ -294,6 +306,18 @@ func composeContainerPrintableJSON(ctx context.Context, container containerd.Con
294306 if err != nil {
295307 return composeContainerPrintable {}, err
296308 }
309+ dataStore , err := clientutil .DataStore (gOptions .DataRoot , gOptions .Address )
310+ if err != nil {
311+ return composeContainerPrintable {}, err
312+ }
313+ containerLabels , err := container .Labels (ctx )
314+ if err != nil {
315+ return composeContainerPrintable {}, err
316+ }
317+ portMappings , err := portutil .LoadPortMappings (dataStore , gOptions .Namespace , info .ID , containerLabels )
318+ if err != nil {
319+ return composeContainerPrintable {}, err
320+ }
297321
298322 return composeContainerPrintable {
299323 ID : container .ID (),
@@ -305,7 +329,7 @@ func composeContainerPrintableJSON(ctx context.Context, container containerd.Con
305329 State : state ,
306330 Health : "" ,
307331 ExitCode : exitCode ,
308- Publishers : formatPublishers (info . Labels ),
332+ Publishers : formatPublishers (portMappings ),
309333 }, nil
310334}
311335
@@ -321,7 +345,7 @@ type PortPublisher struct {
321345
322346// formatPublishers parses and returns docker-compatible []PortPublisher from
323347// label map. If an error happens, an empty slice is returned.
324- func formatPublishers (labelMap map [ string ] string ) []PortPublisher {
348+ func formatPublishers (portMappings []cni. PortMapping ) []PortPublisher {
325349 mapper := func (pm cni.PortMapping ) PortPublisher {
326350 return PortPublisher {
327351 URL : pm .HostIP ,
@@ -332,12 +356,8 @@ func formatPublishers(labelMap map[string]string) []PortPublisher {
332356 }
333357
334358 var dockerPorts []PortPublisher
335- if portMappings , err := portutil .ParsePortsLabel (labelMap ); err == nil {
336- for _ , p := range portMappings {
337- dockerPorts = append (dockerPorts , mapper (p ))
338- }
339- } else {
340- log .L .Error (err .Error ())
359+ for _ , p := range portMappings {
360+ dockerPorts = append (dockerPorts , mapper (p ))
341361 }
342362 return dockerPorts
343363}
0 commit comments