kubernetes-client / python-base

Apache License 2.0
70 stars 185 forks source link

Python3 write_channel "Can't convert 'bytes' object to str implicitly" #151

Closed hdimitriou closed 4 years ago

hdimitriou commented 5 years ago

I am using the rundeck kubernetes plugin to execute a script to a pod and I get the following error

15:53:46 | Traceback (most recent call last):
-- | --
15:53:46 | File "/home/rundeck/libext/cache/kubernetes-plugin-1.10.1-SNAPSHOT/pods-copy-file.py", line 69, in <module>
15:53:46 | main()
15:53:46 | File "/home/rundeck/libext/cache/kubernetes-plugin-1.10.1-SNAPSHOT/pods-copy-file.py", line 65, in main
15:53:46 | common.copy_file(name, namespace, container, source_file, destination_path, destination_file_name)
15:53:46 | File "/home/rundeck/libext/cache/kubernetes-plugin-1.10.1-SNAPSHOT/common.py", line 396, in copy_file
15:53:46 | resp.write_stdin(c)
15:53:46 | File "/usr/local/lib/python3.5/dist-packages/kubernetes/stream/ws_client.py", line 160, in write_stdin
15:53:46 | self.write_channel(STDIN_CHANNEL, data)
15:53:46 | File "/usr/local/lib/python3.5/dist-packages/kubernetes/stream/ws_client.py", line 114, in write_channel
15:53:46 | self.sock.send(chr(channel) + data)
15:53:46 | TypeError: Can't convert 'bytes' object to str implicitly

On this comment there is a workaround - a change to ws_client.py

def write_channel(self, channel, data):
    """Write data to a channel."""
    self.sock.send(bytes(chr(channel), 'utf-8') + data)

This worked for me, so maybe someone with more knowledge can see if it should be merged fixed this way, or there's something better

I am using 'Python 3.5.2' and I tried kubernetes 9.0.0 and 10.0.0 This issue does not happen with Python2.7

roycaihw commented 5 years ago

/assign

roycaihw commented 5 years ago

The ws client we use OPCODE_TEXT by default, which accepts utf-8 string or unicode as payload https://github.com/websocket-client/websocket-client/blob/66081be8e0523c9dfaffcb18ba1c8e4765e2288e/websocket/_core.py#L241-L253.

I think the error means that we don't support bytes payload yet. One fix could be supporting the opcode parameter in our client and do the bytes(chr(channel), 'utf-8') conversion when necessary, but we need to test that

I will work on this when I get time, but in the meantime feel free to jump in if anyone is interested.

/help

k8s-ci-robot commented 5 years ago

@roycaihw: This request has been marked as needing help from a contributor.

Please ensure the request meets the requirements listed here.

If this request no longer meets these requirements, the label can be removed by commenting with the /remove-help command.

In response to [this](https://github.com/kubernetes-client/python-base/issues/151): >The ws client we use `OPCODE_TEXT` by default, which accepts utf-8 string or unicode as payload https://github.com/websocket-client/websocket-client/blob/66081be8e0523c9dfaffcb18ba1c8e4765e2288e/websocket/_core.py#L241-L253. > >I think the error means that we don't support bytes payload yet. One fix could be supporting the `opcode` parameter in our client and do the `bytes(chr(channel), 'utf-8')` conversion when necessary, but we need to test that > >I will work on this when I get time, but in the meantime feel free to jump in if anyone is interested. > >/help Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
sergei-maertens commented 5 years ago

I'll give this a stab

fejta-bot commented 5 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

fejta-bot commented 4 years ago

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle rotten

fejta-bot commented 4 years ago

Rotten issues close after 30d of inactivity. Reopen the issue with /reopen. Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /close

k8s-ci-robot commented 4 years ago

@fejta-bot: Closing this issue.

In response to [this](https://github.com/kubernetes-client/python-base/issues/151#issuecomment-573303582): >Rotten issues close after 30d of inactivity. >Reopen the issue with `/reopen`. >Mark the issue as fresh with `/remove-lifecycle rotten`. > >Send feedback to sig-testing, kubernetes/test-infra and/or [fejta](https://github.com/fejta). >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
Ark-kun commented 4 years ago

I'm not sure this issue is fixed. /reopen

k8s-ci-robot commented 4 years ago

@Ark-kun: You can't reopen an issue/PR unless you authored it or you are a collaborator.

In response to [this](https://github.com/kubernetes-client/python-base/issues/151#issuecomment-584526031): >I'm not sure this issue is fixed. >/reopen Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
roycaihw commented 4 years ago

/reopen /remove-lifecycle rotten /unassign

This issue still exists. https://github.com/kubernetes-client/python-base/pull/152 is a great potential fix for it

k8s-ci-robot commented 4 years ago

@roycaihw: Reopened this issue.

In response to [this](https://github.com/kubernetes-client/python-base/issues/151#issuecomment-584887792): >/reopen >/remove-lifecycle rotten >/unassign > >This issue still exists Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
roycaihw commented 4 years ago

Fixed by #152

/close

k8s-ci-robot commented 4 years ago

@roycaihw: Closing this issue.

In response to [this](https://github.com/kubernetes-client/python-base/issues/151#issuecomment-586049828): >Fixed by #152 > >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.