MCPcopy Create free account
hub / github.com/containerd/nerdctl / composeContainerPrintableJSON

Function composeContainerPrintableJSON

cmd/nerdctl/compose/compose_ps.go:279–334  ·  view source on GitHub ↗

composeContainerPrintableJSON constructs composeContainerPrintable with fields only for json output and compatible docker output.

(ctx context.Context, container containerd.Container, gOptions types.GlobalCommandOptions)

Source from the content-addressed store, hash-verified

277// composeContainerPrintableJSON constructs composeContainerPrintable with fields
278// only for json output and compatible docker output.
279func composeContainerPrintableJSON(ctx context.Context, container containerd.Container, gOptions types.GlobalCommandOptions) (composeContainerPrintable, error) {
280 info, err := container.Info(ctx, containerd.WithoutRefreshedMetadata)
281 if err != nil {
282 return composeContainerPrintable{}, err
283 }
284 spec, err := container.Spec(ctx)
285 if err != nil {
286 return composeContainerPrintable{}, err
287 }
288
289 var (
290 state string
291 exitCode uint32
292 )
293 status, err := containerutil.ContainerStatus(ctx, container)
294 if err == nil {
295 // show exitCode only when container is exited/stopped
296 if status.Status == containerd.Stopped {
297 state = "exited"
298 exitCode = status.ExitStatus
299 } else {
300 state = string(status.Status)
301 }
302 } else {
303 state = string(containerd.Unknown)
304 }
305 image, err := container.Image(ctx)
306 if err != nil {
307 return composeContainerPrintable{}, err
308 }
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 }
321
322 return composeContainerPrintable{
323 ID: container.ID(),
324 Name: info.Labels[labels.Name],
325 Image: image.Metadata().Name,
326 Command: formatter.InspectContainerCommand(spec, false, false),
327 Project: info.Labels[labels.ComposeProject],
328 Service: info.Labels[labels.ComposeService],
329 State: state,
330 Health: "",
331 ExitCode: exitCode,
332 Publishers: formatPublishers(portMappings),
333 }, nil
334}
335
336// PortPublisher hold status about published port

Callers 1

psActionFunction · 0.85

Calls 7

ContainerStatusFunction · 0.92
DataStoreFunction · 0.92
LoadPortMappingsFunction · 0.92
InspectContainerCommandFunction · 0.92
formatPublishersFunction · 0.85
InfoMethod · 0.80
LabelsMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…