From 559506c17036137479e4434fd9d90a63af033808 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 3 May 2023 12:14:06 +0200 Subject: [PATCH] sort tags for docker also --- cmd/container.go | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/cmd/container.go b/cmd/container.go index f52ba5e..5810a1a 100644 --- a/cmd/container.go +++ b/cmd/container.go @@ -10,8 +10,10 @@ import ( "os" "path" "path/filepath" + "sort" "strconv" "strings" + "time" "github.com/containers/image/v5/copy" "github.com/containers/image/v5/signature" @@ -466,6 +468,12 @@ func (o *OCIMultiArch) UploadImageS3(buck *blob.Bucket) error { type StaticRegistryManager struct { name string buck *blob.Bucket + images []ImageDescriptor +} + +type ImageDescriptor struct { + Tag string + Date time.Time } func NewStaticRegistryManager(buck *blob.Bucket, name string) *StaticRegistryManager { @@ -480,14 +488,10 @@ type TagList struct { Tags []string `json:"tags"` } -func (l *StaticRegistryManager) ComputeTagList() (TagList, error) { +func (l *StaticRegistryManager) Scan() error { digestPrefix := "sha256:" cutDigestPrefix := len(digestPrefix) - tagList := TagList{ - Name: l.name, - } - iter := l.buck.List(&blob.ListOptions{ Prefix: fmt.Sprintf("v2/%s/manifests/", l.name), Delimiter: "/", @@ -498,12 +502,12 @@ func (l *StaticRegistryManager) ComputeTagList() (TagList, error) { break } if err != nil { - return tagList, err + return err } ksplit := strings.Split(obj.Key, "/") 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] @@ -511,18 +515,40 @@ func (l *StaticRegistryManager) ComputeTagList() (TagList, error) { // we ignore sha256 addressed manifests 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 { fmt.Printf("--- update taglist ---\n") - tagList, err := l.ComputeTagList() + err := l.Scan() if err != nil { return err } + + tagList := l.TagList() fmt.Printf("computed tag list: %v\n", tagList) txt, err := json.Marshal(tagList)