Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle empty RepoTags for ListImage #156

Merged
merged 1 commit into from
Oct 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,12 @@ var listImageCommand = cli.Command{
printHeader = false
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
}
repoTags := "<none>"
if image.RepoTags != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change this to if len(image.RepoTags) > 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only this is insufficient. repoTagsPair[1] will still cause panic when it is "" or "tag1"

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)
Expand Down Expand Up @@ -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, "<none>"})
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) {
Expand Down