nolar / kopf

A Python framework to write Kubernetes operators in just a few lines of code
https://kopf.readthedocs.io/
MIT License
2.08k stars 158 forks source link

How to implement Replicaset and Pod like mechanism using kopf - kopf daemon #705

Open sajuptpm opened 3 years ago

sajuptpm commented 3 years ago

How we can implement this using kopf ? Should we use kopf.daemon with some infinite loop and polling ?

nolar commented 3 years ago

Either a daemon or a timer. Depends on what logic you want to implement.

Indexing can also be helpful for cross-resource data exchange in 1.30.0rc1 (note: a release candidate yet): https://github.com/nolar/kopf/releases/tag/1.30.0rc1

sajuptpm commented 3 years ago

some how we need to pause the execution of daemon/timer during update event, right ? that is were this indexing or memo is useful right ? my question is how to avoid deamon/timer and event handle execution conflict ? (if they are handling similar logic)

Let me explain what i am doing. on.create handler install some applications (helm install) in k8s cluster. on.update handler update the applications (helm upgrade) which already installed. I want to re-execute the application installation script if application is down, there is no create/update event in this case. I am planning to use deamon/timer here, which periodically check the status of installed application (api call to some endpoint) and re-execute installation script to bring the application up if it is down. Here problem is, during this re-execution, update event can come from the user (user doesn't know about daemon) and there will be a re-execution and upgrade conflict.

Thanks for the quick reply

nolar commented 3 years ago

Thanks for clarifying. Indeed, memos are a good tool for this. Perhaps, you want to never run any scripts in the update handlers, but only set a flag there. And the daemon/timer can check for that flag OR the actual state of the app and run the script when either of these are “on”.

Conditions (threading or asyncio) are worth considering as values for memo fields if you need to wake up the daemons faster from the update handlers.