c8d/list: Fix Repo(Digests|Tags) for untagged images

Show dangling images in `docker image ls` output.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2023-02-22 16:32:17 +01:00
parent 2f9e3cca3d
commit f8791db4be
No known key found for this signature in database
GPG key ID: B85EFCFE26DEF92A
2 changed files with 41 additions and 4 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/identity"
"github.com/sirupsen/logrus"
)
var acceptedImageFilterTags = map[string]bool{
@ -64,6 +65,7 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions)
root = make([]*[]digest.Digest, len(imgs))
layers = make(map[digest.Digest]int)
}
for n, img := range imgs {
if !filter(img) {
continue
@ -91,12 +93,43 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions)
return nil, err
}
var repoTags, repoDigests []string
rawImg := img.Metadata()
target := rawImg.Target.Digest
logger := logrus.WithFields(logrus.Fields{
"name": img.Name(),
"digest": target,
})
ref, err := reference.ParseNamed(rawImg.Name)
if err != nil {
// If the image has unexpected name format (not a Named reference or a dangling image)
// add the offending name to RepoTags but also log an error to make it clear to the
// administrator that this is unexpected.
// TODO: Reconsider when containerd is more strict on image names, see:
// https://github.com/containerd/containerd/issues/7986
if !isDanglingImage(rawImg) {
logger.WithError(err).Error("failed to parse image name as reference")
repoTags = append(repoTags, img.Name())
}
} else {
repoTags = append(repoTags, reference.TagNameOnly(ref).String())
digested, err := reference.WithDigest(reference.TrimNamed(ref), target)
if err != nil {
logger.WithError(err).Error("failed to create digested reference")
} else {
repoDigests = append(repoDigests, digested.String())
}
}
summaries = append(summaries, &types.ImageSummary{
ParentID: "",
ID: img.Target().Digest.String(),
Created: img.Metadata().CreatedAt.Unix(),
RepoDigests: []string{img.Name() + "@" + img.Target().Digest.String()}, // "hello-world@sha256:bfea6278a0a267fad2634554f4f0c6f31981eea41c553fdf5a83e95a41d40c38"},
RepoTags: []string{img.Name()},
ID: target.String(),
Created: rawImg.CreatedAt.Unix(),
RepoDigests: repoDigests,
RepoTags: repoTags,
Size: size,
VirtualSize: virtualSize,
// -1 indicates that the value has not been set (avoids ambiguity

View file

@ -59,3 +59,7 @@ func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages
func danglingImageName(digest digest.Digest) string {
return "moby-dangling@" + digest.String()
}
func isDanglingImage(image containerdimages.Image) bool {
return image.Name == danglingImageName(image.Target.Digest)
}