authentik.outposts.controllers.k8s.utils

k8s utils

 1"""k8s utils"""
 2
 3from kubernetes.client.models.v1_container_port import V1ContainerPort
 4from kubernetes.client.models.v1_service_port import V1ServicePort
 5
 6from authentik.outposts.controllers.k8s.triggers import NeedsRecreate
 7
 8
 9def compare_port(
10    current: V1ServicePort | V1ContainerPort, reference: V1ServicePort | V1ContainerPort
11) -> bool:
12    """Compare a single port"""
13    if current.name != reference.name:
14        return False
15    if current.protocol != reference.protocol:
16        return False
17    if isinstance(current, V1ServicePort) and isinstance(reference, V1ServicePort):
18        # We only care about the target port
19        if current.target_port != reference.target_port:
20            return False
21    if isinstance(current, V1ContainerPort) and isinstance(reference, V1ContainerPort):
22        # We only care about the target port
23        if current.container_port != reference.container_port:
24            return False
25    return True
26
27
28def compare_ports(
29    current: list[V1ServicePort | V1ContainerPort] | None,
30    reference: list[V1ServicePort | V1ContainerPort] | None,
31):
32    """Compare ports of a list"""
33    if not current or not reference:
34        raise NeedsRecreate()
35    if len(current) != len(reference):
36        raise NeedsRecreate()
37    for port in reference:
38        if not any(compare_port(port, current_port) for current_port in current):
39            raise NeedsRecreate()
def compare_port( current: kubernetes.client.models.v1_service_port.V1ServicePort | kubernetes.client.models.v1_container_port.V1ContainerPort, reference: kubernetes.client.models.v1_service_port.V1ServicePort | kubernetes.client.models.v1_container_port.V1ContainerPort) -> bool:
10def compare_port(
11    current: V1ServicePort | V1ContainerPort, reference: V1ServicePort | V1ContainerPort
12) -> bool:
13    """Compare a single port"""
14    if current.name != reference.name:
15        return False
16    if current.protocol != reference.protocol:
17        return False
18    if isinstance(current, V1ServicePort) and isinstance(reference, V1ServicePort):
19        # We only care about the target port
20        if current.target_port != reference.target_port:
21            return False
22    if isinstance(current, V1ContainerPort) and isinstance(reference, V1ContainerPort):
23        # We only care about the target port
24        if current.container_port != reference.container_port:
25            return False
26    return True

Compare a single port

def compare_ports( current: list[kubernetes.client.models.v1_service_port.V1ServicePort | kubernetes.client.models.v1_container_port.V1ContainerPort] | None, reference: list[kubernetes.client.models.v1_service_port.V1ServicePort | kubernetes.client.models.v1_container_port.V1ContainerPort] | None):
29def compare_ports(
30    current: list[V1ServicePort | V1ContainerPort] | None,
31    reference: list[V1ServicePort | V1ContainerPort] | None,
32):
33    """Compare ports of a list"""
34    if not current or not reference:
35        raise NeedsRecreate()
36    if len(current) != len(reference):
37        raise NeedsRecreate()
38    for port in reference:
39        if not any(compare_port(port, current_port) for current_port in current):
40            raise NeedsRecreate()

Compare ports of a list