Skip to content

Commit 02aa138

Browse files
authored
refactor: rename VersionSortMode to UpdateStrategy (#343)
Signed-off-by: jannfis <[email protected]>
1 parent 752f305 commit 02aa138

File tree

8 files changed

+75
-55
lines changed

8 files changed

+75
-55
lines changed

cmd/test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strate
7676

7777
vc := &image.VersionConstraint{
7878
Constraint: semverConstraint,
79-
SortMode: image.VersionSortSemVer,
79+
Strategy: image.StrategySemVer,
8080
}
8181

82-
vc.SortMode = image.ParseUpdateStrategy(strategy)
82+
vc.Strategy = image.ParseUpdateStrategy(strategy)
8383

8484
if allowTags != "" {
8585
vc.MatchFunc, vc.MatchArgs = image.ParseMatchfunc(allowTags)
@@ -101,7 +101,7 @@ argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strate
101101
}
102102
vc.Options = vc.Options.
103103
WithPlatform(os, arch, variant).
104-
WithMetadata(vc.SortMode.NeedsMetadata())
104+
WithMetadata(vc.Strategy.NeedsMetadata())
105105
}
106106

107107
if registriesConfPath != "" {

pkg/argocd/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat
194194
imgCtx.Debugf("Using no version constraint when looking for a new tag")
195195
}
196196

197-
vc.SortMode = applicationImage.GetParameterUpdateStrategy(updateConf.UpdateApp.Application.Annotations)
197+
vc.Strategy = applicationImage.GetParameterUpdateStrategy(updateConf.UpdateApp.Application.Annotations)
198198
vc.MatchFunc, vc.MatchArgs = applicationImage.GetParameterMatch(updateConf.UpdateApp.Application.Annotations)
199199
vc.IgnoreList = applicationImage.GetParameterIgnoreTags(updateConf.UpdateApp.Application.Annotations)
200-
vc.Options = applicationImage.GetPlatformOptions(updateConf.UpdateApp.Application.Annotations, updateConf.IgnorePlatforms).WithMetadata(vc.SortMode.NeedsMetadata())
200+
vc.Options = applicationImage.GetPlatformOptions(updateConf.UpdateApp.Application.Annotations, updateConf.IgnorePlatforms).WithMetadata(vc.Strategy.NeedsMetadata())
201201

