ContainerStatus inspects the container and returns the status.
(ctx context.Context, r *runtime.ContainerStatusRequest)
| 32 | |
| 33 | // ContainerStatus inspects the container and returns the status. |
| 34 | func (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 |