Skip to content
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

API for creating multiple resources #526

Open
jacobtomlinson opened this issue Dec 4, 2024 · 3 comments
Open

API for creating multiple resources #526

jacobtomlinson opened this issue Dec 4, 2024 · 3 comments
Labels
enhancement New feature or request good first issue Good description with clear steps for implementation. Great for newcomers

Comments

@jacobtomlinson
Copy link
Member

jacobtomlinson commented Dec 4, 2024

Which project are you requesting an enhancement for?

kr8s

What do you need?

It would be nice to have a way to conveniently create lots of resources. Right now you just have to iterate one by one and create each one. I expect this could be made much more convenient and faster by creating them concurrently.

# current sync api one-by-one
import kr8s

resources = [...]

for resource in resources:
    resource.create()  # Each create must complete before starting the next one

In the async API you could use a task group to make this concurrent.

# async task group
import asyncio
import kr8s

resources = [...]

async with asyncio.TaskGroup() as tg:
    for resource in resources:
        tg.create_task(resource.create())  # All create calls are invoked concurrently and gathers in the task group `__aexit__`

I expect both of these could be made more streamlined and consistent.

@jacobtomlinson jacobtomlinson added the enhancement New feature or request label Dec 4, 2024
@jacobtomlinson
Copy link
Member Author

jacobtomlinson commented Dec 4, 2024

Here's a suggestion for a solution. Create an Api.create(resources: list[APIResource]) method (and also a top level kr8s.create(...) and kr8s.asyncio.create(...) method).

# sync
import kr8s

resources = [...]
kr8s.create(resources)

# or
# api = kr8s.api()
# api.create(resources)
# async
import kr8s

resources = [...]
await kr8s.asyncio.create(resources)

# or
# api = await kr8s.asyncio.api()
# await api.create(resources)

@jacobtomlinson
Copy link
Member Author

I wonder if we also want an equivalent of wait too so that you can want for many resources to be in a phase before continuing.

@jacobtomlinson
Copy link
Member Author

jacobtomlinson commented Dec 5, 2024

Here's an example where these would be helpful and provide a cleaner API.

kr8s/kr8s/tests/test_api.py

Lines 427 to 433 in 55e235d

async with anyio.create_task_group() as tg:
for pod in pods:
tg.start_soon(pod.create)
async with anyio.create_task_group() as tg:
for pod in pods:
tg.start_soon(pod.wait, "condition=Ready")

This could be neatly simplified to

await kr8s.asyncio.create(pods)
await kr8s.asyncio.wait(pods, "condition=Ready")

@jacobtomlinson jacobtomlinson added the good first issue Good description with clear steps for implementation. Great for newcomers label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good description with clear steps for implementation. Great for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant