Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: writing helm values back to git #693

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,12 @@ func GetImagesFromApplication(app *v1alpha1.Application) image.ContainerImageLis
return images
}

func GetImagesFromImageList(app *v1alpha1.Application) image.ContainerImageList {
images := make(image.ContainerImageList, 0)
images = append(images, *parseImageList(app.Annotations)...)
return images
}

// GetApplicationTypeByName first retrieves application with given appName and
// returns its application type
func GetApplicationTypeByName(client ArgoCD, appName string) (ApplicationType, error) {
Expand Down Expand Up @@ -552,6 +558,9 @@ func getApplicationSourceType(app *v1alpha1.Application) v1alpha1.ApplicationSou
if st, set := app.Annotations[common.WriteBackTargetAnnotation]; set &&
strings.HasPrefix(st, common.KustomizationPrefix) {
return v1alpha1.ApplicationSourceTypeKustomize
} else if st, set := app.Annotations[common.WriteBackTargetAnnotation]; set &&
strings.HasPrefix(st, common.HelmPrefix) {
return v1alpha1.ApplicationSourceTypeHelm
}

if app.Spec.HasMultipleSources() {
Expand Down
48 changes: 40 additions & 8 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,20 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
}

if strings.HasPrefix(app.Annotations[common.WriteBackTargetAnnotation], common.HelmPrefix) {
images := GetImagesFromApplication(app)
images := GetImagesFromImageList(app)

for _, c := range images {
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c.ImageName)
imageName := c.ImageAlias
if c.ImageAlias == "" {
imageName = c.ImageName
}

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

helmParamName := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamName)
Expand All @@ -437,11 +442,16 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
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", helmAnnotationParamName, helmParamName.Value, helmAnnotationParamVersion, helmParamVersion.Value)

var mergedParams *conflate.Conflate
mergedParams, err = conflate.FromData(originalData, []byte(helmValues))
mergedParams, err = conflate.FromData(originalData)
if err != nil {
return nil, err
}

err = mergedParams.AddGo(
dotToObj(helmAnnotationParamName, helmParamName.Value),
dotToObj(helmAnnotationParamVersion, helmParamVersion.Value),
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -481,6 +491,28 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
return override, nil
}

func dotToObj(key string, value string) map[string]interface{} {
obj := make(map[string]interface{})
if key == "" {
return obj
}

keys := strings.Split(key, ".")

current := obj
for i, k := range keys {
if i == len(keys)-1 {
current[k] = value
} else {
currentMap := make(map[string]interface{})
current[k] = currentMap
current = currentMap
}
}

return obj
}

func mergeHelmOverride(t *helmOverride, o *helmOverride) {
for _, param := range o.Helm.Parameters {
idx := slices.IndexFunc(t.Helm.Parameters, func(tp v1alpha1.HelmParameter) bool { return tp.Name == param.Name })
Expand Down
42 changes: 36 additions & 6 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,8 +1320,9 @@ helm:

t.Run("Valid Helm source with Helm values file", func(t *testing.T) {
expected := `
image.name: nginx
image.tag: v1.0.0
image:
name: nginx
tag: v1.0.0
replicas: 1
`
app := v1alpha1.Application{
Expand Down Expand Up @@ -1366,8 +1367,9 @@ replicas: 1
}

originalData := []byte(`
image.name: nginx
image.tag: v0.0.0
image:
name: nginx
tag: v0.0.0
replicas: 1
`)
yaml, err := marshalParamsOverride(&app, originalData)
Expand Down Expand Up @@ -1512,7 +1514,10 @@ replicas: 1
},
}

originalData := []byte(`random content`)
originalData := []byte(`
random:
content: hello
`)
_, err := marshalParamsOverride(&app, originalData)
assert.Error(t, err)
assert.Equal(t, "wrongimage.name parameter not found", err.Error())
Expand Down Expand Up @@ -1560,7 +1565,10 @@ replicas: 1
},
}

originalData := []byte(`random content`)
originalData := []byte(`
random:
content: hello
`)
_, err := marshalParamsOverride(&app, originalData)
assert.Error(t, err)
assert.Equal(t, "wrongimage.tag parameter not found", err.Error())
Expand Down Expand Up @@ -1644,6 +1652,28 @@ replicas: 1
})
}

func Test_DotToObj(t *testing.T) {
t.Run("no value", func(t *testing.T) {
obj := dotToObj("", "val")
assert.Equal(t, map[string]interface{}{}, obj)
})

t.Run("single value", func(t *testing.T) {
obj := dotToObj("a", "val")
assert.Equal(t, map[string]interface{}{"a": "val"}, obj)
})

t.Run("multiple values", func(t *testing.T) {
obj := dotToObj("a.b.c", "val")
assert.Equal(t, map[string]interface{}{"a": map[string]interface{}{
"b": map[string]interface{}{
"c": "val",
},
},
}, obj)
})
}

func Test_GetWriteBackConfig(t *testing.T) {
t.Run("Valid write-back config - git", func(t *testing.T) {
app := v1alpha1.Application{
Expand Down
Loading