小编Jor*_*ese的帖子

使用 kubernetes python 客户端将文件从 pod 复制到主机

我需要使用 kubernetes python 客户端将文件从 pod 复制到主机。它会像kubectl cp pod:file file.

我正在测试来自:https : //github.com/prafull01/Kubernetes-Utilities/blob/master/kubectl_cp_as_python_client.py的代码。

具体这段代码:

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) …
Run Code Online (Sandbox Code Playgroud)

python kubernetes kubernetes-python-client

6
推荐指数
2
解决办法
2023
查看次数