Skip to content

Commit

Permalink
update catalogue library to v1 (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucabello authored Apr 24, 2024
1 parent 9a3670d commit c8963fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@
import ipaddress
import logging
import socket
import warnings
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 = 6
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 @@ -39,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 @@ -53,44 +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)

warnings.warn(
"charms.catalogue_k8s.v0.catalogue is deprecated. "
"Use charms.catalogue_k8s.v1.catalogue instead. "
"For more details, see https://github.com/canonical/catalogue-k8s-operator/issues/41.",
category=DeprecationWarning,
)

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 @@ -103,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
8 changes: 2 additions & 6 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import yaml
from blackbox import ConfigUpdateFailure, WorkloadManager
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.loki_k8s.v1.loki_push_api import LogForwarder
from charms.observability_libs.v0.kubernetes_compute_resources_patch import (
Expand Down Expand Up @@ -114,14 +114,10 @@ def __init__(self, *args):
self.framework.observe(self.ingress.on.revoked, self._handle_ingress)
self.catalog = CatalogueConsumer(
charm=self,
refresh_event=[
self.on.update_status,
self.on.config_changed, # also covers upgrade-charm
],
item=CatalogueItem(
name="Blackbox Exporter",
icon="box",
url=self._external_url,
icon="box-variant",
description=(
"Blackbox exporter allows blackbox probing of endpoints over a multitude of "
"protocols, including HTTP, HTTPS, DNS, TCP, ICMP and gRPC."
Expand Down

0 comments on commit c8963fe

Please sign in to comment.