outposts: improve controller error handling

This commit is contained in:
Jens Langhammer 2020-10-16 11:31:31 +02:00
parent 91ce7f7363
commit f6b8171624
3 changed files with 61 additions and 49 deletions

View File

@ -3,9 +3,14 @@ from typing import Dict
from structlog import get_logger from structlog import get_logger
from passbook.lib.sentry import SentryIgnoredException
from passbook.outposts.models import Outpost from passbook.outposts.models import Outpost
class ControllerException(SentryIgnoredException):
"""Exception raise when anything fails during controller run"""
class BaseController: class BaseController:
"""Base Outpost deployment controller""" """Base Outpost deployment controller"""

View File

@ -2,12 +2,12 @@
from typing import Dict, Tuple from typing import Dict, Tuple
from docker import DockerClient, from_env from docker import DockerClient, from_env
from docker.errors import NotFound from docker.errors import DockerException, NotFound
from docker.models.containers import Container from docker.models.containers import Container
from yaml import safe_dump from yaml import safe_dump
from passbook import __version__ from passbook import __version__
from passbook.outposts.controllers.base import BaseController from passbook.outposts.controllers.base import BaseController, ControllerException
class DockerController(BaseController): class DockerController(BaseController):
@ -62,6 +62,7 @@ class DockerController(BaseController):
) )
def run(self): def run(self):
try:
container, has_been_created = self._get_container() container, has_been_created = self._get_container()
if has_been_created: if has_been_created:
return None return None
@ -99,6 +100,8 @@ class DockerController(BaseController):
self.logger.info("Container is not running, restarting...") self.logger.info("Container is not running, restarting...")
container.start() container.start()
return None return None
except DockerException as exc:
raise ControllerException from exc
def get_static_deployment(self) -> str: def get_static_deployment(self) -> str:
"""Generate docker-compose yaml for proxy, version 3.5""" """Generate docker-compose yaml for proxy, version 3.5"""

View File

@ -1,11 +1,12 @@
"""Kubernetes deployment controller""" """Kubernetes deployment controller"""
from io import StringIO from io import StringIO
from kubernetes.client import OpenApiException
from kubernetes.config import load_incluster_config, load_kube_config from kubernetes.config import load_incluster_config, load_kube_config
from kubernetes.config.config_exception import ConfigException from kubernetes.config.config_exception import ConfigException
from yaml import dump_all from yaml import dump_all
from passbook.outposts.controllers.base import BaseController from passbook.outposts.controllers.base import BaseController, ControllerException
from passbook.outposts.controllers.k8s.deployment import DeploymentReconciler from passbook.outposts.controllers.k8s.deployment import DeploymentReconciler
from passbook.outposts.controllers.k8s.secret import SecretReconciler from passbook.outposts.controllers.k8s.secret import SecretReconciler
from passbook.outposts.controllers.k8s.service import ServiceReconciler from passbook.outposts.controllers.k8s.service import ServiceReconciler
@ -23,6 +24,7 @@ class KubernetesController(BaseController):
def run(self): def run(self):
"""Called by scheduled task to reconcile deployment/service/etc""" """Called by scheduled task to reconcile deployment/service/etc"""
try:
namespace = self.outpost.config.kubernetes_namespace namespace = self.outpost.config.kubernetes_namespace
secret_reconciler = SecretReconciler(self.outpost) secret_reconciler = SecretReconciler(self.outpost)
@ -38,6 +40,8 @@ class KubernetesController(BaseController):
service_reconciler.namespace = namespace service_reconciler.namespace = namespace
service_reconciler.deployment_ports = self.deployment_ports service_reconciler.deployment_ports = self.deployment_ports
service_reconciler.run() service_reconciler.run()
except OpenApiException as exc:
raise ControllerException from exc
def get_static_deployment(self) -> str: def get_static_deployment(self) -> str:
secret_reconciler = SecretReconciler(self.outpost) secret_reconciler = SecretReconciler(self.outpost)