Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nsklikas committed Apr 21, 2023
1 parent c2e5475 commit 994419e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/charms/kratos/v0/kubernetes_network_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
)
from charms.kratos.v0.kratos_endpoints import KratosEndpointsProvider
from charms.kratos.v0.kubernetes_network_policies import (
K8sNetworkPoliciesHandler,
KubernetesNetworkPoliciesHandler,
NetworkPoliciesHandlerError,
PortDefinition,
)
Expand All @@ -54,6 +54,7 @@
HookEvent,
PebbleReadyEvent,
RelationEvent,
RemoveEvent,
)
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, ModelError, WaitingStatus
Expand Down Expand Up @@ -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,
Expand All @@ -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
)
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 994419e

Please sign in to comment.