python icon indicating copy to clipboard operation
python copied to clipboard

How to close stdin in a `exec` response?

Open mahesh-hegde opened this issue 9 months ago • 4 comments

What is the feature and why do you need it: I have the use case to do the python equivalent of

cat $local_file | kubectl exec command_in_container

I have been able to use something like this

        core_api = client.CoreV1Api(api_client)
        stream_response: WSClient = stream(
            core_api.connect_get_namespaced_pod_exec, pod_name,
            pod_namespace, command=command,
            stdin=True,
            stdout=False,
            stderr=True,
            _preload_content=False,
        )
        # Ideally I need to do streaming-write, but for sake of example, just a text:
        if input_text:
          stream_response.write_stdin(input_text)

But here's the problem. The process invoked on container through the exec command needs to know the input has ended. I don't find a way to close the stdin of the command.

Describe the solution you'd like to see:

I think ideally we have a function to close the stdin

response.close_stdin()

Need to check how go client and kubectl handles this.

mahesh-hegde avatar Mar 08 '25 11:03 mahesh-hegde

On golang / kubectl side, it seems client-go library handles this

https://github.com/kubernetes/client-go/blob/b3c87441408e747a62785f2f1478de64d9da5293/tools/remotecommand/v2.go#L98

mahesh-hegde avatar Mar 08 '25 12:03 mahesh-hegde

i think this is not implemented currently in python.

yliaog avatar Mar 12 '25 19:03 yliaog

Is there a workaround?

mickvangelderen avatar May 05 '25 22:05 mickvangelderen

You can probably encode the input as base64 and have a wrapper function / process break on newline.

On Tue, 6 May, 2025, 4:05 am Mick van Gelderen, @.***> wrote:

mickvangelderen left a comment (kubernetes-client/python#2371) https://github.com/kubernetes-client/python/issues/2371#issuecomment-2852505648

Is there a workaround?

— Reply to this email directly, view it on GitHub https://github.com/kubernetes-client/python/issues/2371#issuecomment-2852505648, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALAKLFRMTWBOVHRDMKO2NEL247RRNAVCNFSM6AAAAABYS6YBFSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDQNJSGUYDKNRUHA . You are receiving this because you authored the thread.Message ID: @.***>

mahesh-hegde avatar May 06 '25 09:05 mahesh-hegde

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Aug 04 '25 10:08 k8s-triage-robot

I've stumbled upon the same problem. As a test, I'm trying to reproduce the result of echo hello | kubectl exec -i simple-server-pod -- tr a-z A-Z, but fail to do so with the python API.

The only way I kinda get it to work is by setting tty to True and writing <string>\x04 (i.e. appending a End of Transmission byte at the end, or Ctrl-D). TTY mode ensure the byte is interpreted properly, but comes with other, undesirable side effects, such as echoing the input.

As @mahesh-hegde noticed, the go client seems to do some proper closing magic, I do not fully understand (it seems to read from the channel until it's closed by the server, I think) and have no clue how to emulate.

I'd like to get this working in python, as the alternative would be calling kubectl to do my bidding, which seems unnecessary.

chris-crunchr avatar Aug 07 '25 10:08 chris-crunchr

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Sep 06 '25 11:09 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-triage-robot avatar Oct 06 '25 11:10 k8s-triage-robot

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

k8s-ci-robot avatar Oct 06 '25 11:10 k8s-ci-robot