|
| 1 | +# This workflow checks that the yaml-mapper mapping file is in sync with the keys |
| 2 | +# in charts/datadog/values.yaml. It fails if any key from values.yaml is missing |
| 3 | +# from charts/datadog/files/mapping_datadog_helm_to_datadogagent_crd.yaml. |
| 4 | +# |
| 5 | +# To update the mapping file, run from the yaml-mapper directory: |
| 6 | +# ./yaml-mapper -updateMap -sourceFile=../../charts/datadog/values.yaml |
| 7 | +# |
| 8 | +# This workflow is currently disabled (rename to .yaml to enable). |
| 9 | +name: Yaml-Mapper Tool |
| 10 | + |
| 11 | +on: |
| 12 | + pull_request: |
| 13 | + paths: |
| 14 | + - "charts/datadog/values.yaml" |
| 15 | + - "charts/datadog/files/mapping_datadog_helm_to_datadogagent_crd.yaml" |
| 16 | + workflow_dispatch: |
| 17 | +# Permission forced by repo-level setting; only elevate on job-level |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | + |
| 21 | +jobs: |
| 22 | + check-mapper-update: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 28 | + with: |
| 29 | + fetch-depth: 0 |
| 30 | + - name: Set up Python |
| 31 | + uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4.7.1 |
| 32 | + with: |
| 33 | + python-version: 3.12 |
| 34 | + |
| 35 | + - name: Install dependencies |
| 36 | + run: pip install pyyaml |
| 37 | + |
| 38 | + - name: Compare values.yaml with mapping file |
| 39 | + run: | |
| 40 | + python <<EOF |
| 41 | + import yaml |
| 42 | + import sys |
| 43 | + |
| 44 | + def flatten_dict(d, parent_key="", sep="."): |
| 45 | + """Recursively flattens a nested dictionary.""" |
| 46 | + items = [] |
| 47 | + for k, v in d.items(): |
| 48 | + new_key = f"{parent_key}{sep}{k}" if parent_key else k |
| 49 | + if isinstance(v, dict): |
| 50 | + items.extend(flatten_dict(v, new_key, sep=sep).items()) |
| 51 | + else: |
| 52 | + items.append((new_key, v)) |
| 53 | + return dict(items) |
| 54 | + |
| 55 | + values_file = "charts/datadog/values.yaml" |
| 56 | + mapping_file = "charts/datadog/files/mapping_datadog_helm_to_datadogagent_crd.yaml" |
| 57 | + |
| 58 | + with open(values_file, "r") as vf, open(mapping_file, "r") as mf: |
| 59 | + values_data = yaml.safe_load(vf) or {} |
| 60 | + mapping_data = yaml.safe_load(mf) or {} |
| 61 | + |
| 62 | + # Flatten values.yaml structure |
| 63 | + flattened_values = flatten_dict(values_data) |
| 64 | + |
| 65 | + # Extract keys from the mapping file |
| 66 | + mapping_keys = set(mapping_data.keys()) |
| 67 | + |
| 68 | + # Compare missing keys |
| 69 | + missing_keys = [key for key in flattened_values.keys() if key not in mapping_keys] |
| 70 | + |
| 71 | + if missing_keys: |
| 72 | + print("The following keys are missing in the mapping file:") |
| 73 | + for key in missing_keys: |
| 74 | + print(f"- {key}") |
| 75 | + print("To update the mapping file, run from the yaml-mapper directory:") |
| 76 | + print(" ./yaml-mapper -updateMap -sourceFile=../../charts/datadog/values.yaml") |
| 77 | + sys.exit(1) |
| 78 | + else: |
| 79 | + print("Mapper file is correctly updated!") |
| 80 | + EOF |
0 commit comments