Skip to content

Commit dc3eb82

Browse files
author
Casey Morton
committed
Added annotation for image aliases to target a specific source index
Signed-off-by: Casey Morton <[email protected]>
1 parent 93f8f94 commit dc3eb82

File tree

6 files changed

+80
-20
lines changed

6 files changed

+80
-20
lines changed

pkg/argocd/argocd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ func FilterApplicationsForUpdate(apps []v1alpha1.Application, patterns []string,
198198
for sourceIndex, _ := range getApplicationTypes(&app) {
199199
// Check for valid application type
200200
if !IsValidApplicationTypeForSource(&app, sourceIndex) {
201-
logCtx.Infof("skipping app '%s' of type '%s' because it's not of supported source type", app.GetName(), GetSourceTypes(app.Status)[sourceIndex])
201+
logCtx.Infof("skipping application '%s' source index %d of type '%s' because it's not a supported source type", app.GetName(), sourceIndex, GetSourceTypes(app.Status)[sourceIndex])
202202
continue
203203
}
204204

205-
logCtx.Tracef("processing app '%s' of type '%v'", app.GetName(), app.Status.SourceType)
205+
logCtx.Tracef("processing application '%s' source index %s of type '%s'", app.GetName(), sourceIndex, GetSourceTypes(app.Status)[sourceIndex])
206206
imageList := parseImageList(annotations)
207207
appImages := ApplicationImages{}
208208
appImages.Application = app

pkg/argocd/git.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, sourceInd
247247
}
248248

249249
func writeOverrides(app *v1alpha1.Application, wbc *WriteBackConfig, sourceIndex int, gitC git.Client) (err error, skip bool) {
250-
logCtx := log.WithContext().AddField("application", app.GetName())
251-
logCtx = log.WithContext().AddField("source", sourceIndex)
250+
logCtx := log.WithContext().AddField("application", app.GetName()).AddField("source", sourceIndex)
252251
targetExists := true
253252
targetFile := path.Join(gitC.Root(), wbc.Targets[sourceIndex])
254253
_, err = os.Stat(targetFile)

pkg/argocd/update.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat
201201
vc.Strategy = applicationImage.GetParameterUpdateStrategy(updateConf.UpdateApp.Application.Annotations)
202202
vc.MatchFunc, vc.MatchArgs = applicationImage.GetParameterMatch(updateConf.UpdateApp.Application.Annotations)
203203
vc.IgnoreList = applicationImage.GetParameterIgnoreTags(updateConf.UpdateApp.Application.Annotations)
204+
vc.SourceIndex = applicationImage.GetSourceIndex(updateConf.UpdateApp.Application.Annotations)
204205
vc.Options = applicationImage.
205206
GetPlatformOptions(updateConf.UpdateApp.Application.Annotations, updateConf.IgnorePlatforms).
206207
WithMetadata(vc.Strategy.NeedsMetadata()).
@@ -378,15 +379,36 @@ func needsUpdate(updateableImage *image.ContainerImage, applicationImage *image.
378379
}
379380

380381
func setAppImage(app *v1alpha1.Application, img *image.ContainerImage) error {
382+
logCtx := img.LogContext()
381383
imageUpdated := false
382384
imageUpdatable := false
383385
for i, appType := range getApplicationTypes(app) {
384-
if appType == ApplicationTypeKustomize {
385-
imageUpdatable = true
386-
imageUpdated = SetKustomizeImageWithIndex(app, i, img) || imageUpdated
387-
} else if appType == ApplicationTypeHelm {
388-
imageUpdatable = true
389-
imageUpdated = SetHelmImageWithIndex(app, i, img) || imageUpdated
386+
logCtx.Tracef("Examining source index %d for update", i)
387+
if imgSourceIndex := img.GetSourceIndex(app.Annotations); imgSourceIndex == image.SourceIndexAll || i == int(imgSourceIndex) {
388+
logCtx.Debugf("Image source index %d - attempting to update source", i)
389+
if appType == ApplicationTypeKustomize {
390+
logCtx.Debugf("Image source index %d - attempting to update source as Kustomize application type", i)
391+
imageUpdatable = true
392+
if imageSourceUpdated := SetKustomizeImageWithIndex(app, i, img); imageSourceUpdated {
393+
logCtx.Debugf("Image source index %d - successfully updated source as Kustomize image", i)
394+
imageUpdated = true
395+
} else {
396+
logCtx.Debugf("Image source index %d - unable to update Kustomize images - no updatable images found", i)
397+
}
398+
} else if appType == ApplicationTypeHelm {
399+
logCtx.Debugf("Image source index %d - attempting to update source as Helm application type", i)
400+
imageUpdatable = true
401+
if imageSourceUpdated := SetHelmImageWithIndex(app, i, img); imageSourceUpdated {
402+
logCtx.Debugf("Image source index %d - successfully updated source as Helm parameters", i)
403+
imageUpdated = true
404+
} else {
405+
logCtx.Debugf("Image source index %d - unable to update source as helm parameters - no updatable images found", i)
406+
}
407+
} else {
408+
logCtx.Debugf("Image source index %d - skipping update to source due to unsupported application type", i)
409+
}
410+
} else {
411+
logCtx.Tracef("Skipping update of source index %d due to image source index %d", i, imgSourceIndex)
390412
}
391413
}
392414

pkg/common/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535
UpdateStrategyAnnotation = ImageUpdaterAnnotationPrefix + "/%s.update-strategy"
3636
PullSecretAnnotation = ImageUpdaterAnnotationPrefix + "/%s.pull-secret"
3737
PlatformsAnnotation = ImageUpdaterAnnotationPrefix + "/%s.platforms"
38+
SourceIndexAnnotation = ImageUpdaterAnnotationPrefix + "/%s.source-index"
3839
)
3940

4041
// Application-wide update strategy related annotations

pkg/image/options.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"regexp"
66
"runtime"
7+
"strconv"
78
"strings"
89

910
"github.com/argoproj-labs/argocd-image-updater/pkg/common"
@@ -71,7 +72,7 @@ func (img *ContainerImage) HasForceUpdateOptionAnnotation(annotations map[string
7172
return forceUpdateVal == "true"
7273
}
7374

74-
// GetParameterSort gets and validates the value for the sort option for the
75+
// GetParameterUpdateStrategy gets and validates the value for the update strategy for the
7576
// image from a set of annotations
7677
func (img *ContainerImage) GetParameterUpdateStrategy(annotations map[string]string) UpdateStrategy {
7778
updateStrategyAnnotations := []string{
@@ -205,6 +206,35 @@ func (img *ContainerImage) GetParameterPullSecret(annotations map[string]string)
205206
return credSrc
206207
}
207208

209+
func (img *ContainerImage) GetSourceIndex(annotations map[string]string) SourceIndex {
210+
sourceIndexAnnotation := fmt.Sprintf(common.SourceIndexAnnotation, img.normalizedSymbolicName())
211+
212+
var sourceIndexVal = ""
213+
if val, ok := annotations[sourceIndexAnnotation]; ok {
214+
sourceIndexVal = val
215+
}
216+
217+
logCtx := img.LogContext()
218+
if sourceIndexVal == "" {
219+
logCtx.Tracef("No source index found. Defaulting to all.")
220+
// Default is sort by version
221+
return SourceIndexAll
222+
}
223+
logCtx.Tracef("Found source index %s", sourceIndexVal)
224+
return img.ParseSourceIndex(sourceIndexVal)
225+
}
226+
227+
func (img *ContainerImage) ParseSourceIndex(sourceIndexVal string) SourceIndex {
228+
logCtx := img.LogContext()
229+
230+
if num, err := strconv.ParseInt(sourceIndexVal, 10, 0); err == nil {
231+
return SourceIndex(num)
232+
} else {
233+
logCtx.Warnf("Invalid sourceIndex. Defaulting to all. Unexpected results may occur.")
234+
return SourceIndexAll
235+
}
236+
}
237+
208238
// GetParameterIgnoreTags retrieves a list of tags to ignore from a comma-separated string
209239
func (img *ContainerImage) GetParameterIgnoreTags(annotations map[string]string) []string {
210240
ignoreTagsAnnotations := []string{

pkg/image/version.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const (
2424
StrategyDigest UpdateStrategy = 3
2525
)
2626

27+
type SourceIndex int
28+
29+
const (
30+
SourceIndexAll SourceIndex = -1
31+
)
32+
2733
func (us UpdateStrategy) String() string {
2834
switch us {
2935
case StrategySemVer:
@@ -53,12 +59,13 @@ const (
5359

5460
// VersionConstraint defines a constraint for comparing versions
5561
type VersionConstraint struct {
56-
Constraint string
57-
MatchFunc MatchFuncFn
58-
MatchArgs interface{}
59-
IgnoreList []string
60-
Strategy UpdateStrategy
61-
Options *options.ManifestOptions
62+
Constraint string
63+
MatchFunc MatchFuncFn
64+
MatchArgs interface{}
65+
IgnoreList []string
66+
Strategy UpdateStrategy
67+
Options *options.ManifestOptions
68+
SourceIndex SourceIndex
6269
}
6370

6471
type MatchFuncFn func(tagName string, pattern interface{}) bool
@@ -70,9 +77,10 @@ func (vc *VersionConstraint) String() string {
7077

7178
func NewVersionConstraint() *VersionConstraint {
7279
return &VersionConstraint{
73-
MatchFunc: MatchFuncNone,
74-
Strategy: StrategySemVer,
75-
Options: options.NewManifestOptions(),
80+
MatchFunc: MatchFuncNone,
81+
Strategy: StrategySemVer,
82+
SourceIndex: SourceIndexAll,
83+
Options: options.NewManifestOptions(),
7684
}
7785
}
7886

0 commit comments

Comments
 (0)