2020-10-14 15:49:09 +00:00
|
|
|
"""Kubernetes Deployment 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 (
|
|
|
|
AppsV1Api,
|
|
|
|
V1Container,
|
|
|
|
V1ContainerPort,
|
|
|
|
V1Deployment,
|
|
|
|
V1DeploymentSpec,
|
|
|
|
V1EnvVar,
|
|
|
|
V1EnvVarSource,
|
|
|
|
V1LabelSelector,
|
|
|
|
V1ObjectMeta,
|
|
|
|
V1PodSpec,
|
|
|
|
V1PodTemplateSpec,
|
|
|
|
V1SecretKeySelector,
|
|
|
|
)
|
|
|
|
|
|
|
|
from passbook import __version__
|
|
|
|
from passbook.outposts.controllers.k8s.base import (
|
|
|
|
KubernetesObjectReconciler,
|
|
|
|
NeedsUpdate,
|
|
|
|
)
|
|
|
|
from passbook.outposts.models import Outpost
|
|
|
|
|
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 DeploymentReconciler(KubernetesObjectReconciler[V1Deployment]):
|
|
|
|
"""Kubernetes Deployment Reconciler"""
|
|
|
|
|
|
|
|
image_base = "beryju/passbook"
|
|
|
|
|
2020-10-18 15:07:11 +00:00
|
|
|
outpost: Outpost
|
2020-10-14 15:49:09 +00:00
|
|
|
|
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 = AppsV1Api()
|
2020-10-18 15:07:11 +00:00
|
|
|
self.outpost = self.controller.outpost
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
return f"passbook-outpost-{self.outpost.name}"
|
2020-10-14 15:49:09 +00:00
|
|
|
|
|
|
|
def reconcile(self, current: V1Deployment, reference: V1Deployment):
|
|
|
|
if current.spec.replicas != reference.spec.replicas:
|
|
|
|
raise NeedsUpdate()
|
|
|
|
if (
|
|
|
|
current.spec.template.spec.containers[0].image
|
|
|
|
!= reference.spec.template.spec.containers[0].image
|
|
|
|
):
|
|
|
|
raise NeedsUpdate()
|
|
|
|
|
|
|
|
def get_reference_object(self) -> V1Deployment:
|
|
|
|
"""Get deployment object for outpost"""
|
|
|
|
# Generate V1ContainerPort objects
|
|
|
|
container_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
|
|
|
container_ports.append(V1ContainerPort(container_port=port, name=port_name))
|
2020-10-18 15:07:11 +00:00
|
|
|
meta = self.get_object_meta(name=self.name)
|
2020-10-14 15:49:09 +00:00
|
|
|
return V1Deployment(
|
|
|
|
metadata=meta,
|
|
|
|
spec=V1DeploymentSpec(
|
|
|
|
replicas=self.outpost.config.kubernetes_replicas,
|
|
|
|
selector=V1LabelSelector(match_labels=meta.labels),
|
|
|
|
template=V1PodTemplateSpec(
|
|
|
|
metadata=V1ObjectMeta(labels=meta.labels),
|
|
|
|
spec=V1PodSpec(
|
|
|
|
containers=[
|
|
|
|
V1Container(
|
|
|
|
name=self.outpost.type,
|
|
|
|
image=f"{self.image_base}-{self.outpost.type}:{__version__}",
|
|
|
|
ports=container_ports,
|
|
|
|
env=[
|
|
|
|
V1EnvVar(
|
|
|
|
name="PASSBOOK_HOST",
|
|
|
|
value_from=V1EnvVarSource(
|
|
|
|
secret_key_ref=V1SecretKeySelector(
|
|
|
|
name=f"passbook-outpost-{self.outpost.name}-api",
|
|
|
|
key="passbook_host",
|
|
|
|
)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
V1EnvVar(
|
|
|
|
name="PASSBOOK_TOKEN",
|
|
|
|
value_from=V1EnvVarSource(
|
|
|
|
secret_key_ref=V1SecretKeySelector(
|
|
|
|
name=f"passbook-outpost-{self.outpost.name}-api",
|
|
|
|
key="token",
|
|
|
|
)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
V1EnvVar(
|
|
|
|
name="PASSBOOK_INSECURE",
|
|
|
|
value_from=V1EnvVarSource(
|
|
|
|
secret_key_ref=V1SecretKeySelector(
|
|
|
|
name=f"passbook-outpost-{self.outpost.name}-api",
|
|
|
|
key="passbook_host_insecure",
|
|
|
|
)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def create(self, reference: V1Deployment):
|
|
|
|
return self.api.create_namespaced_deployment(self.namespace, reference)
|
|
|
|
|
|
|
|
def delete(self, reference: V1Deployment):
|
|
|
|
return self.api.delete_namespaced_deployment(
|
|
|
|
reference.metadata.name, self.namespace
|
|
|
|
)
|
|
|
|
|
|
|
|
def retrieve(self) -> V1Deployment:
|
|
|
|
return self.api.read_namespaced_deployment(
|
|
|
|
f"passbook-outpost-{self.outpost.name}", self.namespace
|
|
|
|
)
|
|
|
|
|
|
|
|
def update(self, current: V1Deployment, reference: V1Deployment):
|
|
|
|
return self.api.patch_namespaced_deployment(
|
|
|
|
current.metadata.name, self.namespace, reference
|
|
|
|
)
|