Skip to content

Commit

Permalink
Allow updateMap to use provided sourceFile and mappingFile (#1588)
Browse files Browse the repository at this point in the history
* updateMap to use provided sourceFile and mappingFile

* remove comment, grammar fixes

* update readme, rename tooling

* fix mapping

* fix logging
  • Loading branch information
fanny-jiang authored Oct 25, 2024
1 parent c18c548 commit c652360
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ The motivation for creating this tool was to provide a way to support Datadog us

```bash

$ go build -o yaml-mapper .
$ go build -o helm-operator-mapper .

```

## How to use

### Mapping Helm YAML to DatadogAgent CRD Spec

If the desired conversion is between the `datadog` Helm chart and a `DatadogAgent` spec, use the provided `mapping_datadog_helm_to_datadogagent_crd.yaml` file as the mapping file. Otherwise, create your own using the following format:

```
Expand All @@ -29,7 +31,7 @@ Both the key and value are period-delimited instead of nested or indented, as in
Pass the source file and mapping file to the command:

```bash
$ ./yaml-mapper -sourceFile=source.yaml -mappingFile=mapping.yaml
$ ./helm-operator-mapper -sourceFile=source.yaml -mappingFile=mapping.yaml

```

Expand All @@ -42,5 +44,25 @@ By default the output is also printed to STDOUT; to disable this use the flag `-
## Example usage (using provided files)

```bash
$ ./yaml-mapper -sourceFile=example_source.yaml -mappingFile=mapping_datadog_helm_to_datadogagent_crd.yaml -prefixFile=example_prefix.yaml
$ ./helm-operator-mapper -sourceFile=example_source.yaml -mappingFile=mapping_datadog_helm_to_datadogagent_crd.yaml -prefixFile=example_prefix.yaml
```

### Updating Mapping File from a Source YAML

#### Update default Datadog Helm to DatadogAgent CRD mapping file with latest published Datadog Helm chart values.yaml

```bash
./helm-operator-mapper -updateMap
```

#### Update default Datadog Helm to DatadogAgent CRD mapping file with local Datadog values.yaml

```bash
./helm-operator-mapper -updateMap -sourceFile=../../charts/datadog/values.yaml
```

#### Update custom mapping file with custom source YAML file

```bash
./helm-operator-mapper -updateMap -sourceFile=<YOUR_SOURCE_FILE> -mappingFile=<YOUR_MAPPING_FILE>
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 24 additions & 15 deletions tools/yaml-mapper/main.go → tools/helm-operator-mapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"helm.sh/helm/v3/pkg/chartutil"
)

const defaultDDAMappingPath = "mapping_datadog_helm_to_datadogagent_crd.yaml"

func main() {

if len(os.Args) > 1 {
Expand All @@ -41,11 +43,11 @@ func main() {
var destFile string
var prefixFile string
var updateMap bool
flag.StringVar(&mappingFile, "mappingFile", "mapping.yaml", "path to mapping YAML file")
flag.StringVar(&sourceFile, "sourceFile", "source.yaml", "path to source YAML file")
flag.StringVar(&destFile, "destFile", "destination.yaml", "path to destination YAML file")
flag.StringVar(&prefixFile, "prefixFile", "", "path to prefix YAML file. The content in this file will be prepended to the output")
flag.BoolVar(&updateMap, "updateMap", false, "Update default Mapping file with latest Datadog Helm Chart values")
flag.StringVar(&mappingFile, "mappingFile", "", "Path to mapping YAML file. Example: mapping.yaml")
flag.StringVar(&sourceFile, "sourceFile", "", "Path to source YAML file. Example: source.yaml")
flag.StringVar(&destFile, "destFile", "destination.yaml", "Path to destination YAML file.")
flag.StringVar(&prefixFile, "prefixFile", "example_prefix.yaml", "Path to prefix YAML file. The content in this file will be prepended to the output.")
flag.BoolVar(&updateMap, "updateMap", false, fmt.Sprintf("Update 'mappingFile' with provided 'sourceFile'. (default false) If set to 'true', default mappingFile is %s and default sourceFile is latest published Datadog chart values.yaml.", defaultDDAMappingPath))

flag.Parse()

Expand All @@ -57,14 +59,20 @@ func main() {
fmt.Println("printOutput:", *printPtr)
fmt.Println("")

// If updating default mapping:
// - Create a temp source file for the latest chart values.yaml and use it as the sourceFile
// If updating mapping:
// Use latest datadog chart values.yaml as sourceFile if none provided
// Use default mappingFile if none provided
tmpSourceFile := ""
if updateMap {
mappingFile = "mapping_datadog_helm_to_datadogagent_crd.yaml"
tmpSourceFile = getLatestValuesFile()
sourceFile = tmpSourceFile
if sourceFile == "" {
tmpSourceFile = getLatestValuesFile()
sourceFile = tmpSourceFile
}
if mappingFile == "" {
mappingFile = defaultDDAMappingPath
}
}

// Read mapping file
mapping, err := os.ReadFile(mappingFile)
if err != nil {
Expand Down Expand Up @@ -116,9 +124,10 @@ func main() {
fmt.Println(e)
return
}
newMapYaml = `# This file maps keys from the Datadog Helm chart (YAML) to the DatadogAgent CustomResource spec (YAML).
if mappingFile == defaultDDAMappingPath || tmpSourceFile != "" {
newMapYaml = `# This file maps keys from the Datadog Helm chart (YAML) to the DatadogAgent CustomResource spec (YAML).
` + newMapYaml
}

if *printPtr {
fmt.Println("")
Expand All @@ -127,14 +136,14 @@ func main() {

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

fmt.Printf("Default mapping file, %s, successfully updated", mappingFile)
fmt.Printf("Mapping file, %s, successfully updated", mappingFile)
return
}
// Map values.yaml > DDA
// 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# This file maps keys from the Datadog Helm chart (YAML) to the DatadogAgent CustomResource spec (YAML).

agents.additionalLabels: spec.override.nodeAgent.labels
agents.affinity: spec.override.nodeAgent.affinity
agents.containers.agent.env: spec.override.nodeAgent.containers.agent.env
Expand Down Expand Up @@ -101,7 +100,7 @@ agents.podSecurity.defaultApparmor: ""
agents.podSecurity.podSecurityPolicy.create: ""
agents.podSecurity.privileged: spec.override.nodeAgent.containers.agent.securityContext.privileged
agents.podSecurity.seLinuxContext: spec.override.nodeAgent.containers.agent.securityContext.seLinuxContext
agents.podSecurity.seLinuxContext.rule:
agents.podSecurity.seLinuxContext.rule: ""
agents.podSecurity.seLinuxContext.seLinuxOptions.level: spec.override.nodeAgent.containers.agent.securityContext.seLinuxContext.level
agents.podSecurity.seLinuxContext.seLinuxOptions.role: spec.override.nodeAgent.containers.agent.securityContext.seLinuxContext.role
agents.podSecurity.seLinuxContext.seLinuxOptions.type: spec.override.nodeAgent.containers.agent.securityContext.seLinuxContext.type
Expand Down Expand Up @@ -134,7 +133,7 @@ agents.volumeMounts:
- spec.override.nodeAgent.containers.security-agent.volumeMounts
agents.volumes: spec.override.nodeAgent.volumes
clusterAgent.additionalLabels: spec.override.clusterAgent.labels
clusterAgent.admissionController.agentSidecarInjection.clusterAgentCommunicationEnabled: spec.features.admissionController.agentSidecarInjection.clusterAgentCommunicationEnabled
clusterAgent.admissionController.agentSidecarInjection.clusterAgentCommunicationEnabled: spec.features.admissionController.agentSidecarInjection.clusterAgentCommunicationEnabled
clusterAgent.admissionController.agentSidecarInjection.containerRegistry: spec.features.admissionController.agentSidecarInjection.registry
clusterAgent.admissionController.agentSidecarInjection.enabled: spec.features.admissionController.agentSidecarInjection.enabled
clusterAgent.admissionController.agentSidecarInjection.imageName: spec.features.admissionController.agentSidecarInjection.image.name
Expand Down Expand Up @@ -370,6 +369,7 @@ datadog.kubelet.podLogsPath: ""
datadog.kubelet.tlsVerify: spec.global.kubelet.tlsVerify
datadog.kubernetesEvents.collectedEventTypes: spec.features.eventCollection.collectEventTypes
datadog.kubernetesEvents.filteringEnabled: ""
datadog.kubernetesEvents.sourceDetectionEnabled: ""
datadog.kubernetesEvents.unbundleEvents: spec.features.eventCollection.unbundleEvents
datadog.kubernetesResourcesAnnotationsAsTags: spec.global.kubernetesResourcesAnnotationsAsTags
datadog.kubernetesResourcesLabelsAsTags: spec.global.kubernetesResourcesLabelsAsTags
Expand All @@ -394,6 +394,7 @@ datadog.orchestratorExplorer.customResources: spec.features.orchestratorExplorer
datadog.orchestratorExplorer.enabled: spec.features.orchestratorExplorer.enabled
datadog.originDetectionUnified.enabled: spec.global.originDetectionUnified.enabled
datadog.osReleasePath: ""
datadog.otelCollector.config: ""
datadog.otelCollector.enabled: ""
datadog.otelCollector.ports: ""
datadog.otlp.logs.enabled: ""
Expand Down Expand Up @@ -475,7 +476,6 @@ existingClusterAgent.join: ""
existingClusterAgent.serviceName: ""
existingClusterAgent.tokenSecretName: ""
fips.customFipsConfig: ""
# customFIPSConfig: data, items, name
fips.enabled: spec.global.fips.enabled
fips.image.digest: ""
fips.image.name: spec.global.fips.image.name
Expand All @@ -486,16 +486,15 @@ fips.local_address: spec.global.fips.localAddress
fips.port: spec.global.fips.port
fips.portRange: spec.global.fips.portRange
fips.resources: ""
#resources claims, limits, requests
fips.use_https: spec.global.fips.useHTTPS
fullnameOverride: ""
nameOverride: spec.global.localservice.nameOverride
kube-state-metrics.image: ""
kube-state-metrics.image.repository: ""
kube-state-metrics.nodeSelector.kubernetes.io/os: ""
kube-state-metrics.rbac.create: ""
kube-state-metrics.serviceAccount.create: ""
kube-state-metrics.serviceAccount.name: ""
nameOverride: spec.global.localservice.nameOverride
providers.aks.enabled: ""
providers.eks.ec2.useHostnameFromFile: ""
providers.gke.autopilot: ""
Expand Down

0 comments on commit c652360

Please sign in to comment.