in subcommands/install.py [0:0]
def _create_dashboard_configmaps(output_dir, namespace):
dashboards_dir = os.path.abspath("./dashboards")
output_dir = os.path.join(output_dir, "dashboards")
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for dir_path, _, files in os.walk(dashboards_dir):
for dashboard in files:
dashboard_path = os.path.join(dir_path, dashboard)
dashboard_name, ext = os.path.splitext(dashboard)
if ext == ".json":
source = f"--from-file={dashboard_path}"
elif ext == ".jsonnet":
json = _jsonnet.evaluate_file(dashboard_path, ext_codes={"publish": "false"})
source = f"--from-literal={dashboard_name}.json='{json}'"
else:
continue
output_file = f"{output_dir}/{dashboard_name}.dashboard.yaml"
command = (
f"kubectl create configmap {dashboard_name} -o yaml "
f"{source} --dry-run=client --namespace={namespace} "
f"> {output_file}"
)
try:
subprocess.check_output(command, shell=True)
except subprocess.CalledProcessError as err:
print(err.output)
with open(output_file, "r") as f:
dashboard_cm = yaml.load(f, Loader=yaml.SafeLoader)
dashboard_cm["metadata"]["labels"] = dict()
dashboard_cm["metadata"]["labels"]["grafana_dashboard"] = dashboard_name
dashboard_cm["data"][f"{dashboard_name}.json"] = dashboard_cm["data"][
f"{dashboard_name}.json"
].replace('"${DS_PROMETHEUS}"', "null")
with open(output_file, "w") as f:
yaml.dump(dashboard_cm, f)