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 initial support for Pod.exec() #211

Merged
merged 10 commits into from
Nov 21, 2023
Merged

Conversation

jacobtomlinson
Copy link
Member

@jacobtomlinson jacobtomlinson commented Nov 21, 2023

This PR adds an initial implementation of Pod.exec() to enable running commands in remote containers.

from kr8s.objects import Pod

pod = Pod.get("my-pod")

command = pod.exec(["uptime"])
print(command.stdout.decode())
# 13:49:05 up 23:03,  0 users,  load average: 0.66, 0.87, 0.85

The Pod.exec command is intended to roughly mimic the behaviour of subprocess.run() and supports redirecting stdout/stderr from the container.

import sys
from kr8s.objects import Pod

pod = Pod.get("my-pod")

# Call `ls /` and direct stdout/stderr to the current process
# Also skip checking the return code of the command to avoid raising an exception
pod.exec(["ls", "/"], stdout=sys.stdout.buffer, stderr=sys.stderr.buffer, check=False)

Closes #209
Supersedes #192

@github-actions github-actions bot added documentation Improvements or additions to documentation kr8s kubectl-ng tests labels Nov 21, 2023
Copy link

codecov bot commented Nov 21, 2023

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (dde21b5) 95.03% compared to head (61ad3ae) 95.06%.

Files Patch % Lines
kr8s/_exec.py 93.84% 4 Missing ⚠️
kr8s/tests/test_objects.py 95.45% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #211      +/-   ##
==========================================
+ Coverage   95.03%   95.06%   +0.03%     
==========================================
  Files          25       26       +1     
  Lines        2458     2576     +118     
==========================================
+ Hits         2336     2449     +113     
- Misses        122      127       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jacobtomlinson jacobtomlinson merged commit 16175fa into kr8s-org:main Nov 21, 2023
10 of 11 checks passed
@jacobtomlinson jacobtomlinson deleted the execc branch November 21, 2023 14:29
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 kubectl-ng tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support subprocess.run() style command execution
1 participant