outposts: improve handling of recreate scenarios

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-10-18 14:29:37 +02:00
parent 92d38f62b5
commit a1b3af401d
1 changed files with 15 additions and 10 deletions

View File

@ -1,5 +1,5 @@
"""Base Kubernetes Reconciler""" """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 django.utils.text import slugify
from kubernetes.client import V1ObjectMeta from kubernetes.client import V1ObjectMeta
@ -78,21 +78,26 @@ class KubernetesObjectReconciler(Generic[T]):
# pylint: disable=no-member # pylint: disable=no-member
if isinstance(exc, ApiException) and exc.status == 422: if isinstance(exc, ApiException) and exc.status == 422:
self.logger.debug("Failed to update current, triggering re-create") 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) self.logger.debug("Other unhandled error", exc=exc)
raise exc raise exc
except NeedsRecreate: except NeedsRecreate:
self.logger.debug("Recreate requested") self._recreate(current=current, reference=reference)
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)
else: else:
self.logger.debug("Object is up-to-date.") 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): def down(self):
"""Delete object if found""" """Delete object if found"""
if self.noop: if self.noop: