Skip to content

Commit

Permalink
Detect broken relations
Browse files Browse the repository at this point in the history
Signed-off-by: gatici <gulsum.atici@canonical.com>
  • Loading branch information
gatici committed Nov 24, 2023
1 parent 53c6c12 commit c66244d
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@
StatusBase,
WaitingStatus,
)
from ops.charm import CharmBase, EventBase, InstallEvent, RelationJoinedEvent, RemoveEvent
from ops.charm import (
CharmBase,
EventBase,
InstallEvent,
RelationBrokenEvent,
RelationJoinedEvent,
RemoveEvent,
)
from ops.main import main
from ops.pebble import Layer

Expand Down Expand Up @@ -97,6 +104,9 @@ def __init__(self, *args):
self._database = DatabaseRequires(
self, relation_name="database", database_name=DATABASE_NAME
)
self._database_relation_breaking = False
self._fiveg_nrf_relation_breaking = False
self._certificates_relation_breaking = False
self.framework.observe(self.on.collect_unit_status, self._on_collect_unit_status)
self.framework.observe(self.on.install, self._on_install)
self.framework.observe(self.on.remove, self._on_remove)
Expand Down Expand Up @@ -147,9 +157,14 @@ def _is_unit_in_non_active_status(self) -> Optional[StatusBase]: # noqa: C901
if invalid_configs := self._get_invalid_configs():
return BlockedStatus(f"The following configurations are not valid: {invalid_configs}")

for relation in ["fiveg-nrf", "database", "certificates"]:
if not self._relation_created(relation):
return BlockedStatus(f"Waiting for {relation} relation")
if not self.model.get_relation("fiveg-nrf") or self._fiveg_nrf_relation_breaking:
return BlockedStatus("Waiting for fiveg-nrf relation")

if not self.model.get_relation("database") or self._database_relation_breaking:
return BlockedStatus("Waiting for database relation")

if not self.model.get_relation("certificates") or self._certificates_relation_breaking:
return BlockedStatus("Waiting for certificates relation")

if not self._database_is_available():
return WaitingStatus("Waiting for the amf database to be available")
Expand All @@ -166,6 +181,9 @@ def _is_unit_in_non_active_status(self) -> Optional[StatusBase]: # noqa: C901
if not _get_pod_ip():
return WaitingStatus("Waiting for pod IP address to be available")

if not self._certificate_is_stored():
return WaitingStatus("Waiting for certificates to be stored")

try:
self._set_n2_information()
except ValueError:
Expand All @@ -181,6 +199,8 @@ def _on_collect_unit_status(self, event: CollectStatusEvent):
"""
if status := self._is_unit_in_non_active_status():
event.add_status(status)
else:
event.add_status(ActiveStatus())

def _on_install(self, event: InstallEvent) -> None:
client = Client()
Expand Down Expand Up @@ -239,20 +259,13 @@ def _configure_amf(self, event: EventBase) -> None: # noqa C901
event.defer()
return

if not self._certificate_is_stored():
self.unit.status = WaitingStatus("Waiting for certificates to be stored")
event.defer()
return

self._generate_config_file()
self._configure_amf_workload()
try:
self._set_n2_information()
except ValueError:
logger.info("Waiting for MetalLB to be enabled")
event.defer()
return
self.unit.status = ActiveStatus()

def _on_certificates_relation_created(self, event: EventBase) -> None:
"""Generates Private key."""
Expand All @@ -261,15 +274,15 @@ def _on_certificates_relation_created(self, event: EventBase) -> None:
return
self._generate_private_key()

def _on_certificates_relation_broken(self, event: EventBase) -> None:
def _on_certificates_relation_broken(self, event: RelationBrokenEvent) -> None:
"""Deletes TLS related artifacts and reconfigures AMF."""
if not self._amf_container.can_connect():
event.defer()
return
self._delete_private_key()
self._delete_csr()
self._delete_certificate()
self.unit.status = BlockedStatus("Waiting for certificates relation")
self._certificates_relation_breaking = True

def _on_certificates_relation_joined(self, event: EventBase) -> None:
"""Generates CSR and requests new certificate."""
Expand Down Expand Up @@ -308,21 +321,21 @@ def _on_certificate_expiring(self, event: CertificateExpiringEvent):
return
self._request_new_certificate()

def _on_nrf_broken(self, event: EventBase) -> None:
def _on_nrf_broken(self, event: RelationBrokenEvent) -> None:
"""Event handler for NRF relation broken.
Args:
event (NRFBrokenEvent): Juju event
"""
self.unit.status = BlockedStatus("Waiting for fiveg-nrf relation")
self._fiveg_nrf_relation_breaking = True

def _on_database_relation_broken(self, event: EventBase) -> None:
def _on_database_relation_broken(self, event: RelationBrokenEvent) -> None:
"""Event handler for database relation broken.
Args:
event: Juju event
"""
self.unit.status = BlockedStatus("Waiting for database relation")
self._database_relation_breaking = True

def _generate_private_key(self) -> None:
"""Generates and stores private key."""
Expand Down Expand Up @@ -434,7 +447,7 @@ def _on_n2_relation_joined(self, event: RelationJoinedEvent) -> None:
try:
self._set_n2_information()
except ValueError:
self.unit.status = BlockedStatus("Waiting for MetalLB to be enabled")
logger.info("Waiting for MetalLB to be enabled")
return

def _get_n2_amf_ip(self) -> Optional[str]:
Expand Down

0 comments on commit c66244d

Please sign in to comment.