From 2ebcc454baa5cfa03858c692ee0c8d134bc9f65a Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 15 Sep 2017 13:29:27 +0800 Subject: [PATCH] support image size --- docs/swagger.yaml | 3 +++ src/ui/api/repository.go | 58 +++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 9bf728160..920d320b9 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -2586,6 +2586,9 @@ definitions: name: type: string description: The name of the tag. + size: + type: integer + description: The size of the image. architecture: type: string description: The architecture of the image. diff --git a/src/ui/api/repository.go b/src/ui/api/repository.go index 6e15559d0..ab0ac0ad8 100644 --- a/src/ui/api/repository.go +++ b/src/ui/api/repository.go @@ -54,9 +54,10 @@ type repoResp struct { UpdateTime time.Time `json:"update_time"` } -type tag struct { +type tagDetail struct { Digest string `json:"digest"` Name string `json:"name"` + Size int64 `json:"size"` Architecture string `json:"architecture"` OS string `json:"os"` DockerVersion string `json:"docker_version"` @@ -65,7 +66,7 @@ type tag struct { } type tagResp struct { - tag + tagDetail Signature *notary.Target `json:"signature"` ScanOverview *models.ImgScanOverview `json:"scan_overview,omitempty"` } @@ -390,17 +391,13 @@ func assemble(client *registry.Repository, repository string, for _, t := range tags { item := &tagResp{} - // tag configuration - digest, _, cfg, err := getV2Manifest(client, t) + // the detail information of tag + tagDetail, err := getTagDetail(client, t) if err != nil { - cfg = &tag{ - Digest: digest, - Name: t, - } log.Errorf("failed to get v2 manifest of %s:%s: %v", repository, t, err) } - if cfg != nil { - item.tag = *cfg + if tagDetail != nil { + item.tagDetail = *tagDetail } // scan overview @@ -425,40 +422,45 @@ func assemble(client *registry.Repository, repository string, return result } -// get v2 manifest of tag, returns digest, manifest, -// manifest config and error. The manifest config contains -// architecture, os, author, etc. -func getV2Manifest(client *registry.Repository, tagName string) ( - string, *schema2.DeserializedManifest, *tag, error) { - digest, _, payload, err := client.PullManifest(tagName, []string{schema2.MediaTypeManifest}) - if err != nil { - return "", nil, nil, err +// getTagDetail returns the detail information for v2 manifest image +// The information contains architecture, os, author, size, etc. +func getTagDetail(client *registry.Repository, tag string) (*tagDetail, error) { + detail := &tagDetail{ + Name: tag, } + digest, _, payload, err := client.PullManifest(tag, []string{schema2.MediaTypeManifest}) + if err != nil { + return detail, err + } + detail.Digest = digest + manifest := &schema2.DeserializedManifest{} if err = manifest.UnmarshalJSON(payload); err != nil { - return digest, nil, nil, err + return detail, err + } + + // size of manifest + size of layers + detail.Size = int64(len(payload)) + for _, ref := range manifest.References() { + detail.Size += ref.Size } _, reader, err := client.PullBlob(manifest.Target().Digest.String()) if err != nil { - return digest, manifest, nil, err + return detail, err } configData, err := ioutil.ReadAll(reader) if err != nil { - return digest, manifest, nil, err + return detail, err } - config := &tag{} - if err = json.Unmarshal(configData, config); err != nil { - return digest, manifest, nil, err + if err = json.Unmarshal(configData, detail); err != nil { + return detail, err } - config.Name = tagName - config.Digest = digest - - return digest, manifest, config, nil + return detail, nil } // GetManifests returns the manifest of a tag