-
-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: What is the best approach to monitor children objects within their parent? #317
Comments
Thanks @nolar for the detailed sample which I have successfully adapted and used. I'm wondering now if there is a way for this approach to be extended to situations where I don't know ahead of time of what kind the child resource will be? This would require for a generic |
@ableuler That answer was written in March 2020. A lot of new features have appeared since then. I guess, if I would implement parent-children relations again, I would use in-memory indexing for that — instead of the As for "all kinds of resources" — that feature was also added: @kopf.on.event(kopf.EVERYTHING)
def fn(**_): ...
@kopf.on.event('example.com', kopf.EVERYTHING) # all resources in a group
def fn(**_): ...
@kopf.on.event(category='all') # the same as "kubectl get all" (except secrets, something else)
def fn(**_): ... That also works for on-creation/update/deletion/indexing handlers, timers and daemons. Though, it might be not the best idea to do this on a live cluster without filters (e.g. by labels/annotations/when-callback), but it will work. |
Thank you very much for the pointer to |
I have another question related to a parent-child relation as described above: I use a
What am I missing? ps: in case you're interested what people are building based on your work, this is the project that I am using kopf for: https://github.com/SwissDataScienceCenter/amalthea |
I managed to answer my own question. In my example I was only reacting to creation or modification events. However, the events that I get during the initial listing on operator restart come without and event type. Handling events without a type properly solved my problem. |
Yes, exactly. The event type is |
Hi all,
I'm currently working on porting some code from metacontroller into Kopf.
Metacontroller, gives you the option to receive callbacks when the monitored object changes, or any of its children is updated.
For example, if I'm watching an object of kind
multijob
, which creates an arbitrary number of standard kubernetesjob
s, I would receive a callback if the children fail, restart, succeed, etc, which I can use to update thestatus
field in the parent.I haven't been able to find a clean way to do the same thing in Kopf, other than adding separate listeners for both the parent/children, and within the children listeners update the parent CRD. Of course the example here is simplified, the actual application would have many dependencies and larger hierarchies of objects, and having this kind of inter-dependencies between listeners make them harder to maintain.
Is there any better way to do this? Or is there any feature in Kopf that would make the management of children easier/cleaner?
Thanks!
Related: #58 #264
See also: #264 (comment)
You are right, the only way is —as you said— "adding separate listeners for both the parent/children, and within the children listeners update the parent CRD".
Keep in mind, that Kopf keeps one and only one watch-query (an API request) per resource kind no matter how many handlers are there for that resource kind. So, there should be no problems with APIs.
There is no simpler (i.e. few-liner) solution at the moment.
A better solution is planned though — but rather later than sooner (because: priorities; and my regular employment takes time).
Under the hood, it will be working exactly the same way, just with better DSL for handlers. Some ideation was happening in this gist.
For all those looking for a solution/pattern and coming to this issue — here is an example, which we currently use for ourselves:
Label the children resources with name & namespace of the parent object (assuming they can be in different namespaces; if it is the same namespace by design, only the name is needed).
Watch for children resources that have this label (any value). In the watcher, get the name of the parent resource and patch its status field (e.g.
status.subpods
) with the status of the watched children resource (selected or agregated).Back in the parent resource, react to changes in that status field, and do the calculation on all the children overall statuses.
A sample skeleton code:
Thank you nolar for the detailed update. My current approach looks very similar to the example you provided. Will follow up closely on future releases.
The text was updated successfully, but these errors were encountered: