2020-09-02 22:04:12 +00:00
|
|
|
"""Kubernetes deployment controller"""
|
|
|
|
from io import StringIO
|
2020-10-18 15:07:11 +00:00
|
|
|
from typing import Dict, List, Type
|
2020-09-02 22:04:12 +00:00
|
|
|
|
2020-10-16 09:31:31 +00:00
|
|
|
from kubernetes.client import OpenApiException
|
2020-11-08 20:02:52 +00:00
|
|
|
from kubernetes.client.api_client import ApiClient
|
2020-10-19 19:29:58 +00:00
|
|
|
from structlog.testing import capture_logs
|
2020-09-02 22:04:12 +00:00
|
|
|
from yaml import dump_all
|
|
|
|
|
2020-10-16 09:31:31 +00:00
|
|
|
from passbook.outposts.controllers.base import BaseController, ControllerException
|
2020-10-18 15:07:11 +00:00
|
|
|
from passbook.outposts.controllers.k8s.base import KubernetesObjectReconciler
|
2020-10-14 15:49:09 +00:00
|
|
|
from passbook.outposts.controllers.k8s.deployment import DeploymentReconciler
|
|
|
|
from passbook.outposts.controllers.k8s.secret import SecretReconciler
|
|
|
|
from passbook.outposts.controllers.k8s.service import ServiceReconciler
|
2020-11-04 09:41:18 +00:00
|
|
|
from passbook.outposts.models import KubernetesServiceConnection, Outpost
|
2020-09-13 20:19:26 +00:00
|
|
|
|
|
|
|
|
2020-09-02 22:04:12 +00:00
|
|
|
class KubernetesController(BaseController):
|
|
|
|
"""Manage deployment of outpost in kubernetes"""
|
|
|
|
|
2020-10-18 15:07:11 +00:00
|
|
|
reconcilers: Dict[str, Type[KubernetesObjectReconciler]]
|
|
|
|
reconcile_order: List[str]
|
|
|
|
|
2020-11-08 20:02:52 +00:00
|
|
|
config: ApiClient
|
2020-11-04 09:41:18 +00:00
|
|
|
connection: KubernetesServiceConnection
|
|
|
|
|
2020-11-04 09:54:44 +00:00
|
|
|
def __init__(
|
|
|
|
self, outpost: Outpost, connection: KubernetesServiceConnection
|
|
|
|
) -> None:
|
|
|
|
super().__init__(outpost, connection)
|
2020-11-08 20:02:52 +00:00
|
|
|
self.client = connection.client()
|
2020-10-18 15:07:11 +00:00
|
|
|
self.reconcilers = {
|
|
|
|
"secret": SecretReconciler,
|
|
|
|
"deployment": DeploymentReconciler,
|
|
|
|
"service": ServiceReconciler,
|
|
|
|
}
|
|
|
|
self.reconcile_order = ["secret", "deployment", "service"]
|
2020-09-02 22:04:12 +00:00
|
|
|
|
2020-10-16 20:22:15 +00:00
|
|
|
def up(self):
|
2020-10-16 09:31:31 +00:00
|
|
|
try:
|
2020-10-18 15:07:11 +00:00
|
|
|
for reconcile_key in self.reconcile_order:
|
|
|
|
reconciler = self.reconcilers[reconcile_key](self)
|
|
|
|
reconciler.up()
|
2020-10-14 15:49:09 +00:00
|
|
|
|
2020-10-16 20:22:15 +00:00
|
|
|
except OpenApiException as exc:
|
|
|
|
raise ControllerException from exc
|
|
|
|
|
2020-10-19 19:29:58 +00:00
|
|
|
def up_with_logs(self) -> List[str]:
|
|
|
|
try:
|
|
|
|
all_logs = []
|
|
|
|
for reconcile_key in self.reconcile_order:
|
|
|
|
with capture_logs() as logs:
|
|
|
|
reconciler = self.reconcilers[reconcile_key](self)
|
|
|
|
reconciler.up()
|
|
|
|
all_logs += [f"{reconcile_key.title()}: {x['event']}" for x in logs]
|
|
|
|
return all_logs
|
|
|
|
except OpenApiException as exc:
|
|
|
|
raise ControllerException from exc
|
|
|
|
|
2020-10-16 20:22:15 +00:00
|
|
|
def down(self):
|
|
|
|
try:
|
2020-10-18 15:07:11 +00:00
|
|
|
for reconcile_key in self.reconcile_order:
|
|
|
|
reconciler = self.reconcilers[reconcile_key](self)
|
|
|
|
reconciler.down()
|
2020-10-16 20:22:15 +00:00
|
|
|
|
2020-10-16 09:31:31 +00:00
|
|
|
except OpenApiException as exc:
|
|
|
|
raise ControllerException from exc
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
def get_static_deployment(self) -> str:
|
2020-10-18 15:07:11 +00:00
|
|
|
documents = []
|
|
|
|
for reconcile_key in self.reconcile_order:
|
|
|
|
reconciler = self.reconcilers[reconcile_key](self)
|
|
|
|
documents.append(reconciler.get_reference_object().to_dict())
|
2020-10-14 15:49:09 +00:00
|
|
|
|
2020-09-02 22:04:12 +00:00
|
|
|
with StringIO() as _str:
|
|
|
|
dump_all(
|
2020-10-18 15:07:11 +00:00
|
|
|
documents,
|
2020-09-02 22:04:12 +00:00
|
|
|
stream=_str,
|
|
|
|
default_flow_style=False,
|
|
|
|
)
|
|
|
|
return _str.getvalue()
|