From 02a3a31c630c305527b328af49724f348fbdae15 Mon Sep 17 00:00:00 2001 From: reymont <258227346@qq.com> Date: Mon, 23 Jul 2018 20:38:13 +0800 Subject: [PATCH] python k8s --- examples/exec.py | 66 ++++++++++++++++++++++---------------------- examples/pod-list.py | 10 +++++++ 2 files changed, 43 insertions(+), 33 deletions(-) create mode 100644 examples/pod-list.py diff --git a/examples/exec.py b/examples/exec.py index 0fe7d5b78..410b4cd05 100644 --- a/examples/exec.py +++ b/examples/exec.py @@ -11,46 +11,46 @@ c.assert_hostname = False Configuration.set_default(c) api = core_v1_api.CoreV1Api() -name = 'busybox-test' +name = 'dubbo-admin-fp94l' resp = None try: resp = api.read_namespaced_pod(name=name, - namespace='default') + namespace='test01') 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.") +# 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. @@ -58,7 +58,7 @@ '/bin/sh', '-c', 'echo This message goes to stderr >&2; echo This message goes to stdout'] -resp = stream(api.connect_get_namespaced_pod_exec, name, 'default', +resp = stream(api.connect_get_namespaced_pod_exec, name, 'test01', command=exec_command, stderr=True, stdin=False, stdout=True, tty=False) @@ -66,7 +66,7 @@ # Calling exec interactively. exec_command = ['/bin/sh'] -resp = stream(api.connect_get_namespaced_pod_exec, name, 'default', +resp = stream(api.connect_get_namespaced_pod_exec, name, 'test01', command=exec_command, stderr=True, stdin=True, stdout=True, tty=False, diff --git a/examples/pod-list.py b/examples/pod-list.py new file mode 100644 index 000000000..45604e0ce --- /dev/null +++ b/examples/pod-list.py @@ -0,0 +1,10 @@ +from kubernetes import client, config + +# Configs can be set in Configuration class directly or using helper utility +config.load_kube_config() + +v1 = client.CoreV1Api() +print("Listing pods with their IPs:") +ret = v1.list_pod_for_all_namespaces(watch=False) +for i in ret.items: + print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) \ No newline at end of file