-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[health-check] Refactor the config usage #9662
base: health-check-skeleton
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ dependencies = [ | |
"requests", | ||
"Jinja2", | ||
"PyYAML", | ||
"tomli", | ||
] | ||
maintainers = [ | ||
{name = "Pablo Suárez Hernández", email = "[email protected]"}, | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import functools | ||
import os | ||
from typing import Any, Dict | ||
import tomli | ||
from pathlib import Path | ||
import json | ||
import jinja2 | ||
|
||
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
CONFIG_DIR = os.path.join(BASE_DIR, "config") | ||
TEMPLATES_DIR = os.path.join(CONFIG_DIR, "templates") | ||
CONTAINERS_DIR = os.path.join(BASE_DIR, "containers") | ||
CONFIG_TOML_PATH = os.environ.get("HEALTH_CHECK_TOML", os.path.join(BASE_DIR, "config.toml")) | ||
|
||
@functools.lru_cache | ||
def _init_jinja_env() -> jinja2.Environment: | ||
return jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATES_DIR)) | ||
|
||
@functools.lru_cache | ||
def parse_config() -> Dict: | ||
if not os.path.exists(CONFIG_TOML_PATH): | ||
raise ValueError(f"Config file does not exist: {CONFIG_TOML_PATH}") | ||
|
||
with open(CONFIG_TOML_PATH, "rb") as f: | ||
conf = tomli.load(f) | ||
return conf | ||
|
||
def get_json_template_filepath(json_relative_path: str) -> str: | ||
return os.path.join(TEMPLATES_DIR, json_relative_path) | ||
|
||
def load_jinja_template(template: str) -> jinja2.Template: | ||
return _init_jinja_env().get_template(template) | ||
|
||
def load_dockerfile_dir(dockerfile_dir: str) -> str: | ||
agraul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return os.path.join(CONTAINERS_DIR, dockerfile_dir) | ||
|
||
def get_config_dir_path(component: str) -> str: | ||
return os.path.join(CONFIG_DIR, component) | ||
|
||
def load_prop(property: str) -> Any: | ||
res = parse_config().copy() | ||
for prop_part in property.split('.'): | ||
try: | ||
res = res.get(prop_part, {}) | ||
except (AttributeError, ValueError): | ||
res = None | ||
Comment on lines
+45
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to turn access errors into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you'd prefer we let the caller deal with the error in case it asks for non-existent value? In my mind, when you ask a config for a non-existent prop, the correct behavior is to return a non-existent value (i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The caller always needs to deal with the case that it asks for something from the config that can't be found ( The question is: do we force the caller to deal with it by raising an exception or are we okay with the caller ignoring that? Both ways are okay, but since we don't have any config validation, it might make sense to not silence lookup errors (e.g. because the config is incomplete and mandatory values are missing) at runtime |
||
break | ||
return res | ||
|
||
def write_config(component: str, config_file_path: str, content: str, is_json=False): | ||
basedir = Path(get_config_dir_path(component)) | ||
if not basedir.exists(): | ||
basedir.mkdir(parents=True) | ||
file_path = os.path.join(basedir, config_file_path) | ||
with open(file_path, "w") as file: | ||
if is_json: | ||
json.dump(content, file, indent=4) | ||
else: | ||
file.write(content) | ||
|
||
def get_config_file_path(component): | ||
return os.path.join(get_config_dir_path(component), "config.yaml") | ||
|
||
def get_sources_dir(component): | ||
return os.path.join(BASE_DIR, component) | ||
|
||
def get_grafana_config_dir(): | ||
return os.path.join(CONFIG_DIR, "grafana") | ||
|
||
def get_prometheus_config_dir(): | ||
return os.path.join(CONFIG_DIR, "prometheus") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[podman] | ||
network_name = "health-check-network" | ||
|
||
[loki] | ||
loki_container_name = "uyuni_health_check_loki" | ||
loki_port = 3100 | ||
jobs = ["cobbler", "postgresql", "rhn", "apache"] | ||
|
||
[logcli] | ||
logcli_container_name = "uyuni_health_check_logcli" | ||
logcli_image_name = "logcli" |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if I get it right, the idea is not to load the config once and then pass around the config object (which is in fact, nicer) but to allow the parsing function to be called every time needed and speed it up by caching the returned values, correct?