apps-gitops/automation/generate-scrape-config.py
2025-06-26 02:43:44 +03:00

39 lines
1.1 KiB
Python

import os
from ruamel.yaml import YAML
from ruamel.yaml.scalarstring import LiteralScalarString
from pathlib import Path
# ... collect_targets stays the same
def write_scrape_config(jobs):
result = []
for repo, targets in jobs.items():
result.append({
"job_name": repo,
"static_configs": [{"targets": targets}]
})
scrape_yaml = "# This content will be auto-updated by the pipeline\n" + YAML().dump_to_string(result)
secret = {
"apiVersion": "v1",
"kind": "Secret",
"metadata": {
"name": "prometheus-additional-scrape-configs",
"namespace": "monitoring",
"labels": {
"app.kubernetes.io/name": "prometheus"
}
},
"type": "Opaque",
"stringData": {
"additional-scrape-configs.yaml": LiteralScalarString(scrape_yaml)
}
}
os.makedirs(os.path.dirname(OUTPUT_FILE), exist_ok=True)
with open(OUTPUT_FILE, "w") as f:
yaml_writer = YAML()
yaml_writer.default_flow_style = False
yaml_writer.dump(secret, f)