You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Which project are you requesting an enhancement for?
kr8s
What do you need?
Plenty of years ago I had developed a Django backend system providing amazing sync relationsheep between a model and the resource living in the cluster. The killer feature is atomic operations. Normally, when performing multiple operations (i.e create, update, etc), if one fails, you're left with an inconsistent cluster state.
My approach wasn't the best though: a hybrid (hard-coded/computed) mapping of "rollback" delegates did the trick.
I am wondering if such solution is outdated nowadays, the opposite, or does Kubernetes provide such feature out of the box?
If I see interest I will gladly collaborate to integrate a non-ORM variant into Kr8s
Some preview of how it looks like
classK8SAtomicOperationsContext(transaction.Atomic):
""" An Atomic context manager that rolls back operations Has context upon created KubernetesResource's versions When failed, rolls back to the previous version """signals: K8SAtomicOperationSignalsmanager: K8SAtomicOperationsManagerdef__init__(self, *args,**kwargs):
self.created_entities= []
self.manager=K8SAtomicOperationsManager()
self.signals=K8SAtomicOperationSignals(self.manager)
super().__init__(
DEFAULT_DB_ALIAS,
True,
False
)
def__enter__(self):
# Call the superclass method to enter the transactionsuper().__enter__()
# Set up a signal to track when entities are createdself.signals.connect()
def__exit__(self, exc_type, exc_value, traceback):
# Deregister the signalself.signals.disconnect()
ifexc_typeisnotNone:
self.manager.rollback(exc_value)
# Call the superclass method to exit the transactionreturnsuper().__exit__(exc_type, exc_value, traceback)
classK8SAtomicOperationsManager:
rollback_operations: List['ResourceOperation']
def__init__(self) ->None:
self.rollback_operations= []
defon_rollback(self, operation):
""" Rollback the operation """self.rollback_operations.append(operation)
defrollback(self, exception):
""" Rollback all operations """# Filter rollback operations that should not be rolled backops=list(filter(lambdaop: self.should_rollback(op,exception), self.rollback_operations))
log.info("Rolling back %s operations"%len(ops))
foroperationinreversed(ops):
ifnotself.should_rollback(operation, exception):
continueretries_left=10whileretries_left>0:
try:
operation.execute()
breakexceptAPIExceptionase:
ife.status==404:
raiseelog.info('Rollback %s fail: API Error %s:%s. Retrying %s more times'% (
operation,
e.status, e.reason, retries_left
))
time.sleep(2)
retries_left-=1ifretries_left==0:
raiseedefshould_rollback(self,rollback_operation: 'ResourceOperation', exception):
""" Returns True if the manager should rollback an operation Avoids rolling back operations that were never executed """ifisinstance(exception, APIException):
ifexception.operation==rollback_operation.rollsback:
returnFalsereturnTrue
The text was updated successfully, but these errors were encountered:
Which project are you requesting an enhancement for?
kr8s
What do you need?
Plenty of years ago I had developed a Django backend system providing amazing sync relationsheep between a model and the resource living in the cluster. The killer feature is atomic operations. Normally, when performing multiple operations (i.e create, update, etc), if one fails, you're left with an inconsistent cluster state.
My approach wasn't the best though: a hybrid (hard-coded/computed) mapping of "rollback" delegates did the trick.
I am wondering if such solution is outdated nowadays, the opposite, or does Kubernetes provide such feature out of the box?
If I see interest I will gladly collaborate to integrate a non-ORM variant into Kr8s
Some preview of how it looks like
The text was updated successfully, but these errors were encountered: