python - kubernetes python 客户端中的 kubectl cp

标签 python kubernetes kubectl kubernetes-python-client

我一直在尝试将 kubectl cp 命令转换为等效的 kubernetes python 客户端 程序。我有以下代码:

from kubernetes import client, config
from kubernetes.stream import stream
import tarfile
from tempfile import TemporaryFile

# create an instance of the API class
config.load_kube_config()
api_instance = client.CoreV1Api()

exec_command = ['tar', 'xvf', '-', '-C', '/']
resp = stream(api_instance.connect_get_namespaced_pod_exec, "nginx-deployment-6bb6554bf-9sdtr", 'default',
              command=exec_command,
              stderr=True, stdin=True,
              stdout=True, tty=False,
              _preload_content=False)

source_file = '/tmp/abc.txt'

with TemporaryFile() as tar_buffer:
    with tarfile.open(fileobj=tar_buffer, mode='w') as tar:
        tar.add(source_file)

    tar_buffer.seek(0)
    commands = []
    commands.append(tar_buffer.read())

    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()

上面的代码给我以下错误:

/home/velotio/venv/bin/python /home/velotio/PycharmProjects/k8sClient/testing.py
Traceback (most recent call last):
  File "/home/velotio/PycharmProjects/k8sClient/testing.py", line 38, in <module>
    resp.write_stdin(c)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/stream/ws_client.py", line 160, in write_stdin
    self.write_channel(STDIN_CHANNEL, data)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/stream/ws_client.py", line 114, in write_channel
    self.sock.send(chr(channel) + data)
TypeError: must be str, not bytes

我正在使用 Python 3.6.3kubernetes 1.13 版本。

最佳答案

您必须将字节转换回字符串,这是 write_stdin 方法期望得到的。

例如:

resp.write_stdin(c.decode())

另一个例子:

# Array with two byte objects
In [1]: a = [b'1234', b'3455']

# Pop one of them
In [2]: c = a.pop(0)

# it is of byte type
In [3]: c
Out[3]: b'1234'

# wrapping in str won't help if you don't provide decoding
In [4]: str(c)
Out[4]: "b'1234'"

# With decoding
In [5]: str(c, 'utf-8')
Out[5]: '1234'

# Or simply use the decode str method
In [6]: c.decode()
Out[6]: '1234'

更多关于字节到字符串的转换: Convert bytes to a string?

关于python - kubernetes python 客户端中的 kubectl cp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54108278/

相关文章:

python - 从 dict 中检索对象,以便可以通过 json.dumps() 轻松转储该对象

python - 理解和评估强化学习中的不同方法

python - Python 3 引发的 IndentationError : unexpected indent,。Pod 部署 YAML 中 Kubernetes args 条目中使用的代码

kubernetes - 在Azure Kubernetes服务中部署MVC应用程序失败并出现错误- “Back-off restarting failed container”

python - 导入中断,因为库无法导入自己的模块

python - 无法在 python 中调度 com 对象

nginx - Kubernetes NGINX 入口重写目标注释中断

ubuntu - kubernetes minikube 不能与 mount 一起使用

docker - 如何使用pod-local环境变量在pod上执行kubectl命令?

kubernetes - kubectl apply 与 kubectl create?