Skip to content

Commit

Permalink
fix: get image and tag parameter from getHelmParameterNamesFromAnnota…
Browse files Browse the repository at this point in the history
…tion

fix: set default values for image and tag parameters
change: get application source type from Spec intead of Status values

Signed-off-by: David Vidal Villamide <david@askharilabs.com>
  • Loading branch information
askhari committed Nov 22, 2023
1 parent 02b8500 commit cfbb5c3
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 12 deletions.
14 changes: 8 additions & 6 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,16 @@ func getApplicationSourceType(app *v1alpha1.Application) v1alpha1.ApplicationSou
}

if app.Spec.HasMultipleSources() {
for _, s := range app.Status.SourceTypes {
if s == v1alpha1.ApplicationSourceTypeKustomize || s == v1alpha1.ApplicationSourceTypeHelm {
return s
for _, s := range app.Spec.Sources {
if s.Helm != nil {
return v1alpha1.ApplicationSourceTypeHelm
} else if s.Kustomize != nil {
return v1alpha1.ApplicationSourceTypeKustomize
} else if s.Plugin != nil {
return v1alpha1.ApplicationSourceTypePlugin
}
}

log.WithContext().AddField("application", app.GetName()).Tracef("Could not determine if Source Type is Helm or Kustomize from multisource configuration. Returning first source type by default.")
return app.Status.SourceTypes[0]
return v1alpha1.ApplicationSourceTypeDirectory
}

return app.Status.SourceType
Expand Down
25 changes: 19 additions & 6 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,26 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
images := GetImagesFromApplication(app)

for _, c := range images {
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c.ImageName)
if helmAnnotationParamName == "" {
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageName)
}
if helmAnnotationParamVersion == "" {
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
}

helmParamName := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamName)
if helmParamName == nil {
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamName)
}

helmParamVersion := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamVersion)
if helmParamVersion == nil {
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
}

// Build string with YAML format to merge with originalData values
helmValues := fmt.Sprintf("%s: %s\n%s: %s",
app.Annotations[fmt.Sprintf(common.HelmParamImageNameAnnotation, c.ImageName)],
getHelmParam(appSource.Helm.Parameters, app.Annotations[fmt.Sprintf(common.HelmParamImageNameAnnotation, c.ImageName)]).Value,
app.Annotations[fmt.Sprintf(common.HelmParamImageTagAnnotation, c.ImageName)],
getHelmParam(appSource.Helm.Parameters, app.Annotations[fmt.Sprintf(common.HelmParamImageTagAnnotation, c.ImageName)]).Value,
)
helmValues := fmt.Sprintf("%s: %s\n%s: %s", helmAnnotationParamName, helmParamName.Value, helmAnnotationParamVersion, helmParamVersion.Value)

var mergedParams *conflate.Conflate
mergedParams, err = conflate.FromData(originalData, []byte(helmValues))
Expand Down
190 changes: 190 additions & 0 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,196 @@ replicas: 1
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Missing annotation image-tag for helmvalues write-back-target", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
"argocd-image-updater.argoproj.io/nginx.helm.image-name": "image.name",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "dockerimage.name",
Value: "nginx",
ForceString: true,
},
{
Name: "dockerimage.tag",
Value: "v1.0.0",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
Summary: v1alpha1.ApplicationSummary{
Images: []string{
"nginx:v0.0.0",
},
},
},
}

originalData := []byte(`random content`)
_, err := marshalParamsOverride(&app, originalData)
assert.Error(t, err)
assert.Equal(t, "could not find an image-tag annotation for image nginx", err.Error())
})

t.Run("Missing annotation image-name for helmvalues write-back-target", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
"argocd-image-updater.argoproj.io/nginx.helm.image-tag": "image.tag",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "image.name",
Value: "nginx",
ForceString: true,
},
{
Name: "image.tag",
Value: "v1.0.0",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
Summary: v1alpha1.ApplicationSummary{
Images: []string{
"nginx:v0.0.0",
},
},
},
}

originalData := []byte(`random content`)
_, err := marshalParamsOverride(&app, originalData)
assert.Error(t, err)
assert.Equal(t, "could not find an image-name annotation for image nginx", err.Error())
})

t.Run("Image-name annotation value not found in Helm source parameters list", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
"argocd-image-updater.argoproj.io/nginx.helm.image-name": "wrongimage.name",
"argocd-image-updater.argoproj.io/nginx.helm.image-tag": "image.tag",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "image.name",
Value: "nginx",
ForceString: true,
},
{
Name: "image.tag",
Value: "v1.0.0",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
Summary: v1alpha1.ApplicationSummary{
Images: []string{
"nginx:v0.0.0",
},
},
},
}

originalData := []byte(`random content`)
_, err := marshalParamsOverride(&app, originalData)
assert.Error(t, err)
assert.Equal(t, "wrongimage.name parameter not found", err.Error())
})

t.Run("Image-tag annotation value not found in Helm source parameters list", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Name: "testapp",
Annotations: map[string]string{
"argocd-image-updater.argoproj.io/image-list": "nginx",
"argocd-image-updater.argoproj.io/write-back-method": "git",
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
"argocd-image-updater.argoproj.io/nginx.helm.image-name": "image.name",
"argocd-image-updater.argoproj.io/nginx.helm.image-tag": "wrongimage.tag",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "image.name",
Value: "nginx",
ForceString: true,
},
{
Name: "image.tag",
Value: "v1.0.0",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
Summary: v1alpha1.ApplicationSummary{
Images: []string{
"nginx:v0.0.0",
},
},
},
}

originalData := []byte(`random content`)
_, err := marshalParamsOverride(&app, originalData)
assert.Error(t, err)
assert.Equal(t, "wrongimage.tag parameter not found", err.Error())
})

t.Run("Invalid parameters merge for Helm source with Helm values file", func(t *testing.T) {
app := v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
Expand Down

0 comments on commit cfbb5c3

Please sign in to comment.