From fa504e4bf90554521e8519168fd00a509aa6fdba Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 16 Oct 2020 12:54:52 +0200 Subject: [PATCH] outposts: pass outpost reference instead of PK, implement TaskResult.uid --- passbook/outposts/controllers/base.py | 4 ++-- passbook/outposts/controllers/docker.py | 5 ++-- passbook/outposts/controllers/kubernetes.py | 5 ++-- passbook/outposts/tasks.py | 23 ++++++++++--------- passbook/outposts/views.py | 2 +- .../providers/proxy/controllers/docker.py | 5 ++-- .../providers/proxy/controllers/kubernetes.py | 5 ++-- 7 files changed, 27 insertions(+), 22 deletions(-) diff --git a/passbook/outposts/controllers/base.py b/passbook/outposts/controllers/base.py index 226937b88..cc315ddb0 100644 --- a/passbook/outposts/controllers/base.py +++ b/passbook/outposts/controllers/base.py @@ -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 ) diff --git a/passbook/outposts/controllers/docker.py b/passbook/outposts/controllers/docker.py index b1b281f6d..737ae735e 100644 --- a/passbook/outposts/controllers/docker.py +++ b/passbook/outposts/controllers/docker.py @@ -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]: diff --git a/passbook/outposts/controllers/kubernetes.py b/passbook/outposts/controllers/kubernetes.py index 3cac744f8..e3a0413e6 100644 --- a/passbook/outposts/controllers/kubernetes.py +++ b/passbook/outposts/controllers/kubernetes.py @@ -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: diff --git a/passbook/outposts/tasks.py b/passbook/outposts/tasks.py index 5aafa2e10..7b81f8e52 100644 --- a/passbook/outposts/tasks.py +++ b/passbook/outposts/tasks.py @@ -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() diff --git a/passbook/outposts/views.py b/passbook/outposts/views.py index d5ddf58f7..990b96608 100644 --- a/passbook/outposts/views.py +++ b/passbook/outposts/views.py @@ -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") diff --git a/passbook/providers/proxy/controllers/docker.py b/passbook/providers/proxy/controllers/docker.py index 252319a6f..519ac01c8 100644 --- a/passbook/providers/proxy/controllers/docker.py +++ b/passbook/providers/proxy/controllers/docker.py @@ -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, diff --git a/passbook/providers/proxy/controllers/kubernetes.py b/passbook/providers/proxy/controllers/kubernetes.py index 294540655..e18a12df7 100644 --- a/passbook/providers/proxy/controllers/kubernetes.py +++ b/passbook/providers/proxy/controllers/kubernetes.py @@ -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,