Skip to content

Commit 81ca8d3

Browse files
committed
Merge remote-tracking branch 'aogier/feature/476-copy-example'
2 parents 9344b6c + 5cb61bb commit 81ca8d3

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

examples/exec.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import tarfile
12
import time
3+
from tempfile import TemporaryFile
24

35
from kubernetes import config
46
from kubernetes.client import Configuration
@@ -74,6 +76,7 @@
7476
commands = [
7577
"echo test1",
7678
"echo \"This message goes to stderr\" >&2",
79+
"ls -l /etc",
7780
]
7881
while resp.is_open():
7982
resp.update(timeout=1)
@@ -95,3 +98,65 @@
9598
user = resp.readline_stdout(timeout=3)
9699
print("Server user is: %s" % user)
97100
resp.close()
101+
102+
# Copying file client -> pod
103+
print('copying client -> pod')
104+
exec_command = ['tar', 'xvf', '-', '-C', '/']
105+
resp = stream(api.connect_get_namespaced_pod_exec, name, 'default',
106+
command=exec_command,
107+
stderr=True, stdin=True,
108+
stdout=True, tty=False,
109+
binary=True,
110+
_preload_content=False)
111+
112+
source_file = '/tmp/dash'
113+
114+
with TemporaryFile() as tar_buffer:
115+
with tarfile.open(fileobj=tar_buffer, mode='w') as tar:
116+
tar.add(source_file)
117+
118+
tar_buffer.seek(0)
119+
commands = []
120+
commands.append(tar_buffer.read())
121+
122+
while resp.is_open():
123+
resp.update(timeout=1)
124+
if resp.peek_stdout():
125+
print("STDOUT: %s" % resp.read_stdout())
126+
if resp.peek_stderr():
127+
print("STDERR: %s" % resp.read_stderr())
128+
if commands:
129+
c = commands.pop(0)
130+
resp.write_stdin(c)
131+
else:
132+
break
133+
resp.close()
134+
135+
# Copying file pod -> client
136+
print('copying pod -> client')
137+
exec_command = ['tar', 'cf', '-', '/bin/sh']
138+
139+
with TemporaryFile() as tar_buffer:
140+
141+
resp = stream(api.connect_get_namespaced_pod_exec, name, 'default',
142+
command=exec_command,
143+
stderr=True, stdin=True,
144+
stdout=True, tty=False,
145+
binary=True,
146+
_preload_content=False)
147+
148+
while resp.is_open():
149+
resp.update(timeout=1)
150+
if resp.peek_stdout():
151+
out = resp.read_stdout()
152+
print("bytes received: %s" % len(out))
153+
tar_buffer.write(out)
154+
if resp.peek_stderr():
155+
print("STDERR: %s" % resp.read_stderr())
156+
resp.close()
157+
158+
tar_buffer.flush()
159+
tar_buffer.seek(0)
160+
161+
with tarfile.open(fileobj=tar_buffer, mode='r:') as tar:
162+
print('members', tar.getmembers())

kubernetes/client/apis/core_v1_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from six import iteritems
2222

2323
from ..api_client import ApiClient
24+
import six
2425

2526

2627
class CoreV1Api(object):
@@ -926,7 +927,7 @@ def connect_get_namespaced_pod_exec_with_http_info(self, name, namespace, **kwar
926927
body=body_params,
927928
post_params=form_params,
928929
files=local_var_files,
929-
response_type='str',
930+
response_type=six.binary_type,
930931
auth_settings=auth_settings,
931932
async=params.get('async'),
932933
_return_http_data_only=params.get('_return_http_data_only'),

0 commit comments

Comments
 (0)