From 0ccafc296128d772f7b6ab38be44d1d9932092eb Mon Sep 17 00:00:00 2001 From: Aceralon Date: Thu, 16 Dec 2021 17:51:10 +0800 Subject: [PATCH] implement digest --- engine/virt/image.go | 33 +++++++++++++++++++++++++++------ types/errors.go | 1 + 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/engine/virt/image.go b/engine/virt/image.go index ab79b8073..9e097a2fd 100644 --- a/engine/virt/image.go +++ b/engine/virt/image.go @@ -129,13 +129,34 @@ func (v *Virt) ImageBuildCachePrune(ctx context.Context, all bool) (reclaimed ui } // ImageLocalDigests shows local images' digests. -func (v *Virt) ImageLocalDigests(ctx context.Context, image string) (digests []string, err error) { - log.Warnf(ctx, "ImageLocalDigests does not implement") - return +// If local image file not exists return error +// If exists return digests +// Same for remote digest +func (v *Virt) ImageLocalDigests(ctx context.Context, image string) ([]string, error) { + // If not exists return error + // If exists return digests + _, imgName, err := splitUserImage(image) + if err != nil { + return nil, err + } + + return v.client.DigestImage(ctx, imgName, true) } // ImageRemoteDigest shows remote one's digest. -func (v *Virt) ImageRemoteDigest(ctx context.Context, image string) (digest string, err error) { - log.Warnf(ctx, "ImageRemoteDigest does not implement") - return +func (v *Virt) ImageRemoteDigest(ctx context.Context, image string) (string, error) { + _, imgName, err := splitUserImage(image) + if err != nil { + return "", err + } + + digests, err := v.client.DigestImage(ctx, imgName, true) + switch { + case err != nil: + return "", err + case len(digests) < 1: + return "", types.ErrNoRemoteDigest + default: + return digests[0], nil + } } diff --git a/types/errors.go b/types/errors.go index ac6ddb4ec..26417b230 100644 --- a/types/errors.go +++ b/types/errors.go @@ -63,6 +63,7 @@ var ( ErrNoWorkloadIDs = errors.New("No workload ids given") ErrRunAndWaitCountOneWithStdin = errors.New("Count must be 1 if OpenStdin is true") ErrUnknownControlType = errors.New("Unknown control type") + ErrNoRemoteDigest = errors.New("got no digest") ErrNoETCD = errors.New("ETCD must be set") ErrKeyNotExists = errors.New("Key not exists")