Skip to content

Add support for v5 Exec protocol when using Kubernetes 1.29+ #270

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions kr8s/_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
ERROR_CHANNEL = 3
RESIZE_CHANNEL = 4
CLOSE_CHANNEL = 255
EXEC_PROTOCOL = "v4.channel.k8s.io"


class Exec:
Expand Down Expand Up @@ -55,10 +54,15 @@ def __init__(
async def run(
self,
) -> None:
version = await self._resource.api.version()
if int(version["major"]) > 1 or int(version["minor"]) > 28:
exec_protocol = "v5.channel.k8s.io"
else:
exec_protocol = "v4.channel.k8s.io"
async with self._resource.api.open_websocket(
version=self._resource.version,
url=f"{self._resource.endpoint}/{self._resource.name}/exec",
protocols=(EXEC_PROTOCOL,),
protocols=(exec_protocol,),
namespace=self._resource.namespace,
params={
"command": self.args,
Expand All @@ -72,7 +76,7 @@ async def run(
if ws.protocol != "v5.channel.k8s.io":
raise ExecError(
"Stdin is not supported with protocol "
f"{ws.protocol}, only with v5.channel.k8s.io"
f"{ws.protocol}, only with v5.channel.k8s.io from Kubernetes 1.29+"
)
if isinstance(self._stdin, str):
await ws.send_bytes(STDIN_CHANNEL.to_bytes() + self._stdin.encode())
Expand Down
11 changes: 8 additions & 3 deletions kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,15 @@ async def test_pod_exec_to_file(ubuntu_pod):
assert b"invalid date" in tmp.read()


@pytest.mark.xfail(reason="Exec protocol v5.channel.k8s.io not available")
async def test_pod_exec_stdin(ubuntu_pod):
ex = await ubuntu_pod.exec(["cat"], stdin="foo")
assert b"foo" in ex.stdout
api = await kr8s.asyncio.api()
version = await api.version()
if int(version["major"]) == 1 and int(version["minor"]) < 29:
with pytest.raises(ExecError):
await ubuntu_pod.exec(["cat"], stdin="foo")
else:
ex = await ubuntu_pod.exec(["cat"], stdin="foo")
assert b"foo" in ex.stdout


async def test_pod_exec_not_ready(ns):
Expand Down