Skip to content

Commit 973eaea

Browse files
authored
Merge pull request #156 from yanxuean/imagelist
handle empty RepoTags for ListImage
2 parents 487e77c + f19a6f0 commit 973eaea

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cmd/crictl/image.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,12 @@ var listImageCommand = cli.Command{
123123
printHeader = false
124124
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
125125
}
126-
repoTags := "<none>"
127-
if image.RepoTags != nil {
128-
repoTags = image.RepoTags[0]
129-
}
130-
repoTagsPair := strings.Split(repoTags, ":")
126+
repoTagPairs := normalizeRepoTagPair(image.RepoTags, image.RepoDigests)
131127
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
132128
trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedImageIDLen]
133-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagsPair[0], repoTagsPair[1], trunctedImage, size)
129+
for _, repoTagPair := range repoTagPairs {
130+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], trunctedImage, size)
131+
}
134132
continue
135133
}
136134
fmt.Printf("ID: %s\n", image.Id)
@@ -265,6 +263,29 @@ func getAuth(creds string) (*pb.AuthConfig, error) {
265263
}, nil
266264
}
267265

266+
// Ideally repo tag should always be image:tag.
267+
// The repoTags is nil when pulling image by repoDigest,Then we will show image name instead.
268+
func normalizeRepoTagPair(repoTags []string, repoDigests []string) (repoTagPairs [][]string) {
269+
if len(repoTags) == 0 {
270+
if len(repoDigests) == 0 {
271+
repoTagPairs = append(repoTagPairs, []string{"errorRepoDigest", "errorRepoDigest"})
272+
return
273+
}
274+
imageName := strings.Split(repoDigests[0], "@")[0]
275+
repoTagPairs = append(repoTagPairs, []string{imageName, "<none>"})
276+
return
277+
}
278+
279+
for _, repoTag := range repoTags {
280+
if idx := strings.Index(repoTag, ":"); idx == -1 {
281+
repoTagPairs = append(repoTagPairs, []string{"errorRepoTag", "errorRepoTag"})
282+
continue
283+
}
284+
repoTagPairs = append(repoTagPairs, strings.Split(repoTag, ":"))
285+
}
286+
return
287+
}
288+
268289
// PullImage sends a PullImageRequest to the server, and parses
269290
// the returned PullImageResponse.
270291
func PullImage(client pb.ImageServiceClient, image string, auth *pb.AuthConfig) (resp *pb.PullImageResponse, err error) {

0 commit comments

Comments
 (0)