diff --git a/lib/charms/kratos/v0/kubernetes_network_policies.py b/lib/charms/kratos/v0/kubernetes_network_policies.py index ae6fa6e1..ef5f5dde 100644 --- a/lib/charms/kratos/v0/kubernetes_network_policies.py +++ b/lib/charms/kratos/v0/kubernetes_network_policies.py @@ -80,10 +80,12 @@ def to_resource(self): return NetworkPolicyPort(port=self.port, endPort=self.end_port, protocol=self.protocol) +Port = Union[str, int] +IngressPolicyDefinition = Union[Tuple[Port], Tuple[int, str], Tuple[int, str, int]] IngressPolicyDefinition = Tuple[PortDefinition, List[Relation]] -class K8sNetworkPoliciesHandler: +class KubernetesNetworkPoliciesHandler: """A helper class for managing kubernetes network policies.""" def __init__(self, charm: CharmBase) -> None: diff --git a/src/charm.py b/src/charm.py index 52418245..058f0a36 100755 --- a/src/charm.py +++ b/src/charm.py @@ -32,7 +32,7 @@ ) from charms.kratos.v0.kratos_endpoints import KratosEndpointsProvider from charms.kratos.v0.kubernetes_network_policies import ( - K8sNetworkPoliciesHandler, + KubernetesNetworkPoliciesHandler, NetworkPoliciesHandlerError, PortDefinition, ) @@ -54,6 +54,7 @@ HookEvent, PebbleReadyEvent, RelationEvent, + RemoveEvent, ) from ops.main import main from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, ModelError, WaitingStatus @@ -122,7 +123,7 @@ def __init__(self, *args: Any) -> None: port=KRATOS_PUBLIC_PORT, strip_prefix=True, ) - self.network_policy_handler = K8sNetworkPoliciesHandler(self) + self.network_policy_handler = KubernetesNetworkPoliciesHandler(self) self.database = DatabaseRequires( self, @@ -145,6 +146,7 @@ def __init__(self, *args: Any) -> None: self.framework.observe(self.on.kratos_pebble_ready, self._on_pebble_ready) self.framework.observe(self.on.config_changed, self._on_config_changed) + self.framework.observe(self.on.remove, self._cleanup) self.framework.observe( self.endpoints_provider.on.ready, self._update_kratos_endpoints_relation_data ) @@ -452,6 +454,9 @@ def _on_config_changed(self, event: ConfigChangedEvent) -> None: """Event Handler for config changed event.""" self._handle_status_update_config(event) + def _cleanup(self, event: RemoveEvent) -> None: + self.network_policy_handler.delete_network_policy() + def _update_kratos_endpoints_relation_data(self, event: RelationEvent) -> None: admin_endpoint = ( self.admin_ingress.url diff --git a/tests/integration/test_charm.py b/tests/integration/test_charm.py index b621fc3c..762387b7 100644 --- a/tests/integration/test_charm.py +++ b/tests/integration/test_charm.py @@ -98,7 +98,6 @@ async def test_postgres_relation(ops_test: OpsTest) -> None: await ops_test.model.wait_for_idle( apps=[APP_NAME, POSTGRES], status="active", - raise_on_blocked=True, timeout=1000, ) assert ops_test.model.applications[APP_NAME].units[0].workload_status == "active" diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 80b3e868..ae84f755 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -17,7 +17,7 @@ @pytest.fixture(autouse=True) def lk_client(mocker): - return mocker.patch("charm.K8sNetworkPoliciesHandler") + return mocker.patch("charm.KubernetesNetworkPoliciesHandler") @pytest.fixture()