From f19a6f0598675c7930d0b8b5a73e8a817b7f73d3 Mon Sep 17 00:00:00 2001 From: yanxuean Date: Fri, 13 Oct 2017 12:03:20 +0800 Subject: [PATCH] handle empty RepoTags for ListImage fix #155 list all image tags. fix #157 Signed-off-by: yanxuean --- cmd/crictl/image.go | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index 5f7f831ad0..ac7b921d18 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -125,14 +125,12 @@ var listImageCommand = cli.Command{ printHeader = false fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE") } - repoTags := "" - if image.RepoTags != nil { - repoTags = image.RepoTags[0] - } - repoTagsPair := strings.Split(repoTags, ":") + repoTagPairs := normalizeRepoTagPair(image.RepoTags, image.RepoDigests) size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3) trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedImageIDLen] - fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagsPair[0], repoTagsPair[1], trunctedImage, size) + for _, repoTagPair := range repoTagPairs { + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], trunctedImage, size) + } continue } fmt.Printf("ID: %s\n", image.Id) @@ -270,6 +268,29 @@ func getAuth(creds string) (*pb.AuthConfig, error) { }, nil } +// Ideally repo tag should always be image:tag. +// The repoTags is nil when pulling image by repoDigest,Then we will show image name instead. +func normalizeRepoTagPair(repoTags []string, repoDigests []string) (repoTagPairs [][]string) { + if len(repoTags) == 0 { + if len(repoDigests) == 0 { + repoTagPairs = append(repoTagPairs, []string{"errorRepoDigest", "errorRepoDigest"}) + return + } + imageName := strings.Split(repoDigests[0], "@")[0] + repoTagPairs = append(repoTagPairs, []string{imageName, ""}) + return + } + + for _, repoTag := range repoTags { + if idx := strings.Index(repoTag, ":"); idx == -1 { + repoTagPairs = append(repoTagPairs, []string{"errorRepoTag", "errorRepoTag"}) + continue + } + repoTagPairs = append(repoTagPairs, strings.Split(repoTag, ":")) + } + return +} + // PullImage sends a PullImageRequest to the server, and parses // the returned PullImageResponse. func PullImage(client pb.ImageServiceClient, image string, auth *pb.AuthConfig) (*pb.PullImageResponse, error) {