From a1b3af401dfeb20f65199125babab6d0ddff2818 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 18 Oct 2021 14:29:37 +0200 Subject: [PATCH] outposts: improve handling of recreate scenarios Signed-off-by: Jens Langhammer --- authentik/outposts/controllers/k8s/base.py | 25 +++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/authentik/outposts/controllers/k8s/base.py b/authentik/outposts/controllers/k8s/base.py index a298ad325..d9f8a3c98 100644 --- a/authentik/outposts/controllers/k8s/base.py +++ b/authentik/outposts/controllers/k8s/base.py @@ -1,5 +1,5 @@ """Base Kubernetes Reconciler""" -from typing import TYPE_CHECKING, Generic, TypeVar +from typing import TYPE_CHECKING, Generic, Optional, TypeVar from django.utils.text import slugify from kubernetes.client import V1ObjectMeta @@ -78,21 +78,26 @@ class KubernetesObjectReconciler(Generic[T]): # pylint: disable=no-member if isinstance(exc, ApiException) and exc.status == 422: self.logger.debug("Failed to update current, triggering re-create") - raise NeedsRecreate from exc + self._recreate(current=current, reference=reference) + return self.logger.debug("Other unhandled error", exc=exc) raise exc except NeedsRecreate: - self.logger.debug("Recreate requested") - if current: - self.logger.debug("Deleted old") - self.delete(current) - else: - self.logger.debug("No old found, creating") - self.logger.debug("Creating") - self.create(reference) + self._recreate(current=current, reference=reference) else: self.logger.debug("Object is up-to-date.") + def _recreate(self, reference: T, current: Optional[T] = None): + """Recreate object""" + self.logger.debug("Recreate requested") + if current: + self.logger.debug("Deleted old") + self.delete(current) + else: + self.logger.debug("No old found, creating") + self.logger.debug("Creating") + self.create(reference) + def down(self): """Delete object if found""" if self.noop: