Skip to content

Commit 3739fb3

Browse files
authored
Workaround: observe relation changed directly (#115)
* Workaround: observe relation changed directly * Pin websockets
1 parent 0c82137 commit 3739fb3

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/charms/observability_libs/v1/cert_handler.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
LIBID = "b5cd5cd580f3428fa5f59a8876dcbe6a"
7070
LIBAPI = 1
71-
LIBPATCH = 14
71+
LIBPATCH = 15
7272

7373
VAULT_SECRET_LABEL = "cert-handler-private-vault"
7474

@@ -127,7 +127,7 @@ class _RelationVaultBackend(_VaultBackend):
127127
_NEST_UNDER = "lib.charms.observability_libs.v1.cert_handler::vault"
128128
# This key needs to be relation-unique. If someone ever creates multiple Vault(_RelationVaultBackend)
129129
# instances backed by the same (peer) relation, they'll need to set different _NEST_UNDERs
130-
# for each _RelationVaultBackend instance or they'll be fighting over it.
130+
# for each _RelationVaultBackend instance, or they'll be fighting over it.
131131

132132
def __init__(self, charm: CharmBase, relation_name: str):
133133
self.charm = charm
@@ -344,6 +344,13 @@ def __init__(
344344
self.charm.on[self.certificates_relation_name].relation_joined, # pyright: ignore
345345
self._on_certificates_relation_joined,
346346
)
347+
# The following observer is a workaround. The tls-certificates lib sometimes fails to emit the custom
348+
# "certificate_available" event on relation changed. Not sure why this was happening. We certainly have some
349+
# tech debt here to address, but this workaround proved to work.
350+
self.framework.observe(
351+
self.charm.on[self.certificates_relation_name].relation_changed, # pyright: ignore
352+
self._on_certificate_available,
353+
)
347354
self.framework.observe(
348355
self.certificates.on.certificate_available, # pyright: ignore
349356
self._on_certificate_available,
@@ -366,7 +373,7 @@ def __init__(
366373
)
367374

368375
if refresh_events:
369-
logger.warn(
376+
logger.warning(
370377
"DEPRECATION WARNING. `refresh_events` is now deprecated. CertHandler will automatically refresh the CSR when necessary."
371378
)
372379

@@ -429,7 +436,7 @@ def enabled(self) -> bool:
429436
See also the `available` property.
430437
"""
431438
# We need to check for units as a temporary workaround because of https://bugs.launchpad.net/juju/+bug/2024583
432-
# This could in theory not work correctly on scale down to 0 but it is necessary for the moment.
439+
# This could in theory not work correctly on scale down to 0, but it is necessary for the moment.
433440

434441
if not self.relation:
435442
return False
@@ -636,7 +643,7 @@ def _on_all_certificates_invalidated(self, _: AllCertificatesInvalidatedEvent) -
636643
# Note: assuming "limit: 1" in metadata
637644
# The "certificates_relation_broken" event is converted to "all invalidated" custom
638645
# event by the tls-certificates library. Per convention, we let the lib manage the
639-
# relation and we do not observe "certificates_relation_broken" directly.
646+
# relation, and we do not observe "certificates_relation_broken" directly.
640647
self.vault.clear()
641648
# We do not generate a CSR here because the relation is gone.
642649
self.on.cert_changed.emit() # pyright: ignore

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ commands =
8888
description = Run integration tests
8989
deps =
9090
juju ~= 3.1.0
91+
# https://github.com/juju/python-libjuju/issues/1184
92+
websockets<14
9193
lightkube
9294
lightkube-models
9395
pytest

0 commit comments

Comments
 (0)