-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Copy Files to a pod #476
Comments
Hi, unfortunately it doesn't seem there is another way to cope with that as even the kubectl implementation or this ticket seems to suggest this is the only way. |
Hi @aogier, |
Hi @ltamaster, AFAICT current examples/implementation use
so I'd expect it should work for any file type, as long as |
Hi @aogier , I am doing something like this:
|
Ah, I've seen that now. There are some problems with strings send over the websocket, this will in turn also become a python 2/3 issue. I think the problem reside in def write_channel(self, channel, data):
"""Write data to a channel."""
self.sock.send(bytes(chr(channel), 'utf-8') + data) now your modified example does not give errors: # Copying file
exec_command = ['/bin/sh']
resp = stream(api.connect_get_namespaced_pod_exec, name, 'default',
command=exec_command,
stderr=True, stdin=True,
stdout=True, tty=False,
_preload_content=False)
source_file = '/bin/sh'
destination_file = '/tmp/sh'
file = open(source_file, "rb")
buffer = b''
with open(source_file, "rb") as file:
buffer += file.read()
commands = []
commands.append(bytes("cat <<'EOF' >" + destination_file + "\n", 'utf-8'))
commands.append(buffer)
commands.append(bytes("EOF\n", 'utf-8'))
commands.append(bytes("date\n", 'utf-8'))
while resp.is_open():
resp.update(timeout=1)
if resp.peek_stdout():
print("STDOUT: %s" % resp.read_stdout())
if resp.peek_stderr():
print("STDERR: %s" % resp.read_stderr())
if commands:
c = commands.pop(0)
#print("Running command... %s\n" % c)
resp.write_stdin(c)
else:
break
resp.write_stdin(bytes("date\n", 'utf-8'))
sdate = resp.readline_stdout(timeout=3)
print("Server date command returns: %s" % sdate)
resp.write_stdin(bytes("whoami\n", 'utf-8'))
user = resp.readline_stdout(timeout=3)
print("Server user is: %s" % user)
resp.close() however, output is unexpected:
and copied file is truncated. I'd like to use tar rather than shell heredocs just for. What do you think ? Ciao |
Ok this PoC works well: # Copying file
exec_command = ['tar', 'xvf', '-', '-C', '/']
resp = stream(api.connect_get_namespaced_pod_exec, name, 'default',
command=exec_command,
stderr=True, stdin=True,
stdout=True, tty=False,
_preload_content=False)
source_file = '/tmp/dash.tar'
destination_file = '/tmp/sh'
file = open(source_file, "rb")
buffer = b''
with open(source_file, "rb") as file:
buffer += file.read()
commands = []
commands.append(buffer)
while resp.is_open():
resp.update(timeout=1)
if resp.peek_stdout():
print("STDOUT: %s" % resp.read_stdout())
if resp.peek_stderr():
print("STDERR: %s" % resp.read_stderr())
if commands:
c = commands.pop(0)
#print("Running command... %s\n" % c)
resp.write_stdin(c)
else:
break
resp.close() Things to do:
Some preliminary tests will be needed, though, as well as six'ing websocket/example code HTH, ciao ! |
real tar file PoC implemented here, let's wait for kubernetes-client/python-base#52 needed for this to properly work. ATM only client -> pod binary traffic works on py3, either direction seems to work in py2, ymmv ciao |
Hi @aogier , I tested your code and It worked great. I was trying to figure out how I can set a particular destination path on the tar command, is that possible? Thanks |
Hi @ltamaster, first of all I'm very glad this is working yeah :) Defining destination path is indeed possibile and it depends on how you want to do it. I'll suggest you couple of option starting from what i prefer the most: Using proposed
|
Thanks @aogier Adding this on your code made the trick: |
ah brilliant, this is a lot simpler ... I admit it, didn't studied well the tar module 🐐 thanks for sharing ! |
BTW, I got an error message on the line
The file is copied OK; the message is produced when that line is called. |
I've pushed a new |
Ok, I will try that |
@ltamaster FYI I've just deleted old tags and squashed/rebased all the stuff now everything reside at 5cb61bb and two PR submitted and I'm still curious if everything works well for you :) hope this will get accepted soon my work is done on that ciao ! 👍 |
@ltamaster cannot recreate this on examples/exec.py with either py2 and py3 ... are you using different code ? If yes could you please share it with me ? |
Yes, I tried your code using python 2.7.14. I will try with a fresh env. |
The fresh env (ubuntu 17.10 with python 2.7) gives me the same result |
ok thank you for your commitment I'll try to recreate it on travis |
Hi @ltamaster , I'm unfortunately unable to reproduce your issue on travis, relevant build here. Which exec.py stage is returning this error ? Could you paste |
Hi @aogier I am using the new OIDC authentication (my Kubernetes environment is tectonic ). I clone your repo but I cannot use because it doesn't have that support. Maybe the problem is there. |
Hi @aogier , I didn't get the error with your code (which create a pod directly). I noticed that when I run the same code to a pod created with a development I got the error. I created an example here, that is based on example/exec.py, but it create a deployment and connect to a pod from that deployment: example |
Hi @aogier Did you have the change to reproduce the warning message with the script that I sent? Thanks |
ehi ciao @ltamaster just got some vacations :) I'll look at the script by the end of the week ! Thanks, ciao |
This really useful, thank you sirs ;-) |
I am trying to use this PoC https://github.com/aogier/k8s-client-python/blob/12f1443895e80ee24d689c419b5642de96c58cc8/examples/exec.py#L101 I'm not sure how I should specify the file I want to pull from the pod using this solution. I am probably missing something here... |
Hi @aogier I tryed your code (with python 3.6) and I get this error Traceback (most recent call last):
File "cp_test.py", line 136, in <module>
resp.write_stdin(c)
File "/home/ubuntu/kubengine/kublog/venv/lib/python3.5/site-packages/kubernetes/stream/ws_client.py", line 160, in write_stdin
self.write_channel(STDIN_CHANNEL, data)
File "/home/ubuntu/kubengine/kublog/venv/lib/python3.5/site-packages/kubernetes/stream/ws_client.py", line 114, in write_channel
self.sock.send(chr(channel) + data)
TypeError: Can't convert 'bytes' object to str implicitly |
@revollat This might help: |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
Stale issues rot after 30d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
Rotten issues close after 30d of inactivity. Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
@fejta-bot: Closing this issue. In response to this:
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. |
/reopen |
@Ark-kun: You can't reopen an issue/PR unless you authored it or you are a collaborator. In response to this:
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. |
i want to copy foo.tar.gz from pod, when i doing like this
however,Compare the MD5 of the two files, inconsistent is there anything wrong? |
Hey, @aogier, thanks for sharing your code snippets earlier. I know it's been a while, but could you help me compile the final example of copying a file with this library? def copy_file(kube_conn, namespace: str, pod_name: str, source_file, dest_path: str):
pass Unfortunately, most of the links you shared earlier don't work nowadays:
Thanks! |
Okay, after several hours of experiments, I finally made it. def copy_file(kube_conn, namespace: str, pod_name: str, source_file: pathlib.Path, dest_path: str):
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode='w:tar') as tar:
tar.add(source_file, arcname=pathlib.Path(dest_path).joinpath(source_file.name))
commands = [buf.getvalue()]
# Copying file
exec_command = ['tar', 'xvf', '-', '-C', '/']
resp = stream.stream(kube_conn.connect_get_namespaced_pod_exec, pod_name, namespace,
command=exec_command,
stderr=True, stdin=True,
stdout=True, tty=False,
_preload_content=False)
while resp.is_open():
resp.update(timeout=1)
if resp.peek_stdout():
print(f"STDOUT: {resp.read_stdout()}")
if resp.peek_stderr():
print(f"STDERR: {resp.read_stderr()}")
if commands:
c = commands.pop(0)
resp.write_stdin(c)
else:
break
resp.close() |
This is my version, which passes gzipped data for better performance, based on @QIvan 's solution:
|
Is there a way to copy files to a pod?, I am trying with connect_get_namespaced_pod_exec but I think is very a limited workaround.
Thanks
The text was updated successfully, but these errors were encountered: