2020-10-14 15:49:09 +00:00
|
|
|
"""Kubernetes Service Reconciler"""
|
2020-10-18 15:07:11 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2020-10-14 15:49:09 +00:00
|
|
|
|
|
|
|
from kubernetes.client import CoreV1Api, V1Service, V1ServicePort, V1ServiceSpec
|
|
|
|
|
|
|
|
from passbook.outposts.controllers.k8s.base import (
|
|
|
|
KubernetesObjectReconciler,
|
|
|
|
NeedsUpdate,
|
|
|
|
)
|
2020-10-20 07:36:26 +00:00
|
|
|
from passbook.outposts.controllers.k8s.deployment import DeploymentReconciler
|
2020-10-18 15:07:11 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from passbook.outposts.controllers.kubernetes import KubernetesController
|
2020-10-14 15:49:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ServiceReconciler(KubernetesObjectReconciler[V1Service]):
|
|
|
|
"""Kubernetes Service Reconciler"""
|
|
|
|
|
2020-10-18 15:07:11 +00:00
|
|
|
def __init__(self, controller: "KubernetesController") -> None:
|
|
|
|
super().__init__(controller)
|
2020-10-14 15:49:09 +00:00
|
|
|
self.api = CoreV1Api()
|
2020-10-18 15:07:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
2020-10-19 15:39:12 +00:00
|
|
|
return f"passbook-outpost-{self.controller.outpost.uuid.hex}"
|
2020-10-14 15:49:09 +00:00
|
|
|
|
|
|
|
def reconcile(self, current: V1Service, reference: V1Service):
|
|
|
|
if len(current.spec.ports) != len(reference.spec.ports):
|
|
|
|
raise NeedsUpdate()
|
|
|
|
for port in reference.spec.ports:
|
|
|
|
if port not in current.spec.ports:
|
|
|
|
raise NeedsUpdate()
|
|
|
|
|
|
|
|
def get_reference_object(self) -> V1Service:
|
|
|
|
"""Get deployment object for outpost"""
|
2020-10-18 15:07:11 +00:00
|
|
|
meta = self.get_object_meta(name=self.name)
|
2020-10-14 15:49:09 +00:00
|
|
|
ports = []
|
2020-10-18 15:07:11 +00:00
|
|
|
for port_name, port in self.controller.deployment_ports.items():
|
2020-10-14 15:49:09 +00:00
|
|
|
ports.append(V1ServicePort(name=port_name, port=port))
|
2020-10-20 07:36:26 +00:00
|
|
|
selector_labels = DeploymentReconciler(self.controller).get_pod_meta()
|
2020-10-14 15:49:09 +00:00
|
|
|
return V1Service(
|
|
|
|
metadata=meta,
|
2020-10-20 07:36:26 +00:00
|
|
|
spec=V1ServiceSpec(ports=ports, selector=selector_labels, type="ClusterIP"),
|
2020-10-14 15:49:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def create(self, reference: V1Service):
|
|
|
|
return self.api.create_namespaced_service(self.namespace, reference)
|
|
|
|
|
|
|
|
def delete(self, reference: V1Service):
|
|
|
|
return self.api.delete_namespaced_service(
|
|
|
|
reference.metadata.name, self.namespace
|
|
|
|
)
|
|
|
|
|
|
|
|
def retrieve(self) -> V1Service:
|
2020-10-19 15:39:12 +00:00
|
|
|
return self.api.read_namespaced_service(self.name, self.namespace)
|
2020-10-14 15:49:09 +00:00
|
|
|
|
|
|
|
def update(self, current: V1Service, reference: V1Service):
|
|
|
|
return self.api.patch_namespaced_service(
|
|
|
|
current.metadata.name, self.namespace, reference
|
|
|
|
)
|