Skip to content

Commit

Permalink
Simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nsklikas committed Apr 21, 2023
1 parent d3f5a2f commit f5b33f7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
21 changes: 2 additions & 19 deletions lib/charms/kratos/v0/kubernetes_network_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,8 @@ class NetworkPoliciesHandlerError(Exception):
"""Applying the network policies failed."""


@dataclass
class PortDefinition:
"""Network Policy port definition."""

port: Union[str, int]
end_port: Optional[int] = None
protocol: Optional[str] = "TCP"

def to_resource(self):
"""Convert class to NetworkPolicyPort."""
if not self.end_port:
return NetworkPolicyPort(port=self.port, protocol=self.protocol)
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]]
IngressPolicyDefinition = Tuple[Port, List[Relation]]


class KubernetesNetworkPoliciesHandler:
Expand Down Expand Up @@ -130,7 +114,7 @@ def apply_ingress_policy(
ingress.append(
NetworkPolicyIngressRule(
from_=selectors,
ports=[port.to_resource()],
ports=[NetworkPolicyPort(port=port, protocol="TCP")],
),
)

Expand All @@ -144,7 +128,6 @@ def apply_ingress_policy(
),
policyTypes=["Ingress", "Egress"],
ingress=ingress,
egress=[NetworkPolicyEgressRule()],
),
)

Expand Down
11 changes: 4 additions & 7 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from charms.kratos.v0.kubernetes_network_policies import (
KubernetesNetworkPoliciesHandler,
NetworkPoliciesHandlerError,
PortDefinition,
)
from charms.kratos_external_idp_integrator.v0.kratos_external_provider import (
ClientConfigChangedEvent,
Expand Down Expand Up @@ -544,12 +543,10 @@ def _apply_network_policies(self, event: HookEvent) -> None:
try:
self.network_policy_handler.apply_ingress_policy(
[
# (PortDefinition(1, KRATOS_PUBLIC_PORT - 1), []),
(PortDefinition(KRATOS_PUBLIC_PORT), [self.public_ingress.relation]),
(PortDefinition(KRATOS_ADMIN_PORT), [self.admin_ingress.relation]),
(PortDefinition(38812), []),
(PortDefinition(38813), []),
# (PortDefinition(KRATOS_ADMIN_PORT + 1, 65535), []),
(KRATOS_PUBLIC_PORT, [self.public_ingress.relation]),
(KRATOS_ADMIN_PORT, [self.admin_ingress.relation]),
(38812, []),
(38813, []),
]
)
except NetworkPoliciesHandlerError:
Expand Down

0 comments on commit f5b33f7

Please sign in to comment.