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

Kubernetes pod exec using python client not very interactive #1689

Closed
geek-bit opened this issue Jan 27, 2022 · 13 comments
Closed

Kubernetes pod exec using python client not very interactive #1689

geek-bit opened this issue Jan 27, 2022 · 13 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@geek-bit
Copy link

What happened:

I am following the official example to exec into a Kubernetes container using the python Kubernetes-client library.

The above code is able to exec into the container. However, the shell is not very interactive. The issues with it are:-

  • Arrow keys do not work. When pressing arrow keys, exec shows ^[[D^[[C^[[A^[[B characters

  • Tab auto-complete does not work. When I press tab character, it takes tab as input white spaces instead of auto-completing.

  • I have to hit the enter key twice to get the command output. Example:-

ls


/ # ls
abc.txt       etc           root          tmp
bin           home          sys           usr
dev           proc          terminfo.src  var
/ # 
  • Cannot work with vi editor inside the container because of not being able to use arrow and escape key. Example:-
first line
^[[C^[[D^[[C^[[B

Is there anything wrong with the code or is there any better way to exec into the container?

How to reproduce it (as minimally and precisely as possible):

from kubernetes import config
from kubernetes.client.api import core_v1_api
from kubernetes.stream import stream


def exec_commands(api_instance):

    resp = stream(api_instance.connect_get_namespaced_pod_exec,
                  'busybox',
                  'default',
                  command=['/bin/sh'],
                  stderr=True, stdin=True,
                  stdout=True, tty=True,
                  _preload_content=False)

    while resp.is_open():
        resp.update(timeout=1)
        if resp.peek_stdout():
            print("%s" % resp.read_stdout())
        if resp.peek_stderr():
            print("%s" % resp.read_stderr())

        command = input()

        if command == "exit":
            break
        else:
            resp.write_stdin(command + "\n")

    resp.close()


def main():
    config.load_kube_config("~/.kube/config")
    core_v1 = core_v1_api.CoreV1Api()
    exec_commands(core_v1)


if __name__ == '__main__':
    main()

Environment:

  • Kubernetes version (kubectl version) :
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.1", GitCommit:"632ed300f2c34f6d6d15ca4cef3d3c7073412212", GitTreeState:"clean", BuildDate:"2021-08-19T15:45:37Z", GoVersion:"go1.16.7", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:53:14Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
  • OS : MacOS Big Slur v11.6
  • Python version : Python 3.9.10
  • Python client version (pip list | grep kubernetes) : kubernetes 21.7.0
@geek-bit geek-bit added the kind/bug Categorizes issue or PR as related to a bug. label Jan 27, 2022
@geek-bit
Copy link
Author

geek-bit commented Feb 1, 2022

Can anyone please give any pointer for the solution?

@iciclespider
Copy link
Contributor

iciclespider commented Feb 4, 2022

In my opinion, the exec implementation has serious deficiencies. The one time I needed to perform exec's, I implemented my own handling of it. Here is the method I created, use and modify at your own risk:

    def exec(self, *command, stdin=None, progress_at_start=None, filter_initial_startswith=None):
        if progress_at_start:
            print(f"{progress_at_start}...", end='', flush=True)
            progress_at_start = time.time()
        wssock = kubernetes.stream.stream(
            self.core_api.connect_get_namespaced_pod_exec,
            self.jenkins,
            self.namespace,
            command=list(command),
            stdin=stdin is not None,
            tty=False,
            stdout=True,
            stderr=True,
            _preload_content=False,
        ).sock
        rlist = [wssock.sock]
        stdin_channel = bytes([kubernetes.stream.ws_client.STDIN_CHANNEL])
        stdout_channel = kubernetes.stream.ws_client.STDOUT_CHANNEL
        stderr_channel = kubernetes.stream.ws_client.STDERR_CHANNEL
        if isinstance(stdin, str):
            stdin = stdin.encode('utf-8')
        if isinstance(stdin, bytes):
            wssock.send(stdin_channel + stdin, websocket.ABNF.OPCODE_BINARY)
            stdin = None
        if stdin is not None:
            if not isinstance(stdin, int):
                print(file=sys.stderr)
                print(f"Unexpected stdin argument: {stdin}", file=sys.stderr)
                print(file=sys.stderr, flush=True)
                sys.exit(1)
            rlist.append(stdin)
        stdout = sys.stdout.fileno()
        stderr = sys.stderr.fileno()
        if isinstance(filter_initial_startswith, str):
            filter_initial_startswith = filter_initial_startswith.encode('utf-8')
        while True:
            rs, _ws, _xs = select.select(rlist, [], [], 1 if progress_at_start else None)
            if progress_at_start:
                now = time.time()
                if now - progress_at_start >= 1:
                    os.write(stdout, b'.')
                    progress_at_start = now
            if stdin in rs:
                data = os.read(stdin, 32 * 1024)
                if len(data) == 0:
                    rlist.remove(stdin)
                else:
                    wssock.send(stdin_channel + data, websocket.ABNF.OPCODE_BINARY)
            if wssock.sock in rs:
                opcode, frame = wssock.recv_data_frame(True)
                if opcode == websocket.ABNF.OPCODE_CLOSE:
                    rlist.remove(wssock.sock)
                elif opcode == websocket.ABNF.OPCODE_BINARY:
                    channel = frame.data[0]
                    data = frame.data[1:]
                    if channel in (stdout_channel, stderr_channel):
                        if filter_initial_startswith:
                            if len(data):
                                data = data.split(b'\n')
                                while len(data) and data[0].startswith(filter_initial_startswith):
                                    del data[0]
                                if len(data):
                                    filter_initial_startswith = None
                                    data = b'\n'.join(data)
                                else:
                                    data = b''
                        if len(data):
                            if progress_at_start:
                                os.write(stdout, b'\n')
                                progress_at_start = None
                            if channel == stdout_channel:
                                os.write(stdout, data)
                            else:
                                os.write(stderr, data)
                    elif channel == kubernetes.stream.ws_client.ERROR_CHANNEL:
                        if progress_at_start:
                            os.write(stdout, b'\n')
                        wssock.close()
                        error = json.loads(data)
                        if error['status'] == 'Success':
                            return 0
                        if error['reason'] == 'NonZeroExitCode':
                            for cause in error['details']['causes']:
                                if cause['reason'] == 'ExitCode':
                                    return int(cause['message'])
                        print(file=sys.stderr)
                        print(f"Failure running: {' '.join(command)}", file=sys.stderr)
                        print(f"Status: {error['status']} - Message: {error['message']}", file=sys.stderr)
                        print(file=sys.stderr, flush=True)
                        sys.exit(1)
                    else:
                        if progress_at_start:
                            os.write(stdout, b'\n')
                        print(file=sys.stderr)
                        print(f"Unexpected channel: {channel}", file=sys.stderr)
                        print(f"Data: {data}", file=sys.stderr)
                        print(file=sys.stderr, flush=True)
                        sys.exit(1)
                else:
                    if progress_at_start:
                        os.write(stdout, b'\n')
                    print(file=sys.stderr)
                    print(f"Unexpected websocket opcode: {opcode}", file=sys.stderr)
                    print(file=sys.stderr, flush=True)
                    sys.exit(1)

@geek-bit
Copy link
Author

geek-bit commented Feb 5, 2022

Thanks a lot for the reply.

This code still misses the piece of interactive terminal experience. For example, if I set tty=True and stdin=True and run this code the terminal that I get does not allow arrow keys and instead shows / # ^[[A^[[B^[[C^[[D characters after pressing arrow keys and tab auto-completion do not work as well for commands. Can you please help in solving that missing piece of the exec command?

@iciclespider
Copy link
Contributor

The control codes issue has nothing to do with this python kubernetes client. The python cmd package provides support for line-oriented command interpreters, and it deals with many of those control characters. See: https://docs.python.org/3/library/cmd.html

@ellieayla
Copy link

ellieayla commented Feb 7, 2022

Yeah, everything described here is how https://docs.python.org/3/library/functions.html#input works, and has nothing to do with kubernetes or this library. You can reproduce the observed behaviour locally with python -c 'while True: x=input(); print("Got", x)'

python -c 'while True: x=input(); print("Got", x)'
asdf
Got asdf
Got

Got
^[[A^[[B^[[C^[[D
Got

Fancy editor history behaviour (eg up arrow) can be achieved in two ways:

  • locally, where python knows the history, sends nothing until it has a full line of text to send - use something like https://docs.python.org/3/library/readline.html or https://docs.python.org/3/library/cmd.html. This is what python -i does.
  • remote, where python knows about single key presses, but knows nothing about whether Enter or G is special, and certainly knows nothing about history, by passing all keypresses straight through (no input()!) to the remote shell (/bin/sh) and letting it handle cursor movement and history. This is what kubectl exec does.

@geek-bit
Copy link
Author

geek-bit commented Feb 8, 2022

Thanks, @ellieayla, and @iciclespider. Using https://docs.python.org/3/library/cmd.html locally and sending commands ['/bin/sh', '-c', 'command'] to the remote shell one by one will do the trick for arrow keys.
However, commands involving directories and files will face issues such as cd, cp, etc. Also, editing files in a vi editor won't be possible inside the remote shell as files will exist local and not on the remote.

@iciclespider
Copy link
Contributor

This also has nothing to do with kubernetes, python, or the kubernetes client. That is just how running programs locally that interact with a remote server work. Sounds like you need to take some basic programming courses.

@roycaihw
Copy link
Member

cc @yliaog

@k8s-triage-robot
Copy link

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

This bot triages issues and PRs 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 or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

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

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 15, 2022
@iciclespider
Copy link
Contributor

@geek-bit This PR #1805 properly supports binary data and will support your use cases.

@k8s-triage-robot
Copy link

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

This bot triages issues and PRs 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 or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

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

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Jun 23, 2022
@k8s-triage-robot
Copy link

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

This bot triages issues and PRs 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 or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

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

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closing this issue.

In response to this:

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

This bot triages issues and PRs 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 or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

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

/close

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/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests

6 participants