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

Add explicit sync and asyncio APIs #40

Merged
merged 13 commits into from
Apr 30, 2023

Conversation

jacobtomlinson
Copy link
Member

@jacobtomlinson jacobtomlinson commented Apr 28, 2023

Closes #39

kr8s uses asyncio under the hood by default. This PR changes the public API to be sync by default. In order to support a sync API this PR:

  • Moves all library code into private submodules (objects.py => _objects.py, portforward.py => _portforward.py).
  • Expose all public code via kr8s.asyncio so that asyncio is available but opt-in.
  • Vendored in universalasync and modified to be opt-in only (it was only 100 lines or so and was hard to import and subclass without rewriting the whole thing).
  • Expose classes like Api and all objects including Pod, Service, etc with the universalasync wrapper.
  • Rename all object methods to be private (Pod.ready() => Pod._ready(), etc) and replace with new public method that awaits the private one.
    • This allows a method to call other methods with await regardless of the univeralasync wrapper.
  • Added nest-asyncio to allow the sync API to be used within a running event loop (Jupyter is a common case where users may not know there is an event loop running).

Sync API

import kr8s

api = kr8s.api()
pods = api.get("pods")  # Return Pod objects also are sync

for pod in pods:
    assert pod.exists()

Asyncio API

import kr8s.asyncio

api = kr8s.asyncio.api()
pods = await api.get("pods")  # Returned Pod objects are also asyncio

for pod in pods:
    assert await pod.exists()

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Apr 28, 2023
@jacobtomlinson jacobtomlinson changed the title Add sync and asyncio APIs Add explicit sync and asyncio APIs Apr 28, 2023
@codecov
Copy link

codecov bot commented Apr 28, 2023

Codecov Report

Merging #40 (99d5fde) into main (b1331f9) will decrease coverage by 0.60%.
The diff coverage is 95.37%.

@@            Coverage Diff             @@
##             main      #40      +/-   ##
==========================================
- Coverage   97.33%   96.73%   -0.60%     
==========================================
  Files          15       21       +6     
  Lines        1236     1562     +326     
==========================================
+ Hits         1203     1511     +308     
- Misses         33       51      +18     
Impacted Files Coverage Δ
kr8s/tests/test_objects.py 97.64% <84.21%> (-2.36%) ⬇️
kr8s/_asyncio.py 90.90% <90.90%> (ø)
kr8s/tests/test_api.py 99.00% <94.11%> (-1.00%) ⬇️
kr8s/_portforward.py 94.17% <94.17%> (ø)
kr8s/_objects.py 94.81% <94.81%> (ø)
kr8s/__init__.py 100.00% <100.00%> (ø)
kr8s/_api.py 98.46% <100.00%> (-0.04%) ⬇️
kr8s/asyncio/__init__.py 100.00% <100.00%> (ø)
kr8s/asyncio/objects.py 100.00% <100.00%> (ø)
kr8s/asyncio/portforward.py 100.00% <100.00%> (ø)
... and 4 more

@jacobtomlinson jacobtomlinson merged commit 6cf29b2 into kr8s-org:main Apr 30, 2023
@jacobtomlinson jacobtomlinson deleted the universalasync branch April 30, 2023 18:57
This was referenced Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation kr8s tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support sync API
1 participant