From db2f4fbcd8a7c71ad34d953aa69cf0445c345235 Mon Sep 17 00:00:00 2001 From: mbohlool Date: Tue, 15 Nov 2016 16:22:02 -0800 Subject: [PATCH] Add examples --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++---- README.prefix | 38 +++++++++++++++++++++++++++++--------- examples/__init__.py | 15 +++++++++++++++ examples/example1.py | 35 +++++++++++++++++++++++++++++++++++ examples/example2.py | 39 +++++++++++++++++++++++++++++++++++++++ examples/example3.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 200 insertions(+), 13 deletions(-) create mode 100644 examples/__init__.py create mode 100644 examples/example1.py create mode 100644 examples/example2.py create mode 100644 examples/example3.py diff --git a/README.md b/README.md index bcaa379ae..73b9a0e47 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,55 @@ Python clients for talking to a [kubernetes](http://kubernetes.io/) cluster. ## Example -```python -from __future__ import absolute_import +list all pods: +```python +import k8sutil import k8sclient import os +# Configs can be set in Configuration class directly or using helper utility +k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config') + v1=k8sclient.CoreV1Api() -print "Listing pods with their IPs:" +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) + print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) ``` +watch on namespace object: + +```python +import k8sutil +import k8sclient +import os + + +# Configs can be set in Configuration class directly or using helper utility +k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config') + +v1 = k8sclient.CoreV1Api() +count = 10 +watch = k8sutil.Watch() +for event in watch.stream(v1.list_namespace, _request_timeout=60): + print("Event: %s %s" % (event['type'], event['object'].metadata.name)) + count -= 1 + if not count: + watch.stop() + +print("Ended.") +``` + +More examples can be found in [examples](examples/) folder. To run examples, run this command: + +```shell +python -m examples.example1 +``` + +(replace example1 with the example base filename) + + # Generated client README diff --git a/README.prefix b/README.prefix index bf306c8f9..5b7a967dc 100644 --- a/README.prefix +++ b/README.prefix @@ -4,9 +4,9 @@ Python clients for talking to a [kubernetes](http://kubernetes.io/) cluster. ## Example -```python -from __future__ import absolute_import +list all pods: +```python import k8sutil import k8sclient import os @@ -14,15 +14,34 @@ import os # Configs can be set in Configuration class directly or using helper utility k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config') -# Prior to python 3.4 hosts with ip-addresses cannot be verified for SSL. this -# utility function fixes that. -k8sutil.fix_ssl_hosts_with_ipaddress() - v1=k8sclient.CoreV1Api() -print "Listing pods with their IPs:" -ret = v1.list_pod_for_all_namespaces() +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) + print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) +``` + +watch on namespace object: + +```python +import k8sutil +import k8sclient +import os + + +# Configs can be set in Configuration class directly or using helper utility +k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config') + +v1 = k8sclient.CoreV1Api() +count = 10 +watch = k8sutil.Watch() +for event in watch.stream(v1.list_namespace, _request_timeout=60): + print("Event: %s %s" % (event['type'], event['object'].metadata.name)) + count -= 1 + if not count: + watch.stop() + +print("Ended.") ``` More examples can be found in [examples](examples/) folder. To run examples, run this command: @@ -33,6 +52,7 @@ python -m examples.example1 (replace example1 with the example base filename) + # Generated client README diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 000000000..13d0123d1 --- /dev/null +++ b/examples/__init__.py @@ -0,0 +1,15 @@ +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Empty init file to make examples folder a python module. diff --git a/examples/example1.py b/examples/example1.py new file mode 100644 index 000000000..d690da17d --- /dev/null +++ b/examples/example1.py @@ -0,0 +1,35 @@ +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import k8sclient +import k8sutil + + +def main(): + # Configs can be set in Configuration class directly or using helper + # utility + k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config') + + v1 = k8sclient.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)) + + +if __name__ == '__main__': + main() diff --git a/examples/example2.py b/examples/example2.py new file mode 100644 index 000000000..4e334823a --- /dev/null +++ b/examples/example2.py @@ -0,0 +1,39 @@ +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import k8sclient +import k8sutil + + +def main(): + # Configs can be set in Configuration class directly or using helper + # utility + k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config') + + v1 = k8sclient.CoreV1Api() + count = 10 + watch = k8sutil.Watch() + for event in watch.stream(v1.list_namespace, timeout_seconds=10): + print("Event: %s %s" % (event['type'], event['object'].metadata.name)) + count -= 1 + if not count: + watch.stop() + + print("Ended.") + + +if __name__ == '__main__': + main() diff --git a/examples/example3.py b/examples/example3.py new file mode 100644 index 000000000..6bb00b2c8 --- /dev/null +++ b/examples/example3.py @@ -0,0 +1,42 @@ +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import k8sclient +import k8sutil + + +def main(): + # Configs can be set in Configuration class directly or using helper + # utility + k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config') + + print("Supported APIs (* is preferred version):") + print("%-20s %s" % + ("core", ",".join(k8sclient.CoreApi().get_api_versions().versions))) + for api in k8sclient.ApisApi().get_api_versions().groups: + versions = [] + for v in api.versions: + name = "" + if v.version == api.preferred_version.version and len( + api.versions) > 1: + name += "*" + name += v.version + versions.append(name) + print("%-20s %s" % (api.name, ",".join(versions))) + + +if __name__ == '__main__': + main()