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-10-14 15:49:09 +00:00
|
|
|
from kubernetes.config import load_incluster_config, load_kube_config
|
|
|
|
from kubernetes.config.config_exception import ConfigException
|
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-10-16 10:54:52 +00:00
|
|
|
from passbook.outposts.models import 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-10-16 10:54:52 +00:00
|
|
|
def __init__(self, outpost: Outpost) -> None:
|
|
|
|
super().__init__(outpost)
|
2020-10-14 15:49:09 +00:00
|
|
|
try:
|
|
|
|
load_incluster_config()
|
|
|
|
except ConfigException:
|
|
|
|
load_kube_config()
|
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
|
|
|
|
|
|
|
|
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()
|