Skip to content

Commit

Permalink
regenerate nrpe jobs and alert rules on charm upgrade (#136)
Browse files Browse the repository at this point in the history
* add upgrade hook

* fix: pin codespell to 2.3.0
  • Loading branch information
lucabello authored May 29, 2024
1 parent 5115a06 commit c3b57d6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions lib/charms/nrpe_exporter/v0/nrpe_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 []
Expand All @@ -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,
Expand All @@ -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"])
Expand Down Expand Up @@ -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
Expand All @@ -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]

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
pytest -v --tb native --log-cli-level=INFO -s {posargs} {[vars]tst_path}/interface

0 comments on commit c3b57d6

Please sign in to comment.