Copy file from pod to host by using kubernetes python client

1/12/2020

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.

-- Jorgese
kubernetes
kubernetes-python-client
python

1 Answer

1/12/2020

Do you have any other way to do it by using kubernetes python client?

If you just want one file from the Pod, then don't use tar but rather /bin/cat instead, with the added benefit that you can just write directly to the local file, without having to deal with the tar file format. The disadvantage to that approach is that you would be responsible for setting the permissions on the local file to match what you expected them to be, which is something that tar -xf does for you. But if you're copying a remote tar file or zip file, that limitation wouldn't apply anyway, and may make the code a lot easier to reason about

-- mdaniel
Source: StackOverflow