I need to copy a file from a pod to the host by using kubernetes python client. It would be something like kubectl cp pod:file file
.
I'm testing the code from: https://github.com/prafull01/Kubernetes-Utilities/blob/master/kubectl_cp_as_python_client.py.
Specifically this code:
command_copy = ['tar', 'cf', '-', source_path]
with TemporaryFile() as tar_buffer:
exec_stream = stream(self.coreClient.connect_get_namespaced_pod_exec, pod_name, name_space,
command=command_copy, stderr=True, stdin=True, stdout=True, tty=False,
_preload_content=False)
# Copy file to stream
try:
while exec_stream.is_open():
exec_stream.update(timeout=1)
if exec_stream.peek_stdout():
out = exec_stream.read_stdout()
tar_buffer.write(out.encode('utf-8'))
if exec_stream.peek_stderr():
logger.debug("STDERR: %s" % exec_stream.read_stderr())
exec_stream.close()
tar_buffer.flush()
tar_buffer.seek(0)
with tarfile.open(fileobj=tar_buffer, mode='r:') as tar:
member = tar.getmember(source_path)
tar.makefile(member, destination_path)
return True
except Exception as e:
raise manage_kubernetes_exception(e)
I'm using the oficial Kubernetes Python library version 10.0.1 stable with Python 3.6.8
But it is not working properly:
- It is working when I copy small text files
- but it is not working for other files such as a tar or zip file. It copies a corrupted file with same size that the original.
Is there any mistake in the code? Do you have any other way to do it by using kubernetes python client?
All the best.
Thanks.