Skip to content

Commit

Permalink
Fix catalogue code ordering issue (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
sed-i authored Oct 5, 2023
1 parent c4dbd23 commit ea9e3c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
import ipaddress
import logging
import socket
from typing import List, Optional, Union
from typing import Optional

from ops.charm import CharmBase
from ops.framework import BoundEvent, EventBase, EventSource, Object, ObjectEvents
from ops.framework import EventBase, EventSource, Object, ObjectEvents

LIBID = "fa28b361293b46668bcd1f209ada6983"
LIBAPI = 0
LIBPATCH = 5
LIBAPI = 1
LIBPATCH = 0

DEFAULT_RELATION_NAME = "catalogue"

logger = logging.getLogger(__name__)


class CatalogueItem:
"""`CatalogueItem` represents an application entry sent to a catalogue."""
"""`CatalogueItem` represents an application entry sent to a catalogue.
The icon is an iconify mdi string; see https://icon-sets.iconify.design/mdi.
"""

def __init__(self, name: str, url: str, icon: str, description: str = ""):
self.name = name
Expand All @@ -38,7 +41,6 @@ def __init__(
charm,
relation_name: str = DEFAULT_RELATION_NAME,
item: Optional[CatalogueItem] = None,
refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None,
):
super().__init__(charm, relation_name)
self._charm = charm
Expand All @@ -52,37 +54,10 @@ def __init__(
self.framework.observe(events.relation_departed, self._on_relation_changed)
self.framework.observe(events.relation_created, self._on_relation_changed)

self._register_refresh_event(refresh_event)

def _register_refresh_event(
self, refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None
):
if not refresh_event:
if len(self._charm.meta.containers) == 1:
if "kubernetes" in self._charm.meta.series:
# This is a podspec charm
refresh_event = [self._charm.on.update_status]
else:
# This is a sidecar/pebble charm
container = list(self._charm.meta.containers.values())[0]
refresh_event = [self._charm.on[container.name.replace("-", "_")].pebble_ready]
else:
logger.warning(
"%d containers are present in metadata.yaml and "
"refresh_event was not specified. Defaulting to update_status. "
"External address may not be set in a timely fashion.",
len(self._charm.meta.containers),
)
refresh_event = [self._charm.on.update_status]

else:
if not isinstance(refresh_event, list):
refresh_event = [refresh_event]

for ev in refresh_event:
self.framework.observe(ev, self._on_relation_changed)
def _on_relation_changed(self, _):
self._update_relation_data()

def _on_relation_changed(self, event):
def _update_relation_data(self):
if not self._charm.unit.is_leader():
return

Expand All @@ -95,6 +70,11 @@ def _on_relation_changed(self, event):
relation.data[self._charm.model.app]["url"] = self.unit_address(relation)
relation.data[self._charm.model.app]["icon"] = self._item.icon

def update_item(self, item: CatalogueItem):
"""Update the catalogue item."""
self._item = item
self._update_relation_data()

def unit_address(self, relation):
"""The unit address of the consumer, on which it is reachable.
Expand Down
38 changes: 17 additions & 21 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
RemoteConfigurationRequirer,
)
from charms.alertmanager_k8s.v1.alertmanager_dispatch import AlertmanagerProvider
from charms.catalogue_k8s.v0.catalogue import CatalogueConsumer, CatalogueItem
from charms.catalogue_k8s.v1.catalogue import CatalogueConsumer, CatalogueItem
from charms.grafana_k8s.v0.grafana_dashboard import GrafanaDashboardProvider
from charms.grafana_k8s.v0.grafana_source import GrafanaSourceProvider
from charms.karma_k8s.v0.karma_dashboard import KarmaProvider
Expand Down Expand Up @@ -148,26 +148,7 @@ def __init__(self, *args):
],
)

self.catalog = CatalogueConsumer(
charm=self,
refresh_event=[
self.ingress.on.ready, # pyright: ignore
self.ingress.on.revoked, # pyright: ignore
self.on["ingress"].relation_changed,
self.on.update_status,
self.on.config_changed, # also covers upgrade-charm
],
item=CatalogueItem(
name="Alertmanager",
icon="bell-alert",
url=self._external_url,
description=(
"Alertmanager receives alerts from supporting applications, such as "
"Prometheus or Loki, then deduplicates, groups and routes them to "
"the configured receiver(s)."
),
),
)
self.catalog = CatalogueConsumer(charm=self, item=self._catalogue_item)

# Core lifecycle events
self.framework.observe(self.on.config_changed, self._on_config_changed)
Expand Down Expand Up @@ -232,6 +213,19 @@ def set_ports(self):
for p in new_ports_to_open:
self.unit.open_port(p.protocol, p.port)

@property
def _catalogue_item(self) -> CatalogueItem:
return CatalogueItem(
name="Alertmanager",
icon="bell-alert",
url=self._external_url,
description=(
"Alertmanager receives alerts from supporting applications, such as "
"Prometheus or Loki, then deduplicates, groups and routes them to "
"the configured receiver(s)."
),
)

@property
def self_scraping_job(self):
"""The self-monitoring scrape job."""
Expand Down Expand Up @@ -450,6 +444,8 @@ def _common_exit_hook(self, update_ca_certs: bool = False) -> None:
self.unit.status = BlockedStatus(str(e))
return

self.catalog.update_item(item=self._catalogue_item)

self.unit.status = ActiveStatus()

def _on_server_cert_changed(self, _):
Expand Down

0 comments on commit ea9e3c4

Please sign in to comment.