-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from mbohlool/exec
Improvements on ws_client
- Loading branch information
Showing
6 changed files
with
298 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import time | ||
|
||
from kubernetes import config | ||
from kubernetes.client import configuration | ||
from kubernetes.client.apis import core_v1_api | ||
from kubernetes.client.rest import ApiException | ||
|
||
config.load_kube_config() | ||
configuration.assert_hostname = False | ||
api = core_v1_api.CoreV1Api() | ||
name = 'busybox-test' | ||
|
||
resp = None | ||
try: | ||
resp = api.read_namespaced_pod(name=name, | ||
namespace='default') | ||
except ApiException as e: | ||
if e.status != 404: | ||
print("Unknown error: %s" % e) | ||
exit(1) | ||
|
||
if not resp: | ||
print("Pod %s does not exits. Creating it..." % name) | ||
pod_manifest = { | ||
'apiVersion': 'v1', | ||
'kind': 'Pod', | ||
'metadata': { | ||
'name': name | ||
}, | ||
'spec': { | ||
'containers': [{ | ||
'image': 'busybox', | ||
'name': 'sleep', | ||
"args": [ | ||
"/bin/sh", | ||
"-c", | ||
"while true;do date;sleep 5; done" | ||
] | ||
}] | ||
} | ||
} | ||
resp = api.create_namespaced_pod(body=pod_manifest, | ||
namespace='default') | ||
while True: | ||
resp = api.read_namespaced_pod(name=name, | ||
namespace='default') | ||
if resp.status.phase != 'Pending': | ||
break | ||
time.sleep(1) | ||
print("Done.") | ||
|
||
|
||
# calling exec and wait for response. | ||
exec_command = [ | ||
'/bin/sh', | ||
'-c', | ||
'echo This message goes to stderr >&2; echo This message goes to stdout'] | ||
resp = api.connect_get_namespaced_pod_exec(name, 'default', | ||
command=exec_command, | ||
stderr=True, stdin=False, | ||
stdout=True, tty=False) | ||
print("Response: " + resp) | ||
|
||
# Calling exec interactively. | ||
exec_command = ['/bin/sh'] | ||
resp = api.connect_get_namespaced_pod_exec(name, 'default', | ||
command=exec_command, | ||
stderr=True, stdin=True, | ||
stdout=True, tty=False, | ||
|
||
_preload_content=False) | ||
commands = [ | ||
"echo test1", | ||
"echo \"This message goes to stderr\" >&2", | ||
] | ||
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 + "\n") | ||
else: | ||
break | ||
|
||
resp.write_stdin("date\n") | ||
sdate = resp.readline_stdout(timeout=3) | ||
print("Server date command returns: %s" % sdate) | ||
resp.write_stdin("whoami\n") | ||
user = resp.readline_stdout(timeout=3) | ||
print("Server user is: %s" % user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.