Skip to content

Commit

Permalink
fetch-lib (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
sed-i authored Oct 6, 2023
1 parent 576f94b commit 58e7dc1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 58 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
4 changes: 3 additions & 1 deletion metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ resources:
prometheus-image:
type: oci-image
description: Container image for Prometheus
upstream-source: ubuntu/prometheus:2.46.0-22.04_stable
# Use ghcr image until the missing udpate-ca-certificates issue is resolved.
# https://github.com/canonical/prometheus-rock/issues/33
upstream-source: ghcr.io/canonical/prometheus:dev
36 changes: 15 additions & 21 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import yaml
from charms.alertmanager_k8s.v1.alertmanager_dispatch import AlertmanagerConsumer
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.observability_libs.v0.cert_handler import CertHandler
Expand Down Expand Up @@ -173,26 +173,7 @@ def __init__(self, *args):
refresh_event=self.cert_handler.on.cert_changed,
)

self.catalogue = CatalogueConsumer(
charm=self,
refresh_event=[
self.on.prometheus_pebble_ready,
self.on.leader_elected,
self.ingress.on.ready_for_unit,
self.ingress.on.revoked_for_unit,
self.on.config_changed, # also covers upgrade-charm
self.cert_handler.on.cert_changed,
],
item=CatalogueItem(
name="Prometheus",
icon="chart-line-variant",
url=self.external_url,
description=(
"Prometheus collects, stores and serves metrics as time series data, "
"alongside optional key-value pairs called labels."
),
),
)
self.catalogue = CatalogueConsumer(charm=self, item=self._catalogue_item)
self.tracing = TracingEndpointRequirer(self)

self.framework.observe(self.on.prometheus_pebble_ready, self._on_pebble_ready)
Expand Down Expand Up @@ -225,6 +206,18 @@ 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="Prometheus",
icon="chart-line-variant",
url=self.external_url,
description=(
"Prometheus collects, stores and serves metrics as time series data, "
"alongside optional key-value pairs called labels."
),
)

@property
def self_scraping_job(self):
"""Scrape config for "external" self monitoring.
Expand Down Expand Up @@ -572,6 +565,7 @@ def _configure(self, _):

self.remote_write_provider.update_endpoint()
self.grafana_source_provider.update_source(self.external_url)
self.catalogue.update_item(item=self._catalogue_item)

self.unit.status = ActiveStatus()

Expand Down

0 comments on commit 58e7dc1

Please sign in to comment.