2020-09-02 22:04:12 +00:00
|
|
|
"""Kubernetes deployment controller"""
|
|
|
|
from io import StringIO
|
|
|
|
|
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
|
|
|
|
|
|
|
|
from passbook.outposts.controllers.base import BaseController
|
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-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-14 15:49:09 +00:00
|
|
|
def __init__(self, outpost_pk: str) -> None:
|
|
|
|
super().__init__(outpost_pk)
|
|
|
|
try:
|
|
|
|
load_incluster_config()
|
|
|
|
except ConfigException:
|
|
|
|
load_kube_config()
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Called by scheduled task to reconcile deployment/service/etc"""
|
2020-10-14 15:49:09 +00:00
|
|
|
namespace = self.outpost.config.kubernetes_namespace
|
|
|
|
|
|
|
|
secret_reconciler = SecretReconciler(self.outpost)
|
|
|
|
secret_reconciler.namespace = namespace
|
|
|
|
secret_reconciler.run()
|
|
|
|
|
|
|
|
deployment_reconciler = DeploymentReconciler(self.outpost)
|
|
|
|
deployment_reconciler.namespace = namespace
|
|
|
|
deployment_reconciler.deployment_ports = self.deployment_ports
|
|
|
|
deployment_reconciler.run()
|
|
|
|
|
|
|
|
service_reconciler = ServiceReconciler(self.outpost)
|
|
|
|
service_reconciler.namespace = namespace
|
|
|
|
service_reconciler.deployment_ports = self.deployment_ports
|
|
|
|
service_reconciler.run()
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
def get_static_deployment(self) -> str:
|
2020-10-14 15:49:09 +00:00
|
|
|
secret_reconciler = SecretReconciler(self.outpost)
|
|
|
|
secret_reconciler.namespace = ""
|
|
|
|
|
|
|
|
deployment_reconciler = DeploymentReconciler(self.outpost)
|
|
|
|
deployment_reconciler.namespace = ""
|
|
|
|
deployment_reconciler.deployment_ports = self.deployment_ports
|
|
|
|
|
|
|
|
service_reconciler = ServiceReconciler(self.outpost)
|
|
|
|
service_reconciler.namespace = ""
|
|
|
|
service_reconciler.deployment_ports = self.deployment_ports
|
2020-09-02 22:04:12 +00:00
|
|
|
with StringIO() as _str:
|
|
|
|
dump_all(
|
|
|
|
[
|
2020-10-14 15:49:09 +00:00
|
|
|
secret_reconciler.get_reference_object().to_dict(),
|
|
|
|
deployment_reconciler.get_reference_object().to_dict(),
|
|
|
|
service_reconciler.get_reference_object().to_dict(),
|
2020-09-02 22:04:12 +00:00
|
|
|
],
|
|
|
|
stream=_str,
|
|
|
|
default_flow_style=False,
|
|
|
|
)
|
|
|
|
return _str.getvalue()
|