sort tags for docker also

This commit is contained in:
Quentin 2023-05-03 12:14:06 +02:00
parent a9e4924228
commit 559506c170
Signed by: quentin
GPG key ID: E9602264D639FF68

View file

@ -10,8 +10,10 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"sort"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/containers/image/v5/copy" "github.com/containers/image/v5/copy"
"github.com/containers/image/v5/signature" "github.com/containers/image/v5/signature"
@ -466,6 +468,12 @@ func (o *OCIMultiArch) UploadImageS3(buck *blob.Bucket) error {
type StaticRegistryManager struct { type StaticRegistryManager struct {
name string name string
buck *blob.Bucket buck *blob.Bucket
images []ImageDescriptor
}
type ImageDescriptor struct {
Tag string
Date time.Time
} }
func NewStaticRegistryManager(buck *blob.Bucket, name string) *StaticRegistryManager { func NewStaticRegistryManager(buck *blob.Bucket, name string) *StaticRegistryManager {
@ -480,14 +488,10 @@ type TagList struct {
Tags []string `json:"tags"` Tags []string `json:"tags"`
} }
func (l *StaticRegistryManager) ComputeTagList() (TagList, error) { func (l *StaticRegistryManager) Scan() error {
digestPrefix := "sha256:" digestPrefix := "sha256:"
cutDigestPrefix := len(digestPrefix) cutDigestPrefix := len(digestPrefix)
tagList := TagList{
Name: l.name,
}
iter := l.buck.List(&blob.ListOptions{ iter := l.buck.List(&blob.ListOptions{
Prefix: fmt.Sprintf("v2/%s/manifests/", l.name), Prefix: fmt.Sprintf("v2/%s/manifests/", l.name),
Delimiter: "/", Delimiter: "/",
@ -498,12 +502,12 @@ func (l *StaticRegistryManager) ComputeTagList() (TagList, error) {
break break
} }
if err != nil { if err != nil {
return tagList, err return err
} }
ksplit := strings.Split(obj.Key, "/") ksplit := strings.Split(obj.Key, "/")
if len(ksplit) < 1 { if len(ksplit) < 1 {
return tagList, errors.New(fmt.Sprintf("Invalid key name %s", obj.Key)) return errors.New(fmt.Sprintf("Invalid key name %s", obj.Key))
} }
fname := ksplit[len(ksplit)-1] fname := ksplit[len(ksplit)-1]
@ -511,18 +515,40 @@ func (l *StaticRegistryManager) ComputeTagList() (TagList, error) {
// we ignore sha256 addressed manifests // we ignore sha256 addressed manifests
continue continue
} }
tagList.Tags = append(tagList.Tags, fname) id := ImageDescriptor {
Tag: fname,
Date: obj.ModTime,
}
l.images = append(l.images, id)
} }
return tagList, nil return nil
}
func (l *StaticRegistryManager) TagList() TagList {
// Sort by date desc
sort.Slice(l.images, func(i, j int) bool {
return l.images[i].Date.After(l.images[j].Date)
})
// Build tagList
tagList := TagList {
Name: l.name,
}
for _, img := range l.images {
tagList.Tags = append(tagList.Tags, img.Tag)
}
return tagList
} }
func (l *StaticRegistryManager) UpdateTagList() error { func (l *StaticRegistryManager) UpdateTagList() error {
fmt.Printf("--- update taglist ---\n") fmt.Printf("--- update taglist ---\n")
tagList, err := l.ComputeTagList() err := l.Scan()
if err != nil { if err != nil {
return err return err
} }
tagList := l.TagList()
fmt.Printf("computed tag list: %v\n", tagList) fmt.Printf("computed tag list: %v\n", tagList)
txt, err := json.Marshal(tagList) txt, err := json.Marshal(tagList)