Skip to content

Commit

Permalink
Fix image update & helm value writter
Browse files Browse the repository at this point in the history
  • Loading branch information
supercairos committed Mar 20, 2024
1 parent 3538c22 commit f6e8066
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
6 changes: 6 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
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 f6e8066

Please sign in to comment.