-
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.
WIP: Improvements on ws_client. Now the client can returns an object …
…to interact with websocket server
- Loading branch information
Showing
3 changed files
with
190 additions
and
39 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
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( | ||
context="gke_cloud-kubernetes-dev_us-central1-f_mehdy-cluster") | ||
configuration.assert_hostname = False | ||
api = core_v1_api.CoreV1Api() | ||
name = 'busybox-test2' | ||
|
||
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." | ||
|
||
exec_command = [ | ||
'/bin/sh', | ||
'-c', | ||
'sleep 1; echo This message goes to stderr >&2; sleep 2; echo test2'] | ||
resp = api.connect_get_namespaced_pod_exec(name, 'default', | ||
command=exec_command, | ||
stderr=True, stdin=True, | ||
stdout=True, tty=False, | ||
_preload_content=False) | ||
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()) | ||
|
||
exit(1) | ||
# This part does not work yet. resp.write_stdin does not work. | ||
|
||
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", | ||
"sleep 1", | ||
"echo This message goes to stderr >&2", | ||
"sleep 2", | ||
"exit" | ||
] | ||
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" % c | ||
resp.write_stdin(c) |
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