Skip to content

Commit

Permalink
fix values parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
fanny-jiang committed Oct 23, 2024
1 parent 718cab8 commit 4287f7d
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 6 deletions.
31 changes: 25 additions & 6 deletions tools/yaml-mapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func main() {
interim := make(map[string]interface{})
interimMap := make(map[string]interface{})

if updateMap {
interimMap = parseValues(sourceValues, make(map[string]interface{}), "")
}
for sourceKey, sourceVal := range mappingValues {
if updateMap {
if sourceVal == nil {
Expand All @@ -115,13 +118,9 @@ func main() {
destKey, ok = mappingValues[sourceKey]
rt := reflect.TypeOf(destKey)
if !ok || destKey == "" || destKey == nil {
// If updating mapping, add unknown key to interimMap
if updateMap {
interimMap[sourceKey] = ""
continue
}
// Continue through loop
fmt.Printf("Warning: key not found: %s\n", sourceKey)
continue
} else if rt.Kind() == reflect.Slice {
// Provide support for the case where one source key may map to multiple destination keys
for _, v := range destKey.([]interface{}) {
Expand Down Expand Up @@ -177,6 +176,10 @@ func main() {
fmt.Println(e)
return
}
newMapYaml = `# This file maps keys from the Datadog Helm chart (YAML) to the DatadogAgent CustomResource spec (YAML).
` + newMapYaml

e = os.WriteFile(mappingFile, []byte(newMapYaml), 0660)
if e != nil {
fmt.Printf("Error updating default mapping yaml. %v", e)
Expand Down Expand Up @@ -257,7 +260,7 @@ func downloadYaml(url string, name string) string {
return ""
}

tmpFile, err := os.CreateTemp("", fmt.Sprintf("%s.yaml*.", name))
tmpFile, err := os.CreateTemp("", fmt.Sprintf("%s.yaml.*", name))
if err != nil {
fmt.Printf("Error creating temporary file: %v\n", err)
return ""
Expand All @@ -274,3 +277,19 @@ func downloadYaml(url string, name string) string {

return tmpFile.Name()
}

// TODO: fix handling deprecated helm values keys
// TODO: preserve comments

func parseValues(sourceValues chartutil.Values, valuesMap map[string]interface{}, prefix string) map[string]interface{} {
for key, value := range sourceValues {
currentKey := prefix + key
valuesMap[currentKey] = ""

// If the value is a map, recursive call to get nested keys.
if nestedMap, ok := value.(map[string]interface{}); ok {
parseValues(nestedMap, valuesMap, currentKey+".")
}
}
return valuesMap
}
Loading

0 comments on commit 4287f7d

Please sign in to comment.