Handle ending of a pod #694
-
Hello, I'm starting with your library. The flow should give this :
I'm searching a way like async/await on some event to handle modification of ressource status but I didn't find it easly. Could you help me please 🙏 P.S.: Very good job I really like your library |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello. Thank you. Kopf does not support linear scenarios — mostly because Kubernetes itself is highly non-linear and event-driven. For linear workflows, I would recommend looking at specialised workflow engines like Argo, maybe something else. For this described task, you have to react to pods' changes and patch the relevant parent resource. For patching, you will have to know its name (and namespace — for cross-namespace patching). I would recommend putting that name into the labels when you create pods — basically, the same as built-in Jobs do with their Pods. Optionally, if you need to aggregate the status of several children (pods) to make a decision on the next step, you also need another handler on the parent resource itself, where it takes the info from all(!) children at once, analyses it, and does something with that. A single pod's event cannot be aware of the whole parent's state to make such decisions ("yet"; #661 might change that) — so you have to move the info forth & back between resources. import kopf
@kopf.on.create('kopfexample')
def created(namespace, name, **_):
pod = {'kind': 'Pod', ...}
kopf.label(pod, {'my-parent': name})
kopf.adopt(pod)
really_create_pod(namespace, pod)
@kopf.on.update('pods', labels={'my-parent': kopf.PRESENT}, field='status.phase', new='Succeeded')
def pod_changed(namespace, name, labels, **_):
parent_name = labels['my-parent']
really_patch_kopfexample(namespace, parent_name, {'status': {'my-children': {name: {'done': True}}}})
@kopf.on.update('kopfexample', field='status.my-children')
def newinfo(old, new, diff, **_):
print(diff)
print(new) # {'pod1': {'done': True}, 'pod2': {'done': True}, ...}
... # do something This is an idea to start with. I didn't test if this code works, but the concept itself works — we use a similar approach in our operators. This solution depends on Also note: with this approach, you can create not only Pods directly, but also indirectly — e.g. via Jobs. Just ensure that the Pod gets its label in I hope this helps. |
Beta Was this translation helpful? Give feedback.
Hello. Thank you.
Kopf does not support linear scenarios — mostly because Kubernetes itself is highly non-linear and event-driven. For linear workflows, I would recommend looking at specialised workflow engines like Argo, maybe something else.
For this described task, you have to react to pods' changes and patch the relevant parent resource. For patching, you will have to know its name (and namespace — for cross-namespace patching). I would recommend putting that name into the labels when you create pods — basically, the same as built-in Jobs do with their Pods.
Optionally, if you need to aggregate the status of several children (pods) to make a decision on the next step, you also need an…