Skip to content

Commit e64c933

Browse files
committed
handle empty RepoTags for ListImage
fix #155 Signed-off-by: yanxuean <[email protected]> Signed-off-by: yanxuean <[email protected]>
1 parent b9f4185 commit e64c933

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
@@ -125,14 +125,12 @@ var listImageCommand = cli.Command{
125125
printHeader = false
126126
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
127127
}
128-
repoTags := "<none>"
129-
if image.RepoTags != nil {
130-
repoTags = image.RepoTags[0]
131-
}
132-
repoTagsPair := strings.Split(repoTags, ":")
128+
repoTagPairs := normalizeRepoTagPair(image.RepoTags, image.RepoDigests)
133129
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
134130
trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedImageIDLen]
135-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagsPair[0], repoTagsPair[1], trunctedImage, size)
131+
for _, repoTagPair := range repoTagPairs {
132+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], trunctedImage, size)
133+
}
136134
continue
137135
}
138136
fmt.Printf("ID: %s\n", image.Id)
@@ -270,6 +268,29 @@ func getAuth(creds string) (*pb.AuthConfig, error) {
270268
}, nil
271269
}
272270

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

0 commit comments

Comments
 (0)