Skip to content

Commit

Permalink
refactored mergeHelmValues function to use yaml.MapSlice to keep yaml…
Browse files Browse the repository at this point in the history
… file order

Signed-off-by: Aleksandr Petrov <[email protected]>
  • Loading branch information
burnb committed Jun 14, 2024
1 parent b412955 commit c698831
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
}

if strings.HasPrefix(app.Annotations[common.WriteBackTargetAnnotation], common.HelmPrefix) {
values := make(map[interface{}]interface{})
var values yaml.MapSlice
err = yaml.Unmarshal(originalData, &values)
if err != nil {
return nil, err
Expand Down Expand Up @@ -448,7 +448,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
}
newValues[helmAnnotationParamVersion] = helmParamVersion.Value
}
mergeHelmValues(values, newValues)
mergeHelmValues(&values, newValues)

override, err = yaml.Marshal(values)
} else {
Expand Down Expand Up @@ -495,21 +495,38 @@ func mergeHelmOverride(t *helmOverride, o *helmOverride) {
}
}

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

break
} else if _, ok := node[name]; !ok {
node[name] = make(map[interface{}]interface{})
for i := range *values {
if (*values)[i].Key == path[0] {
if len(path) == 1 {
(*values)[i].Value = newValue
} else if v, ok := (*values)[i].Value.(yaml.MapSlice); ok {
update(&v, path[1:], newValue)
(*values)[i].Value = v
}
return
}
node = node[name].(map[interface{}]interface{})
}

// If the key was not found and we're not at the end of the path, insert a new MapSlice
if len(path) > 1 {
newMapSlice := yaml.MapSlice{}
update(&newMapSlice, path[1:], newValue)
*values = append(*values, yaml.MapItem{Key: path[0], Value: newMapSlice})
} else if len(path) == 1 {
// If the key was not found and we're at the end of the path, insert a new key-value pair
*values = append(*values, yaml.MapItem{Key: path[0], Value: newValue})
}
}

for path, newValue := range newValues {
update(values, strings.Split(path, "."), newValue)
}
}

Expand Down

0 comments on commit c698831

Please sign in to comment.