Skip to content

Commit

Permalink
fix: Could not update application spec: could not find an image-tag a…
Browse files Browse the repository at this point in the history
…nnotation for image

Signed-off-by: Cheng Fang <cfang@redhat.com>
  • Loading branch information
chengfang committed Aug 27, 2024
1 parent 5dba714 commit 26a34df
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
33 changes: 19 additions & 14 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"text/template"
"time"

"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v2"

"github.com/argoproj-labs/argocd-image-updater/ext/git"
"github.com/argoproj-labs/argocd-image-updater/pkg/common"
Expand All @@ -18,10 +21,6 @@ import (
"github.com/argoproj-labs/argocd-image-updater/pkg/log"
"github.com/argoproj-labs/argocd-image-updater/pkg/registry"
"github.com/argoproj-labs/argocd-image-updater/pkg/tag"

"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"gopkg.in/yaml.v2"
)

// Stores some statistics about the results of a run
Expand Down Expand Up @@ -451,28 +450,34 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
if helmAnnotationParamName == "" {
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageName)
}
// for image-spec annotation, helmAnnotationParamName holds image-spec annotation value,
// and helmAnnotationParamVersion is empty
if helmAnnotationParamVersion == "" {
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
if c.GetParameterHelmImageSpec(app.Annotations) == "" {
// not a full image-spec, so image-tag is required
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
}
} else {
// image-tag annotation is present, so continue to process image-tag
helmParamVersion := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamVersion)
if helmParamVersion == nil {
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
}
err = setHelmValue(helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
if err != nil {
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
}
}

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)
}

err = setHelmValue(helmNewValues, helmAnnotationParamName, helmParamName.Value)
if err != nil {
return nil, fmt.Errorf("failed to set image parameter name value: %v", err)
}
err = setHelmValue(helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
if err != nil {
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
}
}

override, err = yaml.Marshal(helmNewValues)
Expand Down
51 changes: 50 additions & 1 deletion pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,56 @@ replicas: 1
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Valid Helm source with Helm values file and image-spec", func(t *testing.T) {
expected := `
image.spec.foo: nginx:v1.0.0
replicas: 1
`
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-spec": "image.spec.foo",
},
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
RepoURL: "https://example.com/example",
TargetRevision: "main",
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "image.spec.foo",
Value: "nginx:v1.0.0",
ForceString: true,
},
},
},
},
},
Status: v1alpha1.ApplicationStatus{
SourceType: v1alpha1.ApplicationSourceTypeHelm,
Summary: v1alpha1.ApplicationSummary{
Images: []string{
"nginx:v0.0.0",
},
},
},
}

originalData := []byte(`
image.spec.foo: nginx:v0.0.0
replicas: 1
`)
yaml, err := marshalParamsOverride(&app, originalData)
require.NoError(t, err)
assert.NotEmpty(t, yaml)
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
})

t.Run("Valid Helm source with Helm values file with multiple images", func(t *testing.T) {
expected := `
nginx.image.name: nginx
Expand Down Expand Up @@ -1714,7 +1764,6 @@ replicas: 1
originalData := []byte(`random: yaml`)
_, 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) {
Expand Down

0 comments on commit 26a34df

Please sign in to comment.