Я пытался преобразовать команду kubectl cp
в эквивалентную kubernetes python client
программу. У меня есть следующий код для этого:
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.3
и kubernetes 1.13
версию.