Skip to content

Commit 6fdffcc

Browse files
committed
feat: add size column to tags page
Refs: thanks the idea from genuinetools#215
1 parent c884e00 commit 6fdffcc

File tree

6 files changed

+23
-3
lines changed

6 files changed

+23
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/distribution/distribution/v3 v3.0.0-20230131081513-cf87e8d07e8d
77
github.com/docker/cli v23.0.1+incompatible
88
github.com/docker/docker v23.0.1+incompatible
9+
github.com/dustin/go-humanize v1.0.1
910
github.com/genuinetools/pkg v0.0.0-20181022210355-2fcf164d37cb
1011
github.com/google/go-cmp v0.5.9
1112
github.com/labstack/echo/v4 v4.10.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
3434
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
3535
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 h1:UhxFibDNY/bfvqU5CAUmr9zpesgbU6SWc8/B4mflAE4=
3636
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
37+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
38+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
3739
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
3840
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
3941
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=

handlers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Repository struct {
4141
Created *time.Time `json:"created,omitempty"` // only "Image Manifest Version 2, Schema 1" has this field
4242
URI string `json:"uri"`
4343
ImageType string `json:"image_type"`
44+
ImageSize int64 `json:"image_size"`
4445
VulnerabilityReport clair.VulnerabilityReport `json:"vulnerability"`
4546
}
4647

@@ -194,7 +195,7 @@ func (rc *registryController) generateTagsTemplate(ctx context.Context, repo str
194195
// get the image creat time, for v2 or oci image,
195196
// maybe someday we can get it from `org.opencontainers.image.created` annotation
196197
// ref https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys
197-
createdDate, imageType, err := rc.reg.TagCreatedDate(ctx, repo, tag)
198+
createdDate, imageType, imageSize, err := rc.reg.TagCreatedDate(ctx, repo, tag)
198199
if err != nil {
199200
logrus.Warnf("getting created date for %s:%s failed: %v", repo, tag, err)
200201
}
@@ -208,6 +209,7 @@ func (rc *registryController) generateTagsTemplate(ctx context.Context, repo str
208209
Tag: tag,
209210
URI: repoURI,
210211
ImageType: imageType,
212+
ImageSize: imageSize,
211213
Created: createdDate,
212214
}
213215

registry/tags.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,21 @@ func (r *Registry) CreatedDate(ctx context.Context, repository string, configDig
4141
return configBase.Created, nil
4242
}
4343

44-
func (r *Registry) TagCreatedDate(ctx context.Context, repo, tag string) (createdDate *time.Time, imageType string, retErr error) {
44+
func (r *Registry) TagCreatedDate(ctx context.Context, repo, tag string) (createdDate *time.Time, imageType string, imageSize int64, retErr error) {
4545
imageType = "Docker v1"
4646
manifest, descriptor, err := r.Manifest(ctx, repo, tag)
4747
if err != nil {
4848
logrus.Errorf("getting v2 or oci manifest for %s:%s failed: %v", repo, tag, err)
4949
retErr = err
5050
return
51-
} else if descriptor.MediaType == ociv1.MediaTypeImageManifest {
51+
}
52+
53+
for _, ref := range manifest.References() {
54+
imageSize += ref.Size
55+
logrus.Debugf("adding ref %s with size %d, media_type=%s", ref.Digest, ref.Size, ref.MediaType)
56+
}
57+
58+
if descriptor.MediaType == ociv1.MediaTypeImageManifest {
5259
if ocimanifest, ok := manifest.(*ocischema.DeserializedManifest); ok && ocimanifest.Annotations != nil {
5360
if created, ok := ocimanifest.Annotations["org.opencontainers.image.created"]; ok {
5461
if t, err := time.Parse(time.RFC3339, created); err == nil {

server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414
"time"
1515

16+
"github.com/dustin/go-humanize"
1617
"github.com/genuinetools/reg/clair"
1718
"github.com/labstack/echo/v4"
1819
wordwrap "github.com/mitchellh/go-wordwrap"
@@ -153,6 +154,9 @@ func (cmd *serverCommand) Run(ctx context.Context, args []string) error {
153154
return "default"
154155
}
155156
},
157+
"humanize_bytes": func(s int64) string {
158+
return humanize.Bytes(uint64(s))
159+
},
156160
}
157161

158162
rc.tmpl = template.New("").Funcs(funcMap)

server/templates/tags.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ <h1>{{ .RegistryDomain }}/{{ .Name }}</h1>
1919
<tr>
2020
<th>Name</th>
2121
<th>Tag</th>
22+
<th>Size</th>
2223
<th>Image Type</th>
2324
<th>Created</th>
2425
{{if .HasVulns}}<th>Vulnerabilities</th>{{end}}
@@ -35,6 +36,9 @@ <h1>{{ .RegistryDomain }}/{{ .Name }}</h1>
3536
{{ $value.Tag }}
3637
{{if $.HasVulns}}</a>{{end}}
3738
</td>
39+
<td align="right" nowrap>
40+
{{ $value.ImageSize | humanize_bytes }}
41+
</td>
3842
<td align="right" nowrap>
3943
{{ $value.ImageType }}
4044
</td>

0 commit comments

Comments
 (0)