outposts: pass outpost reference instead of PK, implement TaskResult.uid

This commit is contained in:
Jens Langhammer 2020-10-16 12:54:52 +02:00
parent 86cfb10b9b
commit fa504e4bf9
7 changed files with 27 additions and 22 deletions

View File

@ -19,8 +19,8 @@ class BaseController:
outpost: Outpost
def __init__(self, outpost_pk: str):
self.outpost = Outpost.objects.get(pk=outpost_pk)
def __init__(self, outpost: Outpost):
self.outpost = outpost
self.logger = get_logger(
controller=self.__class__.__name__, outpost=self.outpost
)

View File

@ -8,6 +8,7 @@ from yaml import safe_dump
from passbook import __version__
from passbook.outposts.controllers.base import BaseController, ControllerException
from passbook.outposts.models import Outpost
class DockerController(BaseController):
@ -19,8 +20,8 @@ class DockerController(BaseController):
image_base = "beryju/passbook"
def __init__(self, outpost_pk: str) -> None:
super().__init__(outpost_pk)
def __init__(self, outpost: Outpost) -> None:
super().__init__(outpost)
self.client = from_env()
def _get_env(self) -> Dict[str, str]:

View File

@ -10,13 +10,14 @@ from passbook.outposts.controllers.base import BaseController, ControllerExcepti
from passbook.outposts.controllers.k8s.deployment import DeploymentReconciler
from passbook.outposts.controllers.k8s.secret import SecretReconciler
from passbook.outposts.controllers.k8s.service import ServiceReconciler
from passbook.outposts.models import Outpost
class KubernetesController(BaseController):
"""Manage deployment of outpost in kubernetes"""
def __init__(self, outpost_pk: str) -> None:
super().__init__(outpost_pk)
def __init__(self, outpost: Outpost) -> None:
super().__init__(outpost)
try:
load_incluster_config()
except ConfigException:

View File

@ -29,25 +29,26 @@ def outpost_controller_all():
for outpost in Outpost.objects.exclude(
deployment_type=OutpostDeploymentType.CUSTOM
):
outpost_controller.delay(outpost.pk.hex, outpost.deployment_type, outpost.type)
outpost_controller.delay(outpost.pk.hex)
@CELERY_APP.task(bind=True, base=MonitoredTask)
def outpost_controller(
self: MonitoredTask, outpost_pk: str, deployment_type: str, outpost_type: str
):
def outpost_controller(self: MonitoredTask, outpost_pk: str):
"""Launch controller and reconcile deployment/service/etc"""
logs = []
outpost: Outpost = Outpost.objects.get(pk=outpost_pk)
try:
if outpost_type == OutpostType.PROXY:
if deployment_type == OutpostDeploymentType.KUBERNETES:
logs = ProxyKubernetesController(outpost_pk).run_with_logs()
if deployment_type == OutpostDeploymentType.DOCKER:
logs = ProxyDockerController(outpost_pk).run_with_logs()
if outpost.type == OutpostType.PROXY:
if outpost.deployment_type == OutpostDeploymentType.KUBERNETES:
logs = ProxyKubernetesController(outpost).run_with_logs()
if outpost.deployment_type == OutpostDeploymentType.DOCKER:
logs = ProxyDockerController(outpost).run_with_logs()
except ControllerException as exc:
self.set_status(TaskResult(TaskResultStatus.ERROR, [str(exc)], exc))
self.set_status(
TaskResult(TaskResultStatus.ERROR, [str(exc)], exc, uid=outpost.name)
)
else:
self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, logs))
self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, logs, uid=outpost.name))
@CELERY_APP.task()

View File

@ -35,7 +35,7 @@ class DockerComposeView(LoginRequiredMixin, View):
)
manifest = ""
if outpost.type == OutpostType.PROXY:
controller = DockerController(outpost_pk)
controller = DockerController(outpost)
manifest = controller.get_static_deployment()
return HttpResponse(manifest, content_type="text/vnd.yaml")

View File

@ -1,12 +1,13 @@
"""Proxy Provider Docker Contoller"""
from passbook.outposts.controllers.docker import DockerController
from passbook.outposts.models import Outpost
class ProxyDockerController(DockerController):
"""Proxy Provider Docker Contoller"""
def __init__(self, outpost_pk: str):
super().__init__(outpost_pk)
def __init__(self, outpost: Outpost):
super().__init__(outpost)
self.deployment_ports = {
"http": 4180,
"https": 4443,

View File

@ -1,12 +1,13 @@
"""Proxy Provider Kubernetes Contoller"""
from passbook.outposts.controllers.kubernetes import KubernetesController
from passbook.outposts.models import Outpost
class ProxyKubernetesController(KubernetesController):
"""Proxy Provider Kubernetes Contoller"""
def __init__(self, outpost_pk: str):
super().__init__(outpost_pk)
def __init__(self, outpost: Outpost):
super().__init__(outpost)
self.deployment_ports = {
"http": 4180,
"https": 4443,