Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/exec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import tarfile
import time
from tempfile import TemporaryFile

from kubernetes import config
from kubernetes.client import Configuration
Expand Down Expand Up @@ -74,6 +76,7 @@
commands = [
"echo test1",
"echo \"This message goes to stderr\" >&2",
"ls -l /etc",
]
while resp.is_open():
resp.update(timeout=1)
Expand All @@ -95,3 +98,65 @@
user = resp.readline_stdout(timeout=3)
print("Server user is: %s" % user)
resp.close()

# Copying file client -> pod
print('copying client -> pod')
exec_command = ['tar', 'xvf', '-', '-C', '/']
resp = stream(api.connect_get_namespaced_pod_exec, name, 'default',
command=exec_command,
stderr=True, stdin=True,
stdout=True, tty=False,
binary=True,
_preload_content=False)

source_file = '/bin/sh'

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)
resp.write_stdin(c)
else:
break
resp.close()

# Copying file pod -> client
print('copying pod -> client')
exec_command = ['tar', 'cf', '-', '/bin/sh']

with TemporaryFile() as tar_buffer:

resp = stream(api.connect_get_namespaced_pod_exec, name, 'default',
command=exec_command,
stderr=True, stdin=True,
stdout=True, tty=False,
binary=True,
_preload_content=False)

while resp.is_open():
resp.update(timeout=1)
if resp.peek_stdout():
out = resp.read_stdout()
print("bytes received: %s" % len(out))
tar_buffer.write(out)
if resp.peek_stderr():
print("STDERR: %s" % resp.read_stderr())
resp.close()

tar_buffer.flush()
tar_buffer.seek(0)

with tarfile.open(fileobj=tar_buffer, mode='r:') as tar:
print('members', tar.getmembers())
2 changes: 1 addition & 1 deletion kubernetes/base
3 changes: 2 additions & 1 deletion kubernetes/client/apis/core_v1_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from six import iteritems

from ..api_client import ApiClient
import six


class CoreV1Api(object):
Expand Down Expand Up @@ -926,7 +927,7 @@ def connect_get_namespaced_pod_exec_with_http_info(self, name, namespace, **kwar
body=body_params,
post_params=form_params,
files=local_var_files,
response_type='str',
response_type=six.binary_type,
auth_settings=auth_settings,
async=params.get('async'),
_return_http_data_only=params.get('_return_http_data_only'),
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ commands =
commands =
python -V
{toxinidir}/scripts/kube-init.sh py.test -vvv -s []
{toxinidir}/scripts/kube-init.sh python examples/exec.py

[testenv:py35-functional]
commands =
python -V
{toxinidir}/scripts/kube-init.sh py.test -vvv -s []
{toxinidir}/scripts/kube-init.sh python examples/exec.py

[testenv:py36-functional]
commands =
python -V
{toxinidir}/scripts/kube-init.sh py.test -vvv -s []
{toxinidir}/scripts/kube-init.sh python examples/exec.py

[testenv:coverage]
commands =
Expand Down