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

[wip] Implement pod exec functionality #192

Closed
wants to merge 2 commits into from
Closed
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
38 changes: 38 additions & 0 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,44 @@
async for line in resp.aiter_lines():
yield line

def exec(self, command: List[str], container: str) -> Generator[str]:
"""Executes a command inside a pod and returns the output."""

params = {}
params["stdout"] = True
params["tty"] = True
params["command"] = command

Check warning on line 738 in kr8s/_objects.py

View check run for this annotation

Codecov / codecov/patch

kr8s/_objects.py#L735-L738

Added lines #L735 - L738 were not covered by tests

if container:
params["container"] = container

Check warning on line 741 in kr8s/_objects.py

View check run for this annotation

Codecov / codecov/patch

kr8s/_objects.py#L740-L741

Added lines #L740 - L741 were not covered by tests

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about defaulting to the first container right here?

params["container"] = container or self.spec.containers[0].name

else:
# TODO: automatically choose correct container based on
# "kubectl.kubernetes.io/default-container" annotation
# or fallback to the "first" container of the pod
raise Error("A container must be provided")

Check warning on line 746 in kr8s/_objects.py

View check run for this annotation

Codecov / codecov/patch

kr8s/_objects.py#L746

Added line #L746 was not covered by tests

headers = {

Check warning on line 748 in kr8s/_objects.py

View check run for this annotation

Codecov / codecov/patch

kr8s/_objects.py#L748

Added line #L748 was not covered by tests
"X-Stream-Protocol-Version": "v4.channel.k8s.io",
}

# example "kubectl exec -n my-namespace my-pod -- cat /etc/passwd":
# POST https://{kube_api}/api/v1/namespaces/my-namespace/pods/my-pod/exec?command=cat&command=%2Fetc%2Fpasswd&container=nginx&stdin=true&stdout=true

# NOTE: DOES NOT WORK
# httpx does not support the "Connection: Upgrade" mechanism to switch the
# transport to SPDY or Websockets.
with contextlib.supress(httpx.ReadTimeout):
with self.api.call_api(

Check warning on line 759 in kr8s/_objects.py

View check run for this annotation

Codecov / codecov/patch

kr8s/_objects.py#L758-L759

Added lines #L758 - L759 were not covered by tests
"POST",
url=f"{self.endpoint}/{self.name}/exec",
namespace=self.namespace,
params=params,
stream=True,
headers=headers,
) as resp:
for line in resp.iter_lines():
yield line

Check warning on line 768 in kr8s/_objects.py

View check run for this annotation

Codecov / codecov/patch

kr8s/_objects.py#L767-L768

Added lines #L767 - L768 were not covered by tests

def portforward(self, remote_port: int, local_port: int = None) -> int:
"""Port forward a pod.

Expand Down
Loading