Skip to content
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

Improve exceptions handling and add can_connect guards #433

Merged
merged 10 commits into from
Jul 26, 2024
8 changes: 7 additions & 1 deletion lib/charms/loki_k8s/v1/loki_push_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def _alert_rules_error(self, event):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 11
LIBPATCH = 12

PYDEPS = ["cosl"]

Expand Down Expand Up @@ -2475,6 +2475,9 @@ def disable_inactive_endpoints(
container: Container, active_endpoints: Dict[str, str], topology: JujuTopology
):
"""Disable forwarding for inactive endpoints by checking against the Pebble plan."""
if not container.can_connect():
return
Abuelodelanada marked this conversation as resolved.
Show resolved Hide resolved

pebble_layer = container.get_plan().to_dict().get("log-targets", None)
if not pebble_layer:
return
Expand All @@ -2501,6 +2504,9 @@ def enable_endpoints(
container: Container, active_endpoints: Dict[str, str], topology: JujuTopology
):
"""Enable forwarding for the specified Loki endpoints."""
if not container.can_connect():
return

Abuelodelanada marked this conversation as resolved.
Show resolved Hide resolved
layer = Layer(
{ # pyright: ignore
"log-targets": _PebbleLogClient._build_log_targets(
Expand Down
3 changes: 3 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ def _update_config(self, config: dict) -> bool:
return False

def _update_cert(self):
# If Pebble is not ready, we do not proceed.
# This code will end up running anyway when Pebble is ready, because
# it will eventually be called from the `_configure()` method.
if not self._loki_container.can_connect():
return

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ def __init__(self, args):

def wait_output(self):
return ("v0.1.0", "")

def wait(self):
return ("v0.1.0", "")
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ deps =
black
ruff
commands =
ruff --fix {[vars]all_path}
ruff check --fix {[vars]all_path}
black {[vars]all_path}

# codespell pinned cause version 2.3.0 mistakenly considers joined words such as "assertIn" invalid
Expand All @@ -46,7 +46,7 @@ deps =
commands =
codespell {[vars]lib_path}
codespell . --skip .git --skip .tox --skip build --skip lib --skip venv --skip .mypy_cache
ruff {[vars]all_path}
ruff check {[vars]all_path}
black --check --diff {[vars]all_path}

[testenv:static-{charm,lib}]
Expand Down
Loading