I am trying to copy a binary file to Kubernetes pod using the following way. The script work if the file is text format but not for binary.
from kubernetes import client, config
try:
config.load_kube_config()
except TypeError:
config.load_incluster_config()
api = client.CoreV1Api()
exec_command = ['tar', 'xvf', '-', '-C', '/']
resp = stream(
api.connect_get_namespaced_pod_exec,
'pod_name', 'namespace', command=exec_command,
stderr=True, stdin=True, stdout=True, tty=False, _preload_content=False
)
source_file = './test_data/simulated/some.nc'
destination_path = '/tmp/some.nc'
with TemporaryFile() as tar_buffer:
with tarfile.open(fileobj=tar_buffer, mode='w') as tar:
tar.add(name=source_file, arcname=destination_path)
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)
resp.write_stdin(str(c))
# works if source and destination files are txt format
# and the above line is resp.write_stdin(c.decode())
else:
break
resp.close()
On the STDERR I am getting the the following log "tar: This does not look like a tar archive"
I find a package that is doing exactly this. My problem was copying binary files. Turns out this package runs a Kubernetes Job to complete the copying.