Skip to content

Commit

Permalink
If no stdout/stderr requested tell Kubernetes not to send it
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson committed Nov 21, 2023
1 parent 8503458 commit 06b3435
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
12 changes: 8 additions & 4 deletions kr8s/_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ def __init__(
stdout: Union(str | BinaryIO) = None,
stderr: Union(str | BinaryIO) = None,
check: bool = True,
capture_output: bool = True,
) -> None:
self._resource = resource
self._container = container

self._stdin = stdin
self._stdout = stdout
self._stderr = stderr
self._capture_output = capture_output

self.args = command
self.stdout = b""
Expand All @@ -57,8 +59,8 @@ async def run(
params={
"command": self.args,
"container": self._container or self._resource.spec.containers[0].name,
"stdout": "true",
"stderr": "true",
"stdout": "true" if self._stdout or self._capture_output else "false",
"stderr": "true" if self._stderr or self._capture_output else "false",
"stdin": "true" if self._stdin is not None else "false",
},
) as ws:
Expand All @@ -75,11 +77,13 @@ async def run(
channel, message = int(message.data[0]), message.data[1:]
if message:
if channel == STDOUT_CHANNEL:
self.stdout += message
if self._capture_output:
self.stdout += message
if self._stdout:
self._stdout.write(message)
elif channel == STDERR_CHANNEL:
self.stderr += message
if self._capture_output:
self.stderr += message
if self._stderr:
self._stderr.write(message)
elif channel == ERROR_CHANNEL:
Expand Down
23 changes: 14 additions & 9 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,11 @@ async def _exec(
command: List[str],
*,
container: str = None,
stdin: Union(str | BinaryIO) = None,
stdout: Union(str | BinaryIO) = None,
stderr: Union(str | BinaryIO) = None,
stdin: Union(str | bytes | BinaryIO) = None,
stdout: BinaryIO = None,
stderr: BinaryIO = None,
check: bool = True,
capture_output: bool = True,
):
ex = Exec(
self,
Expand All @@ -789,6 +790,7 @@ async def _exec(
stderr=stderr,
stdin=stdin,
check=check,
capture_output=capture_output,
)
async with ex.run() as process:
await process.wait()
Expand All @@ -799,20 +801,22 @@ async def exec(
command: List[str],
*,
container: str = None,
stdin: Union(str | BinaryIO) = None,
stdout: Union(str | BinaryIO) = None,
stderr: Union(str | BinaryIO) = None,
stdin: Union(str | bytes | BinaryIO) = None,
stdout: BinaryIO = None,
stderr: BinaryIO = None,
check: bool = True,
capture_output: bool = True,
):
"""Run a command in a container and wait until it completes.
Args:
command: Command to execute.
container: Container to execute the command in.
stdin: If True, pass stdin to the container.
stdout: If True, capture stdout from the container.
stderr: If True, capture stderr from the container.
stdin: If set, read stdin to the container.
stdout: If set, write stdout to the provided writable stream object.
stderr: If set, write stderr to the provided writable stream object.
check: If True, raise an exception if the command fails.
capture_output: If True, store stdout and stderr from the container in an attribute.
"""
return await self._exec(
command,
Expand All @@ -821,6 +825,7 @@ async def exec(
stdout=stdout,
stderr=stderr,
check=check,
capture_output=capture_output,
)


Expand Down
3 changes: 2 additions & 1 deletion kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,10 @@ async def test_pod_exec_error(ubuntu_pod):

async def test_pod_exec_to_file(ubuntu_pod):
with tempfile.TemporaryFile(mode="w+b") as tmp:
await ubuntu_pod.exec(["date"], stdout=tmp)
exc = await ubuntu_pod.exec(["date"], stdout=tmp, capture_output=False)
tmp.seek(0)
assert str(datetime.datetime.now().year) in tmp.read().decode()
assert exc.stdout == b""

with tempfile.TemporaryFile(mode="w+b") as tmp:
with pytest.raises(ExecError):
Expand Down

0 comments on commit 06b3435

Please sign in to comment.