MCPcopy
hub / github.com/containerd/containerd / ContainerStatus

Method ContainerStatus

internal/cri/server/container_status.go:34–96  ·  view source on GitHub ↗

ContainerStatus inspects the container and returns the status.

(ctx context.Context, r *runtime.ContainerStatusRequest)

Source from the content-addressed store, hash-verified

32
33// ContainerStatus inspects the container and returns the status.
34func (c *criService) ContainerStatus(ctx context.Context, r *runtime.ContainerStatusRequest) (*runtime.ContainerStatusResponse, error) {
35 container, err := c.containerStore.Get(r.GetContainerId())
36 if err != nil {
37 return nil, fmt.Errorf("an error occurred when try to find container %q: %w", r.GetContainerId(), err)
38 }
39
40 // TODO(random-liu): Clean up the following logic in CRI.
41 // Current assumption:
42 // * ImageSpec in container config is image ID.
43 // * ImageSpec in container status is image tag.
44 // * ImageRef in container status is repo digest (manifest list digest for multi-arch images).
45 // * ImageId in container status is the node-local image config digest.
46 spec := container.Config.GetImage()
47
48 // Note: container.ImageRef holds the platform-specific image config digest that was
49 // resolved during container creation. For multi-arch images, this differs from the
50 // manifest list digest. We capture it here as imageID before imageRef gets overwritten
51 // below with repoDigests[0] (the manifest list digest).
52 imageRef := container.ImageRef
53 imageID := container.ImageRef
54
55 image, err := c.GetImage(imageRef)
56 if err != nil {
57 if !errdefs.IsNotFound(err) {
58 return nil, fmt.Errorf("failed to get image %q: %w", imageRef, err)
59 }
60 } else {
61 repoTags, repoDigests := util.ParseImageReferences(image.References)
62 if len(repoTags) > 0 {
63 // Based on current behavior of dockershim, this field should be
64 // image tag.
65 spec = &runtime.ImageSpec{Image: repoTags[0]}
66 }
67 if len(repoDigests) > 0 {
68 // repoDigests[0] is the manifest list digest for multi-arch images.
69 // This overwrites imageRef (originally the platform-specific digest)
70 // for backwards compatibility with existing CRI consumers.
71 imageRef = repoDigests[0]
72 }
73 }
74 status, err := toCRIContainerStatus(ctx, container, spec, imageRef, imageID)
75 if err != nil {
76 return nil, fmt.Errorf("failed to get ContainerStatus: %w", err)
77 }
78 if status.GetCreatedAt() == 0 {
79 // CRI doesn't allow CreatedAt == 0.
80 info, err := container.Container.Info(ctx)
81 if err != nil {
82 return nil, fmt.Errorf("failed to get CreatedAt in %q state: %w", status.State, err)
83 }
84 status.CreatedAt = info.CreatedAt.UnixNano()
85 }
86
87 info, err := toCRIContainerInfo(ctx, container, r.GetVerbose())
88 if err != nil {
89 return nil, fmt.Errorf("failed to get verbose container info: %w", err)
90 }
91

Callers 4

getContainerStatusesMethod · 0.95
CheckpointContainerMethod · 0.95
containerStatusFuzzFunction · 0.45
TestContainerStatusFunction · 0.45

Implementers 1

criServiceinternal/cri/server/service.go

Calls 8

ParseImageReferencesFunction · 0.92
toCRIContainerStatusFunction · 0.85
toCRIContainerInfoFunction · 0.85
GetMethod · 0.65
GetImageMethod · 0.65
InfoMethod · 0.65
GetCreatedAtMethod · 0.45
GetVerboseMethod · 0.45

Tested by 2

containerStatusFuzzFunction · 0.36
TestContainerStatusFunction · 0.36