Skip to content

Commit

Permalink
Merge branch 'swang392/add-yaml-tooling' into swang392/mapper-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fanny-jiang authored Oct 24, 2024
2 parents 03b35fc + 7ce89d4 commit 18b69e7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 68 deletions.
130 changes: 67 additions & 63 deletions tools/yaml-mapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ func main() {
fmt.Println("printOutput:", *printPtr)
fmt.Println("")

// Read mapping file
// If updating default mapping:
// - Create a temp source file for the latest chart values.yaml and use it as the sourceFile
tmpSourceFile := ""
if updateMap {
mappingFile = "mapping_datadog_helm_to_datadogagent_crd.yaml"
tmpSourceFile = getLatestValuesFile()
sourceFile = tmpSourceFile
}
// Read mapping file
mapping, err := os.ReadFile(mappingFile)
if err != nil {
fmt.Println(err)
Expand All @@ -77,6 +79,8 @@ func main() {

// Read source yaml file
source, err := os.ReadFile(sourceFile)

// Cleanup tmpSourceFile after it's been read
if tmpSourceFile != "" {
defer os.Remove(tmpSourceFile)
}
Expand All @@ -90,25 +94,48 @@ func main() {
return
}

// Create an interim map that that has period-delimited destination key as the key,
// and the value from the source.yaml for the value
// Create an interim map that that has period-delimited destination key as the key, and the value from the source.yaml for the value
var pathVal interface{}
var destKey interface{}
var ok bool
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 {
// Populate interim map with keys from latest chart's values.yaml
interim = parseValues(sourceValues, make(map[string]interface{}), "")
// Add back existing key values from mapping file
for sourceKey, sourceVal := range mappingValues {
if sourceVal == nil {
interimMap[sourceKey] = ""
interim[sourceKey] = ""
} else {
interimMap[sourceKey] = sourceVal
interim[sourceKey] = sourceVal
}
}
newMapYaml, e := chartutil.Values(interim).YAML()
if e != nil {
fmt.Println(e)
return
}
newMapYaml = `# This file maps keys from the Datadog Helm chart (YAML) to the DatadogAgent CustomResource spec (YAML).
` + newMapYaml

if *printPtr {
fmt.Println("")
fmt.Println(newMapYaml)
}

e = os.WriteFile(mappingFile, []byte(newMapYaml), 0660)
if e != nil {
fmt.Printf("Error updating default mapping yaml. %v", e)
return
}

fmt.Printf("Default mapping file, %s, successfully updated", mappingFile)
return
}
// Map values.yaml > DDA
for sourceKey := range mappingValues {
pathVal, _ = sourceValues.PathValue(sourceKey)
// If there is no corresponding key in the destination, then the pathVal will be nil
if pathVal == nil {
Expand All @@ -118,9 +145,8 @@ func main() {
destKey, ok = mappingValues[sourceKey]
rt := reflect.TypeOf(destKey)
if !ok || destKey == "" || destKey == nil {
// Continue through loop
fmt.Printf("Warning: key not found: %s\n", sourceKey)
continue
// Continue through loop
} 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 All @@ -132,62 +158,44 @@ func main() {
}

// Create final mapping with properly nested map keys (converted from period-delimited keys)
if !updateMap {
result := make(map[string]interface{})
for k, v := range interim {
result = makeTable(k, v, result)
}

// Pretty print to YAML format
out, err := chartutil.Values(result).YAML()
if err != nil {
fmt.Println(err)
return
}

// Read prefix yaml file
var prefix []byte
if prefixFile != "" {
prefix, err = os.ReadFile(prefixFile)
if err != nil {
fmt.Println(err)
return
}
}

if len(prefix) > 0 {
out = string(prefix) + out
}
result := make(map[string]interface{})
for k, v := range interim {
result = makeTable(k, v, result)
}

if *printPtr {
fmt.Println("")
fmt.Println(out)
}
// Pretty print to YAML format
out, err := chartutil.Values(result).YAML()
if err != nil {
fmt.Println(err)
return
}

err = os.WriteFile(destFile, []byte(out), 0660)
// Read prefix yaml file
var prefix []byte
if prefixFile != "" {
prefix, err = os.ReadFile(prefixFile)
if err != nil {
fmt.Println(err)
}

fmt.Println("YAML file successfully written to", destFile)
} else {
newMapYaml, e := chartutil.Values(interimMap).YAML()
if e != nil {
fmt.Println(e)
return
}
newMapYaml = `# This file maps keys from the Datadog Helm chart (YAML) to the DatadogAgent CustomResource spec (YAML).
}

` + newMapYaml
if len(prefix) > 0 {
out = string(prefix) + out
}

e = os.WriteFile(mappingFile, []byte(newMapYaml), 0660)
if e != nil {
fmt.Printf("Error updating default mapping yaml. %v", e)
return
}
if *printPtr {
fmt.Println("")
fmt.Println(out)
}

fmt.Printf("Default mapping file, %s, successfully updated", mappingFile)
err = os.WriteFile(destFile, []byte(out), 0660)
if err != nil {
fmt.Println(err)
}

fmt.Println("YAML file successfully written to", destFile)

return
}

Expand Down Expand Up @@ -273,14 +281,10 @@ func downloadYaml(url string, name string) string {
return ""
}

fmt.Printf("File downloaded and saved to temporary file: %s\n", tmpFile.Name())

// fmt.Printf("File downloaded and saved to temporary file: %s\n", tmpFile.Name())
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ datadog-crds.crds.datadogMetrics: ""
datadog-crds.crds.datadogPodAutoscalers: ""
datadog.apiKey: spec.global.credentials.apiKey
datadog.apiKeyExistingSecret: spec.global.credentials.apiSecret.secretName
datadog.apm: ""
datadog.apm.enabled: ""
datadog.apm.hostSocketPath: spec.features.apm.unixDomainSocketConfig.path
datadog.apm.instrumentation.disabledNamespaces: ""
Expand All @@ -308,8 +307,6 @@ datadog.apm.socketPath: ""
datadog.apm.useSocketVolume: ""
datadog.appKey: spec.global.credentials.appKey
datadog.appKeyExistingSecret: spec.global.credentials.appSecret.secretName
datadog.asm: ""
datadog.asm.iast: ""
datadog.asm.iast.enabled: ""
datadog.asm.sca.enabled: ""
datadog.asm.threats.enabled: ""
Expand Down Expand Up @@ -397,7 +394,6 @@ datadog.nodeLabelsAsTags: spec.global.nodeLabelsAsTags
datadog.orchestratorExplorer.container_scrubbing.enabled: spec.features.orchestratorExplorer.scrubContainers
datadog.orchestratorExplorer.customResources: spec.features.orchestratorExplorer.customResources
datadog.orchestratorExplorer.enabled: spec.features.orchestratorExplorer.enabled
datadog.originDetectionUnified: ""
datadog.originDetectionUnified.enabled: spec.global.originDetectionUnified.enabled
datadog.osReleasePath: ""
datadog.otelCollector.enabled: ""
Expand Down Expand Up @@ -453,7 +449,7 @@ datadog.securityAgent.runtime.securityProfile.autoSuppression.enabled: ""
datadog.securityAgent.runtime.securityProfile.enabled: spec.features.cws.securityProfiles.enabled
datadog.securityAgent.runtime.syscallMonitor.enabled: spec.features.cws.syscallMonitorEnabled
datadog.securityAgent.runtime.useSecruntimeTrack: ""
datadog.securityContext: ""
datadog.securityContext: spec.global.securityContext
datadog.securityContext.runAsUser: ""
datadog.serviceMonitoring.enabled: spec.features.usm.enabled
datadog.site: spec.global.site
Expand Down

0 comments on commit 18b69e7

Please sign in to comment.