202202
// The endpoint can provide default credentials for pulling images
203203
err = rep.SetEndpointCredentials(updateConf.KubeClient)
@@ -257,7 +257,7 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat
257257
// image is configured to use a tag and no digest, we need to set an
258258
// initial dummy digest, so that tag.Equals() will return false.
259259
// TODO: Fix this. This is just a workaround.
260-
if vc.SortMode == image.VersionSortDigest {
260+
if vc.Strategy == image.StrategyDigest {
261261
if !updateableImage.ImageTag.IsDigest() {
262262
log.Tracef("Setting dummy digest for image %s", updateableImage.GetFullNameWithTag())
263263
updateableImage.ImageTag.TagDigest = "dummy"

pkg/image/options.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,31 @@ func (img *ContainerImage) HasForceUpdateOptionAnnotation(annotations map[string
6565

6666
// GetParameterSort gets and validates the value for the sort option for the
6767
// image from a set of annotations
68-
func (img *ContainerImage) GetParameterUpdateStrategy(annotations map[string]string) VersionSortMode {
68+
func (img *ContainerImage) GetParameterUpdateStrategy(annotations map[string]string) UpdateStrategy {
6969
key := fmt.Sprintf(common.UpdateStrategyAnnotation, img.normalizedSymbolicName())
7070
val, ok := annotations[key]
7171
if !ok {
7272
// Default is sort by version
7373
log.Tracef("No sort option %s found", key)
74-
return VersionSortSemVer
74+
return StrategySemVer
7575
}
7676
log.Tracef("found update strategy %s in %s", val, key)
7777
return ParseUpdateStrategy(val)
7878
}
7979

80-
func ParseUpdateStrategy(val string) VersionSortMode {
80+
func ParseUpdateStrategy(val string) UpdateStrategy {
8181
switch strings.ToLower(val) {
8282
case "semver":
83-
return VersionSortSemVer
83+
return StrategySemVer
8484
case "latest":
85-
return VersionSortLatest
85+
return StrategyLatest
8686
case "name":
87-
return VersionSortName
87+
return StrategyName
8888
case "digest":
89-
return VersionSortDigest
89+
return StrategyDigest
9090
default:
9191
log.Warnf("Unknown sort option %s -- using semver", val)
92-
return VersionSortSemVer
92+
return StrategySemVer
9393
}
9494

9595
}

pkg/image/options_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func Test_GetSortOption(t *testing.T) {
8383
}
8484
img := NewFromIdentifier("dummy=foo/bar:1.12")
8585
sortMode := img.GetParameterUpdateStrategy(annotations)
86-
assert.Equal(t, VersionSortSemVer, sortMode)
86+
assert.Equal(t, StrategySemVer, sortMode)
8787
})
8888

8989
t.Run("Get update strategy date for configured application", func(t *testing.T) {
@@ -92,7 +92,7 @@ func Test_GetSortOption(t *testing.T) {
9292
}
9393
img := NewFromIdentifier("dummy=foo/bar:1.12")
9494
sortMode := img.GetParameterUpdateStrategy(annotations)
95-
assert.Equal(t, VersionSortLatest, sortMode)
95+
assert.Equal(t, StrategyLatest, sortMode)
9696
})
9797

9898
t.Run("Get update strategy name for configured application", func(t *testing.T) {
@@ -101,7 +101,7 @@ func Test_GetSortOption(t *testing.T) {
101101
}
102102
img := NewFromIdentifier("dummy=foo/bar:1.12")
103103
sortMode := img.GetParameterUpdateStrategy(annotations)
104-
assert.Equal(t, VersionSortName, sortMode)
104+
assert.Equal(t, StrategyName, sortMode)
105105
})
106106

107107
t.Run("Get update strategy option configured application because of invalid option", func(t *testing.T) {
@@ -110,14 +110,14 @@ func Test_GetSortOption(t *testing.T) {
110110
}
111111
img := NewFromIdentifier("dummy=foo/bar:1.12")
112112
sortMode := img.GetParameterUpdateStrategy(annotations)
113-
assert.Equal(t, VersionSortSemVer, sortMode)
113+
assert.Equal(t, StrategySemVer, sortMode)
114114
})
115115

116116
t.Run("Get update strategy option configured application because of option not set", func(t *testing.T) {
117117
annotations := map[string]string{}
118118
img := NewFromIdentifier("dummy=foo/bar:1.12")
119119
sortMode := img.GetParameterUpdateStrategy(annotations)
120-
assert.Equal(t, VersionSortSemVer, sortMode)
120+
assert.Equal(t, StrategySemVer, sortMode)
121121
})
122122
}
123123

pkg/image/version.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import (
1111
)
1212

1313
// VersionSortMode defines the method to sort a list of tags
14-
type VersionSortMode int
14+
type UpdateStrategy int
1515

1616
const (
1717
// VersionSortSemVer sorts tags using semver sorting (the default)
18-
VersionSortSemVer VersionSortMode = 0
18+
StrategySemVer UpdateStrategy = 0
1919
// VersionSortLatest sorts tags after their creation date
20-
VersionSortLatest VersionSortMode = 1
20+
StrategyLatest UpdateStrategy = 1
2121
// VersionSortName sorts tags alphabetically by name
22-
VersionSortName VersionSortMode = 2
22+
StrategyName UpdateStrategy = 2
2323
// VersionSortDigest uses latest digest of an image
24-
VersionSortDigest VersionSortMode = 3
24+
StrategyDigest UpdateStrategy = 3
2525
)
2626

2727
// ConstraintMatchMode defines how the constraint should be matched
@@ -42,7 +42,7 @@ type VersionConstraint struct {
4242
MatchFunc MatchFuncFn
4343
MatchArgs interface{}
4444
IgnoreList []string
45-
SortMode VersionSortMode
45+
Strategy UpdateStrategy
4646
Options *options.ManifestOptions
4747
}
4848

@@ -61,14 +61,14 @@ func (img *ContainerImage) GetNewestVersionFromTags(vc *VersionConstraint, tagLi
6161
logCtx.AddField("image", img.String())
6262

6363
var availableTags tag.SortableImageTagList
64-
switch vc.SortMode {
65-
case VersionSortSemVer:
64+
switch vc.Strategy {
65+
case StrategySemVer:
6666
availableTags = tagList.SortBySemVer()
67-
case VersionSortName:
67+
case StrategyName:
6868
availableTags = tagList.SortByName()
69-
case VersionSortLatest:
69+
case StrategyLatest:
7070
availableTags = tagList.SortByDate()
71-
case VersionSortDigest:
71+
case StrategyDigest:
7272
availableTags = tagList.SortByName()
7373
}
7474

@@ -82,7 +82,7 @@ func (img *ContainerImage) GetNewestVersionFromTags(vc *VersionConstraint, tagLi
8282
// The given constraint MUST match a semver constraint
8383
var semverConstraint *semver.Constraints
8484
var err error
85-
if vc.SortMode == VersionSortSemVer {
85+
if vc.Strategy == StrategySemVer {
8686
// TODO: Shall we really ensure a valid semver on the current tag?
8787
// This prevents updating from a non-semver tag currently.
8888
if img.ImageTag != nil && img.ImageTag.TagName != "" {
@@ -93,7 +93,7 @@ func (img *ContainerImage) GetNewestVersionFromTags(vc *VersionConstraint, tagLi
9393
}
9494

9595
if vc.Constraint != "" {
96-
if vc.SortMode == VersionSortSemVer {
96+
if vc.Strategy == StrategySemVer {
9797
semverConstraint, err = semver.NewConstraint(vc.Constraint)
9898
if err != nil {
9999
logCtx.Errorf("invalid constraint '%s' given: '%v'", vc, err)
@@ -107,7 +107,7 @@ func (img *ContainerImage) GetNewestVersionFromTags(vc *VersionConstraint, tagLi
107107
for _, tag := range availableTags {
108108
logCtx.Tracef("Finding out whether to consider %s for being updateable", tag.TagName)
109109

110-
if vc.SortMode == VersionSortSemVer {
110+
if vc.Strategy == StrategySemVer {
111111
// Non-parseable tag does not mean error - just skip it
112112
ver, err := semver.NewVersion(tag.TagName)
113113
if err != nil {
@@ -123,7 +123,7 @@ func (img *ContainerImage) GetNewestVersionFromTags(vc *VersionConstraint, tagLi
123123
continue
124124
}
125125
}
126-
} else if vc.SortMode == VersionSortDigest {
126+
} else if vc.Strategy == StrategyDigest {
127127
if tag.TagName != vc.Constraint {
128128
logCtx.Tracef("%s did not match contraint %s", tag.TagName, vc.Constraint)
129129
continue
@@ -156,20 +156,40 @@ func (vc *VersionConstraint) IsTagIgnored(tag string) bool {
156156
return false
157157
}
158158

159-
// IsCacheable returns true if we can safely cache tags for a given sort mode
160-
func (vsm VersionSortMode) IsCacheable() bool {
161-
switch vsm {
162-
case VersionSortDigest:
159+
// IsCacheable returns true if we can safely cache tags for strategy s
160+
func (s UpdateStrategy) IsCacheable() bool {
161+
switch s {
162+
case StrategyDigest:
163163
return false
164164
default:
165165
return true
166166
}
167167
}
168168

169-
// NeedsMetadata returns true if v requires image metadata to work correctly
170-
func (vsm VersionSortMode) NeedsMetadata() bool {
171-
switch vsm {
172-
case VersionSortLatest:
169+
// NeedsMetadata returns true if strategy s requires image metadata to work correctly
170+
func (s UpdateStrategy) NeedsMetadata() bool {
171+
switch s {
172+
case StrategyLatest:
173+
return true
174+
default:
175+
return false
176+
}
177+
}
178+
179+
// NeedsVersionConstraint returns true if strategy s requires a version constraint to be defined
180+
func (s UpdateStrategy) NeedsVersionConstraint() bool {
181+
switch s {
182+
case StrategyDigest:
183+
return true
184+
default:
185+
return false
186+
}
187+
}
188+
189+
// WantsOnlyConstraintTag returns true if strategy s only wants to inspect the tag specified by the constraint
190+
func (s UpdateStrategy) WantsOnlyConstraintTag() bool {
191+
switch s {
192+
case StrategyDigest:
173193
return true
174194
default:
175195
return false

pkg/image/version_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Test_LatestVersion(t *testing.T) {
8888
t.Run("Find the latest version using latest sortmode", func(t *testing.T) {
8989
tagList := newImageTagListWithDate([]string{"zz", "bb", "yy", "cc", "yy", "aa", "ll"})
9090
img := NewFromIdentifier("jannfis/test:bb")
91-
vc := VersionConstraint{SortMode: VersionSortLatest}
91+
vc := VersionConstraint{Strategy: StrategyLatest}
9292
newTag, err := img.GetNewestVersionFromTags(&vc, tagList)
9393
require.NoError(t, err)
9494
require.NotNil(t, newTag)
@@ -98,7 +98,7 @@ func Test_LatestVersion(t *testing.T) {
9898
t.Run("Find the latest version using latest sortmode, invalid tags", func(t *testing.T) {
9999
tagList := newImageTagListWithDate([]string{"zz", "bb", "yy", "cc", "yy", "aa", "ll"})
100100
img := NewFromIdentifier("jannfis/test:bb")
101-
vc := VersionConstraint{SortMode: VersionSortSemVer}
101+
vc := VersionConstraint{Strategy: StrategySemVer}
102102
newTag, err := img.GetNewestVersionFromTags(&vc, tagList)
103103
require.NoError(t, err)
104104
require.NotNil(t, newTag)

pkg/registry/registry.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ func (endpoint *RegistryEndpoint) GetTags(img *image.ContainerImage, regClient R
5252
tags := []string{}
5353

5454
// For digest strategy, we do require a version constraint
55-
if vc.SortMode == image.VersionSortDigest && vc.Constraint == "" {
56-
return nil, fmt.Errorf("cannot use digest strategy for image %s without a version constraint", img.Original())
55+
if vc.Strategy.NeedsVersionConstraint() && vc.Constraint == "" {
56+
return nil, fmt.Errorf("cannot use update strategy 'digest' for image '%s' without a version constraint", img.Original())
5757
}
5858

5959
// Loop through tags, removing those we do not want. If update strategy is
6060
// digest, all but the constraint tag are ignored.
61-
if vc.MatchFunc != nil || len(vc.IgnoreList) > 0 || vc.SortMode == image.VersionSortDigest {
61+
if vc.MatchFunc != nil || len(vc.IgnoreList) > 0 || vc.Strategy.WantsOnlyConstraintTag() {
6262
for _, t := range tTags {
63-
if (vc.MatchFunc != nil && !vc.MatchFunc(t, vc.MatchArgs)) || vc.IsTagIgnored(t) || (vc.SortMode == image.VersionSortDigest && t != vc.Constraint) {
63+
if (vc.MatchFunc != nil && !vc.MatchFunc(t, vc.MatchArgs)) || vc.IsTagIgnored(t) || (vc.Strategy.WantsOnlyConstraintTag() && t != vc.Constraint) {
6464
log.Tracef("Removing tag %s because it either didn't match defined pattern or is ignored", t)
6565
} else {
6666
tags = append(tags, t)
@@ -78,7 +78,7 @@ func (endpoint *RegistryEndpoint) GetTags(img *image.ContainerImage, regClient R
7878
//
7979
// We just create a dummy time stamp according to the registry's sort mode, if
8080
// set.
81-
if (vc.SortMode != image.VersionSortLatest && vc.SortMode != image.VersionSortDigest) || endpoint.TagListSort.IsTimeSorted() {
81+
if (vc.Strategy != image.StrategyLatest && vc.Strategy != image.StrategyDigest) || endpoint.TagListSort.IsTimeSorted() {
8282
for i, tagStr := range tags {
8383
var ts int
8484
if endpoint.TagListSort == SortLatestFirst {
@@ -106,7 +106,7 @@ func (endpoint *RegistryEndpoint) GetTags(img *image.ContainerImage, regClient R
106106
// Look into the cache first and re-use any found item. If GetTag() returns
107107
// an error, we treat it as a cache miss and just go ahead to invalidate
108108
// the entry.
109-
if vc.SortMode.IsCacheable() {
109+
if vc.Strategy.IsCacheable() {
110110
imgTag, err := endpoint.Cache.GetTag(nameInRegistry, tagStr)
111111
if err != nil {
112112
log.Warnf("invalid entry for %s:%s in cache, invalidating.", nameInRegistry, imgTag.TagName)
@@ -161,7 +161,7 @@ func (endpoint *RegistryEndpoint) GetTags(img *image.ContainerImage, regClient R
161161

162162
log.Tracef("Found date %s", ti.CreatedAt.String())
163163
var imgTag *tag.ImageTag
164-
if vc.SortMode == image.VersionSortDigest {
164+
if vc.Strategy == image.StrategyDigest {
165165
imgTag = tag.NewImageTag(tagStr, ti.CreatedAt, fmt.Sprintf("sha256:%x", ti.Digest))
166166
} else {
167167
imgTag = tag.NewImageTag(tagStr, ti.CreatedAt, "")

pkg/registry/registry_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Test_GetTags(t *testing.T) {
2727

2828
img := image.NewFromIdentifier("foo/bar:1.2.0")
2929

30-
tl, err := ep.GetTags(img, &regClient, &image.VersionConstraint{SortMode: image.VersionSortSemVer})
30+
tl, err := ep.GetTags(img, &regClient, &image.VersionConstraint{Strategy: image.StrategySemVer})
3131
require.NoError(t, err)
3232
assert.NotEmpty(t, tl)
3333

@@ -46,7 +46,7 @@ func Test_GetTags(t *testing.T) {
4646

4747
img := image.NewFromIdentifier("foo/bar:1.2.0")
4848

49-
tl, err := ep.GetTags(img, &regClient, &image.VersionConstraint{SortMode: image.VersionSortSemVer, MatchFunc: image.MatchFuncNone})
49+
tl, err := ep.GetTags(img, &regClient, &image.VersionConstraint{Strategy: image.StrategySemVer, MatchFunc: image.MatchFuncNone})
5050
require.NoError(t, err)
5151
assert.Empty(t, tl.Tags())
5252

@@ -66,7 +66,7 @@ func Test_GetTags(t *testing.T) {
6666

6767
img := image.NewFromIdentifier("foo/bar:1.2.0")
6868

69-
tl, err := ep.GetTags(img, &regClient, &image.VersionConstraint{SortMode: image.VersionSortName})
69+
tl, err := ep.GetTags(img, &regClient, &image.VersionConstraint{Strategy: image.StrategyName})
7070
require.NoError(t, err)
7171
assert.NotEmpty(t, tl)
7272

@@ -98,7 +98,7 @@ func Test_GetTags(t *testing.T) {
9898
ep.Cache.ClearCache()
9999

100100
img := image.NewFromIdentifier("foo/bar:1.2.0")
101-
tl, err := ep.GetTags(img, &regClient, &image.VersionConstraint{SortMode: image.VersionSortLatest})
101+
tl, err := ep.GetTags(img, &regClient, &image.VersionConstraint{Strategy: image.StrategyLatest})
102102
require.NoError(t, err)
103103
assert.NotEmpty(t, tl)
104104

0 commit comments

Comments
 (0)