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"""
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: