Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Fix: Adding relation between orc8r-orchestrator and orc8r-prometheus-…
Browse files Browse the repository at this point in the history
…cache (#36)

* Fix: Adding relation between orc8r-orchestrator and orc8r-prometheus-cache

* Fix: Fixing fetching relation name from metadata in orc8r_base_db lib

* Fixing static analysis

* Fixing static analysis

* Addressing review comments

* Update orchestrator-bundle/orc8r-libs/lib/charms/magma_orc8r_libs/v0/orc8r_base.py

Co-authored-by: Javier Sánchez Fernández <32944505+sanchezfdezjavier@users.noreply.github.com>

* Fixing lint

* Fixing application status in case of missing relations

* Fixing lint

Co-authored-by: Javier Sánchez Fernández <32944505+sanchezfdezjavier@users.noreply.github.com>
  • Loading branch information
Gmerold and sanchezfdezjavier authored May 31, 2022
1 parent e618ab7 commit ce0f021
Show file tree
Hide file tree
Showing 47 changed files with 345 additions and 172 deletions.
22 changes: 12 additions & 10 deletions orchestrator-bundle/bundle-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,18 @@ applications:
options:
metrics_count_limit: 500000
relations:
- - nms-magmalte:certifier
- orc8r-certifier:certifier
- - nms-magmalte:magma-orc8r-certifier
- orc8r-certifier:magma-orc8r-certifier
- - nms-magmalte:db
- postgresql-k8s:db
- - nms-nginx-proxy:certifier
- orc8r-certifier:certifier
- - nms-nginx-proxy:magma-orc8r-certifier
- orc8r-certifier:magma-orc8r-certifier
- - nms-nginx-proxy:magmalte
- nms-magmalte:magmalte
- - orc8r-accessd:db
- postgresql-k8s:db
- - orc8r-bootstrapper:certifier
- orc8r-certifier:certifier
- - orc8r-bootstrapper:magma-orc8r-certifier
- orc8r-certifier:magma-orc8r-certifier
- - orc8r-certifier:db
- postgresql-k8s:db
- - orc8r-configurator:db
Expand All @@ -224,12 +224,14 @@ relations:
- postgresql-k8s:db
- - orc8r-nginx:bootstrapper
- orc8r-bootstrapper:bootstrapper
- - orc8r-nginx:certifier
- orc8r-certifier:certifier
- - orc8r-nginx:magma-orc8r-certifier
- orc8r-certifier:magma-orc8r-certifier
- - orc8r-nginx:obsidian
- orc8r-obsidian:obsidian
- - orc8r-orchestrator:certifier
- orc8r-certifier:certifier
- - orc8r-orchestrator:magma-orc8r-certifier
- orc8r-certifier:magma-orc8r-certifier
- - orc8r-orchestrator:metrics-endpoint
- orc8r-prometheus-cache:metrics-endpoint
- - orc8r-policydb:db
- postgresql-k8s:db
- - orc8r-prometheus:alertmanager
Expand Down
4 changes: 3 additions & 1 deletion orchestrator-bundle/nms-magmalte-operator/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ provides:
requires:
db:
interface: pgsql
certifier:
limit: 1
magma-orc8r-certifier:
interface: magma-orc8r-certifier
limit: 1
33 changes: 20 additions & 13 deletions orchestrator-bundle/nms-magmalte-operator/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
from lightkube import Client
from lightkube.models.core_v1 import SecretVolumeSource, Volume, VolumeMount
from lightkube.resources.apps_v1 import StatefulSet
from ops.charm import ActionEvent, CharmBase
from ops.charm import (
ActionEvent,
CharmBase,
PebbleReadyEvent,
RelationChangedEvent,
RelationJoinedEvent,
)
from ops.framework import StoredState
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
Expand Down Expand Up @@ -44,7 +50,8 @@ def __init__(self, *args):
self._db.on.database_relation_joined, self._on_database_relation_joined
)
self.framework.observe(
self.on.certifier_relation_changed, self._on_certifier_relation_changed
self.on.magma_orc8r_certifier_relation_changed,
self._on_magma_orc8r_certifier_relation_changed,
)
self.framework.observe(
self.on.get_admin_credentials_action, self._on_get_admin_credentials
Expand All @@ -62,7 +69,7 @@ def __init__(self, *args):
},
)

def _on_magma_nms_magmalte_pebble_ready(self, event):
def _on_magma_nms_magmalte_pebble_ready(self, event: PebbleReadyEvent):
if not self._relations_ready:
event.defer()
return
Expand All @@ -85,7 +92,7 @@ def _create_master_nms_admin_user(self):
logger.info(message)
raise TimeoutError(message)

