Skip to content

Commit fca454f

Browse files
committed
handle calvar format through allow-tags
1 parent c28e3b6 commit fca454f

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

registry-scanner/pkg/image/matchfunc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"regexp"
55

66
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log"
7+
"github.com/k1LoW/calver"
78
)
89

910
// MatchFuncAny matches any pattern, i.e. always returns true
@@ -25,3 +26,13 @@ func MatchFuncRegexp(tagName string, args interface{}) bool {
2526
}
2627
return pattern.Match([]byte(tagName))
2728
}
29+
30+
// MatchFuncCalVer checks if a tag matches the specified CalVer layout
31+
func MatchFuncCalVer(tagName string, args interface{}) bool {
32+
layoutStr, ok := args.(string)
33+
if !ok {
34+
return false
35+
}
36+
_, err := calver.Parse(layoutStr, tagName)
37+
return err == nil
38+
}

registry-scanner/pkg/image/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ func (img *ContainerImage) ParseMatchfunc(val string) (MatchFuncFn, interface{})
175175
return MatchFuncNone, nil
176176
}
177177
return MatchFuncRegexp, re
178+
case "calver":
179+
return MatchFuncCalVer, opt[1]
178180
default:
179181
logCtx.Warnf("Unknown match function: %s", opt[0])
180182
return MatchFuncNone, nil

registry-scanner/pkg/image/version.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package image
22

33
import (
4+
"fmt"
45
"path/filepath"
56

67
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log"
@@ -98,7 +99,12 @@ func (img *ContainerImage) GetNewestVersionFromTags(vc *VersionConstraint, tagLi
9899
case StrategyDigest:
99100
availableTags = tagList.SortAlphabetically()
100101
case StrategyCalVer:
101-
availableTags = tagList.SortByCalVer()
102+
layout, ok := vc.MatchArgs.(string)
103+
if !ok {
104+
logCtx.Errorf("calver layout not specified in allow-tags annotation")
105+
return nil, fmt.Errorf("calver layout not specified in allow-tags annotation")
106+
}
107+
availableTags = tagList.SortByCalVer(layout)
102108
}
103109

104110
considerTags := tag.SortableImageTagList{}

registry-scanner/pkg/image/version_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ func Test_LatestVersion(t *testing.T) {
7676
assert.Nil(t, newTag)
7777
})
7878

79+
t.Run("Find the latest version with a calver constraint that is valid", func(t *testing.T) {
80+
tagList := newImageTagList([]string{"2021.01.01", "2022.02.02", "2023.05.01", "2025.01.25"})
81+
img := NewFromIdentifier("jannfis/test:2021.01.01")
82+
vc := VersionConstraint{Constraint: "2022.01.01", Strategy: StrategyCalVer, MatchArgs: "YYYY.MM.DD"}
83+
newTag, err := img.GetNewestVersionFromTags(&vc, tagList)
84+
assert.NoError(t, err)
85+
assert.NotNil(t, newTag)
86+
})
87+
7988
t.Run("Find the latest version with no tags", func(t *testing.T) {
8089
tagList := newImageTagList([]string{})
8190
img := NewFromIdentifier("jannfis/test:1.0")

registry-scanner/pkg/tag/tag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ func (il ImageTagList) SortBySemVer() SortableImageTagList {
173173
return sil
174174
}
175175

176-
func (il ImageTagList) SortByCalVer() SortableImageTagList {
176+
func (il ImageTagList) SortByCalVer(layout string) SortableImageTagList {
177177
il.lock.RLock()
178178
defer il.lock.RUnlock()
179179
sil := make(SortableImageTagList, 0, len(il.items))
180180
calvers := make(calver.Calvers, 0, len(il.items))
181181

182182
for _, v := range il.items {
183-
cv, err := calver.Parse(v.TagName)
183+
cv, err := calver.Parse(layout, v.TagName)
184184
if err != nil {
185185
// Fallback to alphabetical order if parsing fails
186186
sil = append(sil, v)

0 commit comments

Comments
 (0)