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

时间:2020-01-12 12:06:42

标签: python kubernetes kubernetes-python-client

我需要使用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)

我正在使用正式的Kubernetes Python库版本10.0.1,该版本在Python 3.6.8中稳定

但是它不能正常工作:

  • 复制小文本文件时可以使用
  • ,但不适用于tar或zip文件等其他文件。它以与原始文件相同的大小复制损坏的文件。

代码中是否有错误?您还有其他使用kubernetes python客户端的方法吗?

祝一切顺利。

谢谢。

2 个答案:

答案 0 :(得分:1)

您还有其他使用kubernetes python客户端的方法吗?

如果您只想从Pod中获取一个文件,则不要使用tar而是使用/bin/cat,这样做的好处是您可以直接将其直接写入本地文件,而不必处理tar文件格式。该方法的缺点是您将负责设置本地文件的权限以匹配您期望的权限,这是tar -xf为您所做的事情。但是,如果您要复制远程tar文件或zip文件,则无论如何该限制都将不适用,并且可能使代码更容易推断

答案 1 :(得分:0)

我通过使用以下代码来做到这一点:

def stream_copy_from_pod(self, pod_name, name_space, source_path, destination_path):
    """
    Copy file from pod to the host.

    :param pod_name: String. Pod name
    :param name_space: String. Namespace
    :param source_path: String. Pod destination file path
    :param destination_path: Host destination file path
    :return: bool
    """
    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:
            reader = WSFileManager(exec_stream)
            while True:
                out, err, closed = reader.read_bytes()
                if out:
                    tar_buffer.write(out)
                elif err:
                    logger.debug("Error copying file {0}".format(err.decode("utf-8", "replace")))
                if closed:
                    break
            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)

使用此Web套接字文件管理器进行读取。

class WSFileManager:
"""
WS wrapper to manage read and write bytes in K8s WSClient
"""

def __init__(self, ws_client):
    """

    :param wsclient: Kubernetes WSClient
    """
    self.ws_client = ws_client

def read_bytes(self, timeout=0):
    """
    Read slice of bytes from stream

    :param timeout: read timeout
    :return: stdout, stderr and closed stream flag
    """
    stdout_bytes = None
    stderr_bytes = None

    if self.ws_client.is_open():
        if not self.ws_client.sock.connected:
            self.ws_client._connected = False
        else:
            r, _, _ = select.select(
                (self.ws_client.sock.sock, ), (), (), timeout)
            if r:
                op_code, frame = self.ws_client.sock.recv_data_frame(True)
                if op_code == ABNF.OPCODE_CLOSE:
                    self.ws_client._connected = False
                elif op_code == ABNF.OPCODE_BINARY or op_code == ABNF.OPCODE_TEXT:
                    data = frame.data
                    if len(data) > 1:
                        channel = data[0]
                        data = data[1:]
                        if data:
                            if channel == STDOUT_CHANNEL:
                                stdout_bytes = data
                            elif channel == STDERR_CHANNEL:
                                stderr_bytes = data
    return stdout_bytes, stderr_bytes, not self.ws_client._connected
相关问题