Skip to content

Commit

Permalink
Allow write-back to git helm (fix overwrite code)
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Caire <[email protected]>
  • Loading branch information
supercairos committed Mar 20, 2024
1 parent 7d93c7a commit c23b342
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
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
39 changes: 31 additions & 8 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,15 @@ 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)
helmAnnotationParamName, helmAnnotationParamVersion := getHelmParamNamesFromAnnotation(app.Annotations, c.ImageAlias)
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", c.ImageAlias)
}
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", c.ImageAlias)
}

helmParamName := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamName)
Expand All @@ -437,11 +437,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 +486,24 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
return override, nil
}

func dotToObj(key string, value string) map[string]interface{} {
keys := strings.Split(key, ".")
obj := make(map[string]interface{})

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

0 comments on commit c23b342

Please sign in to comment.