From c3b57d6d702941ad69f306ccd4dc07b52f520a17 Mon Sep 17 00:00:00 2001 From: Luca Bello <36242061+lucabello@users.noreply.github.com> Date: Wed, 29 May 2024 09:52:54 +0200 Subject: [PATCH] regenerate nrpe jobs and alert rules on charm upgrade (#136) * add upgrade hook * fix: pin codespell to 2.3.0 --- lib/charms/nrpe_exporter/v0/nrpe_exporter.py | 18 +++++++++--------- tox.ini | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/charms/nrpe_exporter/v0/nrpe_exporter.py b/lib/charms/nrpe_exporter/v0/nrpe_exporter.py index 7ea3e8b..7dc7f1f 100644 --- a/lib/charms/nrpe_exporter/v0/nrpe_exporter.py +++ b/lib/charms/nrpe_exporter/v0/nrpe_exporter.py @@ -31,7 +31,7 @@ from typing import Any, Dict, List, Optional, Tuple import yaml -from ops.charm import CharmBase, RelationEvent, RelationRole +from ops.charm import CharmBase, RelationRole from ops.framework import ( EventBase, EventSource, @@ -203,14 +203,12 @@ class NrpeTargetsChangedEvent(EventBase): def __init__( self, handle, - relation_id, current_targets: List[Dict[str, Any]], current_alerts: List[Dict[str, Any]], removed_targets: List[str], removed_alerts: List[str], ): super().__init__(handle) - self.relation_id = relation_id self.current_targets = current_targets self.current_alerts = current_alerts self.removed_targets = removed_targets or [] @@ -219,7 +217,6 @@ def __init__( def snapshot(self): """Save target relation information.""" return { - "relation_id": self.relation_id, "current_targets": self.current_targets, "current_alerts": self.current_alerts, "removed_targets": self.removed_targets, @@ -228,7 +225,6 @@ def snapshot(self): def restore(self, snapshot): """Restore target relation information.""" - self.relation_id = snapshot["relation_id"] self.current_targets = _type_convert_stored(snapshot["current_targets"]) self.current_alerts = _type_convert_stored(snapshot["current_alerts"]) self.removed_targets = _type_convert_stored(snapshot["removed_targets"]) @@ -279,8 +275,15 @@ def __init__(self, charm: CharmBase, relation_names: Optional[dict] = None): events = self._charm.on[relation_name] self.framework.observe(events.relation_changed, self._on_nrpe_relation_changed) self.framework.observe(events.relation_departed, self._on_nrpe_relation_changed) + self.framework.observe(self._charm.on.upgrade_charm, self._on_upgrade_charm) - def _on_nrpe_relation_changed(self, event: RelationEvent): + def _on_upgrade_charm(self, _: EventBase): + self._regenerate_nrpe() + + def _on_nrpe_relation_changed(self, _: EventBase): + self._regenerate_nrpe() + + def _regenerate_nrpe(self): """Handle changes with related endpoints. Anytime there are changes in relations between the NRPE exporter @@ -290,8 +293,6 @@ def _on_nrpe_relation_changed(self, event: RelationEvent): Args: event: a `RelationEvent` signifying a change. """ - rel_id = event.relation.id - nrpe_endpoints = [] nrpe_alerts = [] # type: List[Dict] @@ -314,7 +315,6 @@ def _on_nrpe_relation_changed(self, event: RelationEvent): ] self.on.nrpe_targets_changed.emit( # pyright: ignore - relation_id=rel_id, current_targets=nrpe_endpoints, current_alerts=nrpe_alerts, removed_targets=removed_endpoints, diff --git a/tox.ini b/tox.ini index 73829bc..2a52d0a 100644 --- a/tox.ini +++ b/tox.ini @@ -41,7 +41,7 @@ description = Check code against coding style standards deps = black ruff - codespell + codespell<2.3.0 # https://github.com/codespell-project/codespell/issues/3430 commands = codespell {[vars]lib_path} codespell . --skip .git --skip .tox --skip build --skip lib --skip venv* --skip .mypy_cache @@ -106,4 +106,4 @@ deps = cosl -r{toxinidir}/requirements.txt commands = - pytest -v --tb native --log-cli-level=INFO -s {posargs} {[vars]tst_path}/interface \ No newline at end of file + pytest -v --tb native --log-cli-level=INFO -s {posargs} {[vars]tst_path}/interface