Skip to content

Commit bdcecc1

Browse files
fix: getHelmParamNamesFromAnnotation should take info from container image for prevent code duplication
Signed-off-by: Pasha Kostohrys <pavel@codefresh.io>
1 parent f84d0bc commit bdcecc1

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

pkg/argocd/argocd.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,29 +309,29 @@ func (client *argoCD) UpdateSpec(ctx context.Context, in *application.Applicatio
309309
// getHelmParamNamesFromAnnotation inspects the given annotations for whether
310310
// the annotations for specifying Helm parameter names are being set and
311311
// returns their values.
312-
func getHelmParamNamesFromAnnotation(annotations map[string]string, symbolicName string) (string, string) {
312+
func getHelmParamNamesFromAnnotation(annotations map[string]string, img *image.ContainerImage) (string, string) {
313313
// Return default values without symbolic name given
314-
if symbolicName == "" {
314+
if img.ImageAlias == "" {
315315
return "image.name", "image.tag"
316316
}
317317

318318
var annotationName, helmParamName, helmParamVersion string
319319

320320
// Image spec is a full-qualified specifier, if we have it, we return early
321-
annotationName = fmt.Sprintf(common.HelmParamImageSpecAnnotation, symbolicName)
322-
if param, ok := annotations[annotationName]; ok {
321+
param := img.GetParameterHelmImageSpec(annotations)
322+
if param != "" {
323323
log.Tracef("found annotation %s", annotationName)
324324
return strings.TrimSpace(param), ""
325325
}
326326

327-
annotationName = fmt.Sprintf(common.HelmParamImageNameAnnotation, symbolicName)
328-
if param, ok := annotations[annotationName]; ok {
327+
param = img.GetParameterHelmImageName(annotations)
328+
if param != "" {
329329
log.Tracef("found annotation %s", annotationName)
330330
helmParamName = param
331331
}
332332

333-
annotationName = fmt.Sprintf(common.HelmParamImageTagAnnotation, symbolicName)
334-
if param, ok := annotations[annotationName]; ok {
333+
param = img.GetParameterHelmImageTag(annotations)
334+
if param != "" {
335335
log.Tracef("found annotation %s", annotationName)
336336
helmParamVersion = param
337337
}

pkg/argocd/argocd_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
600600
fmt.Sprintf(common.HelmParamImageSpecAnnotation, "myimg"): "image.blub",
601601
fmt.Sprintf(common.HelmParamImageTagAnnotation, "myimg"): "image.blab",
602602
}
603-
name, tag := getHelmParamNamesFromAnnotation(annotations, "")
603+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
604+
ImageAlias: "",
605+
})
604606
assert.Equal(t, "image.name", name)
605607
assert.Equal(t, "image.tag", tag)
606608
})
@@ -610,7 +612,9 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
610612
fmt.Sprintf(common.HelmParamImageSpecAnnotation, "myimg"): "image.path",
611613
fmt.Sprintf(common.HelmParamImageTagAnnotation, "myimg"): "image.tag",
612614
}
613-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
615+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
616+
ImageAlias: "myimg",
617+
})
614618
assert.Equal(t, "image.path", name)
615619
assert.Empty(t, tag)
616620
})
@@ -620,7 +624,9 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
620624
fmt.Sprintf(common.HelmParamImageNameAnnotation, "myimg"): "image.name",
621625
fmt.Sprintf(common.HelmParamImageTagAnnotation, "myimg"): "image.tag",
622626
}
623-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
627+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
628+
ImageAlias: "myimg",
629+
})
624630
assert.Equal(t, "image.name", name)
625631
assert.Equal(t, "image.tag", tag)
626632
})
@@ -630,7 +636,9 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
630636
fmt.Sprintf(common.HelmParamImageNameAnnotation, "otherimg"): "image.name",
631637
fmt.Sprintf(common.HelmParamImageTagAnnotation, "otherimg"): "image.tag",
632638
}
633-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
639+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
640+
ImageAlias: "myimg",
641+
})
634642
assert.Empty(t, name)
635643
assert.Empty(t, tag)
636644
})
@@ -639,14 +647,18 @@ func Test_GetHelmParamAnnotations(t *testing.T) {
639647
annotations := map[string]string{
640648
fmt.Sprintf(common.HelmParamImageTagAnnotation, "myimg"): "image.tag",
641649
}
642-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
650+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
651+
ImageAlias: "myimg",
652+
})
643653
assert.Empty(t, name)
644654
assert.Equal(t, "image.tag", tag)
645655
})
646656

