Skip to content

Commit

Permalink
feat: add size column to tags page
Browse files Browse the repository at this point in the history
Refs: thanks the idea from genuinetools#215
  • Loading branch information
ttys3 committed Feb 12, 2023
1 parent c884e00 commit 6fdffcc
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/distribution/distribution/v3 v3.0.0-20230131081513-cf87e8d07e8d
github.com/docker/cli v23.0.1+incompatible
github.com/docker/docker v23.0.1+incompatible
github.com/dustin/go-humanize v1.0.1
github.com/genuinetools/pkg v0.0.0-20181022210355-2fcf164d37cb
github.com/google/go-cmp v0.5.9
github.com/labstack/echo/v4 v4.10.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 h1:UhxFibDNY/bfvqU5CAUmr9zpesgbU6SWc8/B4mflAE4=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
4 changes: 3 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Repository struct {
Created *time.Time `json:"created,omitempty"` // only "Image Manifest Version 2, Schema 1" has this field
URI string `json:"uri"`
ImageType string `json:"image_type"`
ImageSize int64 `json:"image_size"`
VulnerabilityReport clair.VulnerabilityReport `json:"vulnerability"`
}

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

Expand Down
11 changes: 9 additions & 2 deletions registry/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,21 @@ func (r *Registry) CreatedDate(ctx context.Context, repository string, configDig
return configBase.Created, nil
}

func (r *Registry) TagCreatedDate(ctx context.Context, repo, tag string) (createdDate *time.Time, imageType string, retErr error) {
func (r *Registry) TagCreatedDate(ctx context.Context, repo, tag string) (createdDate *time.Time, imageType string, imageSize int64, retErr error) {
imageType = "Docker v1"
manifest, descriptor, err := r.Manifest(ctx, repo, tag)
if err != nil {
logrus.Errorf("getting v2 or oci manifest for %s:%s failed: %v", repo, tag, err)
retErr = err
return
} else if descriptor.MediaType == ociv1.MediaTypeImageManifest {
}

for _, ref := range manifest.References() {
imageSize += ref.Size
logrus.Debugf("adding ref %s with size %d, media_type=%s", ref.Digest, ref.Size, ref.MediaType)
}

if descriptor.MediaType == ociv1.MediaTypeImageManifest {
if ocimanifest, ok := manifest.(*ocischema.DeserializedManifest); ok && ocimanifest.Annotations != nil {
if created, ok := ocimanifest.Annotations["org.opencontainers.image.created"]; ok {
if t, err := time.Parse(time.RFC3339, created); err == nil {
Expand Down
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/dustin/go-humanize"
"github.com/genuinetools/reg/clair"
"github.com/labstack/echo/v4"
wordwrap "github.com/mitchellh/go-wordwrap"
Expand Down Expand Up @@ -153,6 +154,9 @@ func (cmd *serverCommand) Run(ctx context.Context, args []string) error {
return "default"
}
},
"humanize_bytes": func(s int64) string {
return humanize.Bytes(uint64(s))
},
}

rc.tmpl = template.New("").Funcs(funcMap)
Expand Down
4 changes: 4 additions & 0 deletions server/templates/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h1>{{ .RegistryDomain }}/{{ .Name }}</h1>
<tr>
<th>Name</th>
<th>Tag</th>
<th>Size</th>
<th>Image Type</th>
<th>Created</th>
{{if .HasVulns}}<th>Vulnerabilities</th>{{end}}
Expand All @@ -35,6 +36,9 @@ <h1>{{ .RegistryDomain }}/{{ .Name }}</h1>
{{ $value.Tag }}
{{if $.HasVulns}}</a>{{end}}
</td>
<td align="right" nowrap>
{{ $value.ImageSize | humanize_bytes }}
</td>
<td align="right" nowrap>
{{ $value.ImageType }}
</td>
Expand Down

0 comments on commit 6fdffcc

Please sign in to comment.