-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add sort_objects()
to make batch resource actions simpler
#33
Conversation
This adds an apply_many function, similar in intent to `kubectl apply -f some-yaml.yaml`, that applies a collection of Resources. This function also applies the Resources in a safe order by kind, in order to avoid cases like trying to create an instance of a Custom Resource before applying the CustomResourceDefinition.
This adds a delete_many() function, similar to apply_many() and in intent to `kubectl delete -f some-yaml.yaml`, that deletes a collection of Resources. This function deletes the resources in a safe order by kind, in order to avoid cases like trying to delete an instance of a Custom Resource after the CustomResourceDefinition has already been deleted. Also added here are tests for this function.
Did you find the corresponding logic in In general I would like to keep this library sufficiently low level and easy to learn. So I would prefer to avoid new methods that can be created with few lines of code. The key component of your PR is the sorting function and that is something we could include as it's non trivial and can be used for creating more high level functionalities. So something like from lightkube import sort_objects
for obj in sort_objects(objs, by='kind-rank'): # better name and signature TBD
client.apply(obj)
for obj in sort_objects(objs, by='kind-rank', reverse=True):
namespace = obj.metadata.namespace if isinstance(obj, NamespacedResource) else None
client.delete(obj, name=obj.metadata.name, namespace= namespace) We can also support other types of object sorting in the future. |
That makes sense to me - keeping this to a sorting helper works. In my opinion having a I made a mistake in the original post - as far as I can tell, |
Sorry, between business trips and covid I dropped the ball on this. I'll put a workable solution together next week |
This reverts commit 9e9e30b.
This reverts commit 81b7e0e.
This reverts commit 8cd45f5.
This adds the `sort_objects()` helper to sort iterables of resource objects in particular orders. Currently supported is the `kind` order, which orders resources by their kind in an apply-friendly order (avoiding things like applying an instance of a CR before applying the CRD, etc.). Also supported is an optional `reverse` parameter in the sorting function to reverse the returned order.
I have reverted the previous changes and added a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about codecs
as this functionality will be often used in combination with loading definitions, however it doesn't really have to do with conversions..
Maybe we can just add it into a separate module for now (core/sort_objects.py
?) and import the function name in __init__.py
.
Would be also good to have an example of usage of this function as part of docs/codecs.md
.
Client.apply_many()
and Client.delete_many()
for batch resource operationssort_objects()
to make batch resource actions simpler
Moves sort_objects to a new sort_objects.py. Misc other minor refactors.
@gtsystem thanks for the great feedback. How does it look now? I will write up a example for |
Thanks a lot, changes are merged. |
This PR proposes some helpers to address a problem with handling batches of resources. I'm not married to the API, etc., and am happy to adjust and flesh out anything that is missing (for example,
create_many()
?), but I'm mainly curious atm whether this feature would be appreciated in lightkube or if I should finish it as a separate helper I keep elsewhere.Working with a batch of resources has an order of operations challenge. For example, this valid YAML contains both a
ClusterRoleBinding
and theClusterRole
/ServiceAccount
it references:If users loop over the yaml and create the objects, they will get errors because the
ClusterRoleBinding
is created before the items it uses. Tools likekubectl
orkapp
handle this for the user.This PR is a proposal to similarly automate this issue with lightkube by adding
Client.apply_many()
andClient.delete_many()
helpers. They both accept iterables of resources and, before executing on them, order them in a safer order by kind. afaict this is sufficient to avoid any conflicts, but I'm flexible if there's something missing.