647657
t.Run("No suitable annotations found", func(t *testing.T) {
648658
annotations := map[string]string{}
649-
name, tag := getHelmParamNamesFromAnnotation(annotations, "myimg")
659+
name, tag := getHelmParamNamesFromAnnotation(annotations, &image.ContainerImage{
660+
ImageAlias: "myimg",
661+
})
650662
assert.Empty(t, name)
651663
assert.Empty(t, tag)
652664
})

pkg/argocd/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
419419
images := GetImagesAndAliasesFromApplication(app)
420420

421421
for _, c := range images {
422-
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c.NormalizedSymbolicName())
422+
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c)
423423

424424
if helmAnnotationParamName == "" {
425425
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageAlias)

pkg/image/options.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// GetParameterHelmImageName gets the value for image-name option for the image
1414
// from a set of annotations
1515
func (img *ContainerImage) GetParameterHelmImageName(annotations map[string]string) string {
16-
key := fmt.Sprintf(common.HelmParamImageNameAnnotation, img.NormalizedSymbolicName())
16+
key := fmt.Sprintf(common.HelmParamImageNameAnnotation, img.normalizedSymbolicName())
1717
val, ok := annotations[key]
1818
if !ok {
1919
return ""
@@ -24,7 +24,7 @@ func (img *ContainerImage) GetParameterHelmImageName(annotations map[string]stri
2424
// GetParameterHelmImageTag gets the value for image-tag option for the image
2525
// from a set of annotations
2626
func (img *ContainerImage) GetParameterHelmImageTag(annotations map[string]string) string {
27-
key := fmt.Sprintf(common.HelmParamImageTagAnnotation, img.NormalizedSymbolicName())
27+
key := fmt.Sprintf(common.HelmParamImageTagAnnotation, img.normalizedSymbolicName())
2828
val, ok := annotations[key]
2929
if !ok {
3030
return ""
@@ -35,7 +35,7 @@ func (img *ContainerImage) GetParameterHelmImageTag(annotations map[string]strin
3535
// GetParameterHelmImageSpec gets the value for image-spec option for the image
3636
// from a set of annotations
3737
func (img *ContainerImage) GetParameterHelmImageSpec(annotations map[string]string) string {
38-
key := fmt.Sprintf(common.HelmParamImageSpecAnnotation, img.NormalizedSymbolicName())
38+
key := fmt.Sprintf(common.HelmParamImageSpecAnnotation, img.normalizedSymbolicName())
3939
val, ok := annotations[key]
4040
if !ok {
4141
return ""
@@ -46,7 +46,7 @@ func (img *ContainerImage) GetParameterHelmImageSpec(annotations map[string]stri
4646
// GetParameterKustomizeImageName gets the value for image-spec option for the
4747
// image from a set of annotations
4848
func (img *ContainerImage) GetParameterKustomizeImageName(annotations map[string]string) string {
49-
key := fmt.Sprintf(common.KustomizeApplicationNameAnnotation, img.NormalizedSymbolicName())
49+
key := fmt.Sprintf(common.KustomizeApplicationNameAnnotation, img.normalizedSymbolicName())
5050
val, ok := annotations[key]
5151
if !ok {
5252
return ""
@@ -58,7 +58,7 @@ func (img *ContainerImage) GetParameterKustomizeImageName(annotations map[string
5858
// image from a set of annotations
5959
func (img *ContainerImage) HasForceUpdateOptionAnnotation(annotations map[string]string) bool {
6060
forceUpdateAnnotations := []string{
61-
fmt.Sprintf(common.ForceUpdateOptionAnnotation, img.NormalizedSymbolicName()),
61+
fmt.Sprintf(common.ForceUpdateOptionAnnotation, img.normalizedSymbolicName()),
6262
common.ApplicationWideForceUpdateOptionAnnotation,
6363
}
6464
var forceUpdateVal = ""
@@ -75,7 +75,7 @@ func (img *ContainerImage) HasForceUpdateOptionAnnotation(annotations map[string
7575
// image from a set of annotations
7676
func (img *ContainerImage) GetParameterUpdateStrategy(annotations map[string]string) UpdateStrategy {
7777
updateStrategyAnnotations := []string{
78-
fmt.Sprintf(common.UpdateStrategyAnnotation, img.NormalizedSymbolicName()),
78+
fmt.Sprintf(common.UpdateStrategyAnnotation, img.normalizedSymbolicName()),
7979
common.ApplicationWideUpdateStrategyAnnotation,
8080
}
8181
var updateStrategyVal = ""
@@ -123,7 +123,7 @@ func (img *ContainerImage) ParseUpdateStrategy(val string) UpdateStrategy {
123123
// default, to prevent accidental matches.
124124
func (img *ContainerImage) GetParameterMatch(annotations map[string]string) (MatchFuncFn, interface{}) {
125125
allowTagsAnnotations := []string{
126-
fmt.Sprintf(common.AllowTagsOptionAnnotation, img.NormalizedSymbolicName()),
126+
fmt.Sprintf(common.AllowTagsOptionAnnotation, img.normalizedSymbolicName()),
127127
common.ApplicationWideAllowTagsOptionAnnotation,
128128
}
129129
var allowTagsVal = ""
@@ -137,7 +137,7 @@ func (img *ContainerImage) GetParameterMatch(annotations map[string]string) (Mat
137137
if allowTagsVal == "" {
138138
// The old match-tag annotation is deprecated and will be subject to removal
139139
// in a future version.
140-
key := fmt.Sprintf(common.OldMatchOptionAnnotation, img.NormalizedSymbolicName())
140+
key := fmt.Sprintf(common.OldMatchOptionAnnotation, img.normalizedSymbolicName())
141141
val, ok := annotations[key]
142142
if ok {
143143
logCtx.Warnf("The 'tag-match' annotation is deprecated and subject to removal. Please use 'allow-tags' annotation instead.")
@@ -182,7 +182,7 @@ func (img *ContainerImage) ParseMatchfunc(val string) (MatchFuncFn, interface{})
182182
// GetParameterPullSecret retrieves an image's pull secret credentials
183183
func (img *ContainerImage) GetParameterPullSecret(annotations map[string]string) *CredentialSource {
184184
pullSecretAnnotations := []string{
185-
fmt.Sprintf(common.PullSecretAnnotation, img.NormalizedSymbolicName()),
185+
fmt.Sprintf(common.PullSecretAnnotation, img.normalizedSymbolicName()),
186186
common.ApplicationWidePullSecretAnnotation,
187187
}
188188
var pullSecretVal = ""
@@ -208,7 +208,7 @@ func (img *ContainerImage) GetParameterPullSecret(annotations map[string]string)
208208
// GetParameterIgnoreTags retrieves a list of tags to ignore from a comma-separated string
209209
func (img *ContainerImage) GetParameterIgnoreTags(annotations map[string]string) []string {
210210
ignoreTagsAnnotations := []string{
211-
fmt.Sprintf(common.IgnoreTagsOptionAnnotation, img.NormalizedSymbolicName()),
211+
fmt.Sprintf(common.IgnoreTagsOptionAnnotation, img.normalizedSymbolicName()),
212212
common.ApplicationWideIgnoreTagsOptionAnnotation,
213213
}
214214
var ignoreTagsVal = ""
@@ -242,7 +242,7 @@ func (img *ContainerImage) GetParameterIgnoreTags(annotations map[string]string)
242242
func (img *ContainerImage) GetPlatformOptions(annotations map[string]string, unrestricted bool) *options.ManifestOptions {
243243
logCtx := img.LogContext()
244244
var opts *options.ManifestOptions = options.NewManifestOptions()
245-
key := fmt.Sprintf(common.PlatformsAnnotation, img.NormalizedSymbolicName())
245+
key := fmt.Sprintf(common.PlatformsAnnotation, img.normalizedSymbolicName())
246246
val, ok := annotations[key]
247247
if !ok {
248248
if !unrestricted {
@@ -291,6 +291,6 @@ func ParsePlatform(platformID string) (string, string, string, error) {
291291
return os, arch, variant, nil
292292
}
293293

294-
func (img *ContainerImage) NormalizedSymbolicName() string {
294+
func (img *ContainerImage) normalizedSymbolicName() string {
295295
return strings.ReplaceAll(img.ImageAlias, "/", "_")
296296
}

0 commit comments

Comments
 (0)