Skip to content

Commit e1646e4

Browse files
committed
refactored mergeHelmValues function to use yaml.MapSlice to keep yaml file order
Signed-off-by: Aleksandr Petrov <burnb83@gmail.com>
1 parent 40d0b45 commit e1646e4

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

pkg/argocd/update.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
415415
}
416416

417417
if strings.HasPrefix(app.Annotations[common.WriteBackTargetAnnotation], common.HelmPrefix) {
418-
values := make(map[interface{}]interface{})
418+
var values yaml.MapSlice
419419
err = yaml.Unmarshal(originalData, &values)
420420
if err != nil {
421421
return nil, err
@@ -448,7 +448,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
448448
}
449449
newValues[helmAnnotationParamVersion] = helmParamVersion.Value
450450
}
451-
mergeHelmValues(values, newValues)
451+
mergeHelmValues(&values, newValues)
452452

453453
override, err = yaml.Marshal(values)
454454
} else {
@@ -495,21 +495,38 @@ func mergeHelmOverride(t *helmOverride, o *helmOverride) {
495495
}
496496
}
497497

498-
func mergeHelmValues(values map[interface{}]interface{}, newValues map[string]string) {
499-
for fieldPath, newValue := range newValues {
500-
fields := strings.Split(fieldPath, ".")
501-
lastFieldIndex := len(fields) - 1
502-
node := values
503-
for i, name := range fields {
504-
if i == lastFieldIndex {
505-
node[name] = newValue
498+
func mergeHelmValues(values *yaml.MapSlice, newValues map[string]string) {
499+
var update func(values *yaml.MapSlice, path []string, newValue string)
500+
update = func(values *yaml.MapSlice, path []string, newValue string) {
501+
if len(path) == 0 {
502+
return
503+
}
506504

507-
break
508-
} else if _, ok := node[name]; !ok {
509-
node[name] = make(map[interface{}]interface{})
505+
for i := range *values {
506+
if (*values)[i].Key == path[0] {
507+
if len(path) == 1 {
508+
(*values)[i].Value = newValue
509+
} else if v, ok := (*values)[i].Value.(yaml.MapSlice); ok {
510+
update(&v, path[1:], newValue)
511+
(*values)[i].Value = v
512+
}
513+
return
510514
}
511-
node = node[name].(map[interface{}]interface{})
512515
}
516+
517+
// If the key was not found and we're not at the end of the path, insert a new MapSlice
518+
if len(path) > 1 {
519+
newMapSlice := yaml.MapSlice{}
520+
update(&newMapSlice, path[1:], newValue)
521+
*values = append(*values, yaml.MapItem{Key: path[0], Value: newMapSlice})
522+
} else if len(path) == 1 {
523+
// If the key was not found and we're at the end of the path, insert a new key-value pair
524+
*values = append(*values, yaml.MapItem{Key: path[0], Value: newValue})
525+
}
526+
}
527+
528+
for path, newValue := range newValues {
529+
update(values, strings.Split(path, "."), newValue)
513530
}
514531
}
515532

0 commit comments

Comments
 (0)