diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index a1dda847f6..d34be31581 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -91,6 +91,10 @@ var listImageCommand = cli.Command{ Name: "output, o", Usage: "Output format, One of: json|yaml|table", }, + cli.BoolFlag{ + Name: "digests", + Usage: "Show digests", + }, }, Action: func(context *cli.Context) error { if err := getImageClient(context); err != nil { @@ -119,15 +123,25 @@ var listImageCommand = cli.Command{ continue } if !verbose { + showDigest := context.Bool("digests") if printHeader { printHeader = false - fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE") + if showDigest { + fmt.Fprintln(w, "IMAGE\tTAG\tDIGEST\tIMAGE ID\tSIZE") + } else { + fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE") + } } - repoTagPairs := normalizeRepoTagPair(image.RepoTags, image.RepoDigests) + imageName, repoDigest := normalizeRepoDigest(image.RepoDigests) + repoTagPairs := normalizeRepoTagPair(image.RepoTags, imageName) size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3) trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedImageIDLen] for _, repoTagPair := range repoTagPairs { - fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], trunctedImage, size) + if showDigest { + fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], repoDigest, trunctedImage, size) + } else { + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], trunctedImage, size) + } } continue } @@ -265,17 +279,11 @@ func getAuth(creds string) (*pb.AuthConfig, error) { // 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) { +func normalizeRepoTagPair(repoTags []string, imageName 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"}) @@ -286,6 +294,17 @@ func normalizeRepoTagPair(repoTags []string, repoDigests []string) (repoTagPairs return } +func normalizeRepoDigest(repoDigests []string) (string, string) { + if len(repoDigests) == 0 { + return "", "" + } + repoDigestPair := strings.Split(repoDigests[0], "@") + if len(repoDigestPair) != 2 { + return "errorName", "errorRepoDigest" + } + return repoDigestPair[0], repoDigestPair[1] +} + // PullImage sends a PullImageRequest to the server, and parses // the returned PullImageResponse. func PullImage(client pb.ImageServiceClient, image string, auth *pb.AuthConfig) (resp *pb.PullImageResponse, err error) {