def _on_database_relation_joined(self, event):
def _on_database_relation_joined(self, event: RelationJoinedEvent):
"""
Event handler for database relation change.
- Sets the event.database field on the database joined event.
Expand All @@ -95,17 +102,17 @@ def _on_database_relation_joined(self, event):
in the relation event.
"""
if self.unit.is_leader():
event.database = self.DB_NAME
event.database = self.DB_NAME # type: ignore[attr-defined]
else:
event.defer()

def _on_certifier_relation_changed(self, event):
def _on_magma_orc8r_certifier_relation_changed(self, event: RelationChangedEvent):
"""Mounts certificates required by the magma-nms-magmalte."""
if not self._nms_certs_mounted:
self.unit.status = MaintenanceStatus("Mounting NMS certificates...")
self._mount_certifier_certs()

def _configure_pebble(self, event):
def _configure_pebble(self, event: PebbleReadyEvent):
"""Adds layer to pebble config if the proposed config is different from the current one."""
if self._container.can_connect():
plan = self._container.get_plan()
Expand Down Expand Up @@ -175,7 +182,7 @@ def _pebble_layer(self) -> Layer:
}
)

def _create_nms_admin_user_action(self, event):
def _create_nms_admin_user_action(self, event: ActionEvent):
self._create_nms_admin_user(
email=event.params["email"],
password=event.params["password"],
Expand Down Expand Up @@ -249,7 +256,7 @@ def _magma_nms_magmalte_volume_mounts(self) -> List[VolumeMount]:
@property
def _relations_ready(self) -> bool:
"""Checks whether required relations are ready."""
required_relations = ["certifier", "db"]
required_relations = ["magma-orc8r-certifier", "db"]
if missing_relations := [
relation for relation in required_relations if not self.model.get_relation(relation)
]:
Expand Down Expand Up @@ -279,9 +286,9 @@ def _nms_certs_mounted(self) -> bool:
def _domain_name(self):
"""Returns domain name provided by the orc8r-certifier relation."""
try:
certifier_relation = self.model.get_relation("certifier")
units = certifier_relation.units
return certifier_relation.data[next(iter(units))]["domain"]
certifier_relation = self.model.get_relation("magma-orc8r-certifier")
units = certifier_relation.units # type: ignore[union-attr]
return certifier_relation.data[next(iter(units))]["domain"] # type: ignore[union-attr]
except (KeyError, StopIteration):
return None

Expand All @@ -290,7 +297,7 @@ def _get_db_connection_string(self):
"""Returns DB connection string provided by the DB relation."""
try:
db_relation = self.model.get_relation("db")
return ConnectionString(db_relation.data[db_relation.app]["master"])
return ConnectionString(db_relation.data[db_relation.app]["master"]) # type: ignore[index, union-attr] # noqa: E501
except (AttributeError, KeyError):
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ def test_given_charm_when_pebble_ready_event_emitted_and_no_relations_establishe

self.assertEqual(
self.harness.charm.unit.status,
BlockedStatus("Waiting for relation(s) to be created: certifier, db"),
BlockedStatus("Waiting for relation(s) to be created: magma-orc8r-certifier, db"),
)

def test_given_charm_when_pebble_ready_event_emitted_and_certifier_relation_is_established_but_db_relation_is_missing_then_charm_goes_to_blocked_state( # noqa: E501
self,
):
event = Mock()
relation_id = self.harness.add_relation("certifier", "orc8r-certifier")
relation_id = self.harness.add_relation("magma-orc8r-certifier", "orc8r-certifier")
self.harness.add_relation_unit(relation_id, "orc8r-certifier/0")

self.harness.charm.on.magma_nms_magmalte_pebble_ready.emit(event)
Expand Down
4 changes: 3 additions & 1 deletion orchestrator-bundle/nms-nginx-proxy-operator/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ resources:
upstream-source: nginx:latest

requires:
certifier:
magma-orc8r-certifier:
interface: magma-orc8r-certifier
limit: 1
magmalte:
interface: nms-magmalte
limit: 1
16 changes: 9 additions & 7 deletions orchestrator-bundle/nms-nginx-proxy-operator/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from lightkube.resources.apps_v1 import StatefulSet
from lightkube.resources.core_v1 import ConfigMap
from ops.charm import CharmBase
from ops.charm import CharmBase, PebbleReadyEvent, RelationChangedEvent, RemoveEvent
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
from ops.pebble import Layer
Expand All @@ -34,7 +34,9 @@ def __init__(self, *args):
self.framework.observe(
self.on.magma_nms_nginx_proxy_pebble_ready, self._on_magma_nms_nginx_proxy_pebble_ready
)
self.framework.observe(self.on.certifier_relation_changed, self._configure_nginx)
self.framework.observe(
self.on.magma_orc8r_certifier_relation_changed, self._configure_nginx
)
self.framework.observe(self.on.remove, self._on_remove)
self.service_patcher = KubernetesServicePatch(
charm=self,
Expand All @@ -44,14 +46,14 @@ def __init__(self, *args):
additional_labels={"app.kubernetes.io/part-of": "magma"},
)

def _on_magma_nms_nginx_proxy_pebble_ready(self, event):
def _on_magma_nms_nginx_proxy_pebble_ready(self, event: PebbleReadyEvent):
"""Configures magma-nms-nginx-proxy pebble layer."""
if not self._relations_ready:
event.defer()
return
self._configure_pebble(event)

def _configure_pebble(self, event):
def _configure_pebble(self, event: PebbleReadyEvent):
if self._container.can_connect():
self.unit.status = MaintenanceStatus(
f"Configuring pebble layer for {self._service_name}..."
Expand All @@ -66,7 +68,7 @@ def _configure_pebble(self, event):
self.unit.status = WaitingStatus("Waiting for container to be ready...")
event.defer()

def _configure_nginx(self, event):
def _configure_nginx(self, event: RelationChangedEvent):
if not self._nms_certs_mounted:
self.unit.status = MaintenanceStatus("Mounting NMS certificates...")
self._mount_certifier_certs()
Expand Down Expand Up @@ -112,7 +114,7 @@ def _mount_certifier_certs(self) -> None:
@property
def _relations_ready(self) -> bool:
"""Checks whether required relations are ready."""
required_relations = ["certifier", "magmalte"]
required_relations = ["magma-orc8r-certifier", "magmalte"]
missing_relations = [
relation for relation in required_relations if not self.model.get_relation(relation)
]
Expand Down Expand Up @@ -194,7 +196,7 @@ def _nginx_proxy_etc_configmap_created(self) -> bool:
except httpx.HTTPError:
return False

def _on_remove(self, event):
def _on_remove(self, event: RemoveEvent):
client = Client()
client.delete(ConfigMap, name="nginx-proxy-etc", namespace=self._namespace)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_given_charm_when_pebble_ready_event_emitted_and_no_relations_establishe
self.harness.charm.on.magma_nms_nginx_proxy_pebble_ready.emit(event)
self.assertEqual(
self.harness.charm.unit.status,
BlockedStatus("Waiting for relations: certifier, magmalte"),
BlockedStatus("Waiting for relations: magma-orc8r-certifier, magmalte"),
)

def test_given_charm_when_pebble_ready_event_emitted_and_certifier_relation_is_missing_then_charm_goes_to_blocked_state( # noqa: E501
Expand All @@ -41,15 +41,17 @@ def test_given_charm_when_pebble_ready_event_emitted_and_certifier_relation_is_m
self.harness.charm.on.magma_nms_nginx_proxy_pebble_ready.emit(event)
self.assertEqual(
self.harness.charm.unit.status,
BlockedStatus("Waiting for relations: certifier"),
BlockedStatus("Waiting for relations: magma-orc8r-certifier"),
)

def test_given_charm_when_pebble_ready_event_emitted_and_magmalte_relation_is_missing_then_charm_goes_to_blocked_state( # noqa: E501
self,
):
self.harness.disable_hooks()
event = Mock()
orc8r_relation_id = self.harness.add_relation("certifier", "orc8r-orchestrator")
orc8r_relation_id = self.harness.add_relation(
"magma-orc8r-certifier", "orc8r-orchestrator"
)
self.harness.add_relation_unit(orc8r_relation_id, "orc8r-orchestrator/0")
self.harness.enable_hooks()
self.harness.charm.on.magma_nms_nginx_proxy_pebble_ready.emit(event)
Expand All @@ -63,7 +65,9 @@ def test_given_charm_when_pebble_ready_event_emitted_and_required_relations_are_
):
self.harness.disable_hooks()
event = Mock()
orc8r_relation_id = self.harness.add_relation("certifier", "orc8r-orchestrator")
orc8r_relation_id = self.harness.add_relation(
"magma-orc8r-certifier", "orc8r-orchestrator"
)
self.harness.add_relation_unit(orc8r_relation_id, "orc8r-orchestrator/0")
magmalte_relation_id = self.harness.add_relation("magmalte", "nms-magmalte")
self.harness.add_relation_unit(magmalte_relation_id, "nms-magmalte/0")
Expand All @@ -75,7 +79,7 @@ def test_given_charm_when_pebble_ready_event_emitted_and_required_relations_are_
def test_given_charm_when_certifier_relation_added_then_configure_nginx_action_called(self):
event = Mock()
with patch.object(MagmaNmsNginxProxyCharm, "_configure_nginx", event) as mock:
relation_id = self.harness.add_relation("certifier", "orc8r-certifier")
relation_id = self.harness.add_relation("magma-orc8r-certifier", "orc8r-certifier")
self.harness.add_relation_unit(relation_id, "orc8r-certifier/0")
self.harness.update_relation_data(relation_id, "orc8r-certifier/0", {"key": "value"})
mock.assert_called_once()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _get_db_connection_string(self):
"""Returns DB connection string provided by the DB relation."""
try:
db_relation = self.model.get_relation("db")
return ConnectionString(db_relation.data[db_relation.app]["master"])
return ConnectionString(db_relation.data[db_relation.app]["master"]) # type: ignore[index, union-attr] # noqa: E501
except (AttributeError, KeyError):
return None

Expand Down
1 change: 1 addition & 0 deletions orchestrator-bundle/orc8r-accessd-operator/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ resources:
requires:
db:
interface: pgsql
limit: 1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ provides:
interface: magma-orc8r-bootstrapper

requires:
certifier:
magma-orc8r-certifier:
interface: magma-orc8r-certifier
limit: 1
db:
interface: pgsql
limit: 1
15 changes: 8 additions & 7 deletions orchestrator-bundle/orc8r-bootstrapper-operator/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from lightkube.core.exceptions import ApiError
from lightkube.models.core_v1 import SecretVolumeSource, Volume, VolumeMount
from lightkube.resources.apps_v1 import StatefulSet
from ops.charm import CharmBase
from ops.charm import CharmBase, PebbleReadyEvent, RelationJoinedEvent
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
from ops.pebble import Layer
Expand All @@ -27,18 +27,19 @@ def __init__(self, *args):
self._on_magma_orc8r_bootstrapper_pebble_ready,
)
self.framework.observe(
self.on.certifier_relation_joined, self._on_certifier_relation_joined
self.on.magma_orc8r_certifier_relation_joined,
self._on_magma_orc8r_certifier_relation_joined,
)
self._service_patcher = KubernetesServicePatch(
charm=self,
ports=[("grpc", 9180, 9088)],
additional_labels={"app.kubernetes.io/part-of": "orc8r-app"},
)

def _on_magma_orc8r_bootstrapper_pebble_ready(self, event):
def _on_magma_orc8r_bootstrapper_pebble_ready(self, event: PebbleReadyEvent):
"""Triggered when pebble is ready."""
if not self._certifier_relation_ready:
self.unit.status = BlockedStatus("Waiting for orc8r-certifier relation...")
self.unit.status = BlockedStatus("Waiting for magma-orc8r-certifier relation...")
event.defer()
return
if not self._orc8r_certs_mounted:
Expand All @@ -47,12 +48,12 @@ def _on_magma_orc8r_bootstrapper_pebble_ready(self, event):
return
self._configure_pebble(event)

def _on_certifier_relation_joined(self, event):
def _on_magma_orc8r_certifier_relation_joined(self, event: RelationJoinedEvent):
if not self._orc8r_certs_mounted:
self.unit.status = MaintenanceStatus("Mounting certificates from orc8r-certifier...")
self._mount_orc8r_certs()

def _configure_pebble(self, event):
def _configure_pebble(self, event: PebbleReadyEvent):
"""Adds layer to pebble config if the proposed config is different from the current one."""
if self._container.can_connect():
pebble_layer = self._pebble_layer
Expand Down Expand Up @@ -92,7 +93,7 @@ def _mount_orc8r_certs(self) -> None:
@property
def _certifier_relation_ready(self) -> bool:
"""Checks whether certifier relation is ready."""
certifier_relation = self.model.get_relation("certifier")
certifier_relation = self.model.get_relation("magma-orc8r-certifier")
if not certifier_relation or len(certifier_relation.units) == 0:
return False
return True
Expand Down
Loading

0 comments on commit ce0f021

Please sign in to comment.