diff --git a/README.md b/README.md index 480992027a..d0ab6e01d2 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,32 @@ Python clients for talking to a [kubernetes](http://kubernetes.io/) cluster. ```python from __future__ import absolute_import +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') + +# 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(watch=False) +ret = v1.list_pod_for_all_namespaces() for i in ret.items: print "%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name) ``` +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 for generated client documentation, refer to [generated README](GEN_README.md). diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 0000000000..13d0123d14 --- /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 0000000000..4145cdc388 --- /dev/null +++ b/examples/example1.py @@ -0,0 +1,33 @@ +# 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. + +from __future__ import absolute_import + +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') + +# 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(watch=False) +for i in ret.items: + print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) + diff --git a/examples/example2.py b/examples/example2.py new file mode 100644 index 0000000000..390da85d61 --- /dev/null +++ b/examples/example2.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. + +from __future__ import absolute_import + +import k8sutil +import k8sclient +import os + + +def main(): + # 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() + count = 10 + watch = k8sutil.Watch() + for event in watch.stream(v1.list_namespace, _request_timeout=60): + print("--------------\n%s:\n%s" % (event['type'], event['object'])) + count -= 1 + if not count: + watch.stop() + + print("Ended.") + + +main() diff --git a/scripts/ROOT_README.md b/scripts/ROOT_README.md deleted file mode 100644 index 480992027a..0000000000 --- a/scripts/ROOT_README.md +++ /dev/null @@ -1,23 +0,0 @@ -# Kubernetes Python Client - -Python clients for talking to a [kubernetes](http://kubernetes.io/) cluster. - -## Example - -```python -from __future__ import absolute_import - -import k8sclient -import os - -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) -``` - -# Generated client README - -for generated client documentation, refer to [generated README](GEN_README.md). - diff --git a/scripts/generate.sh b/scripts/generate.sh index 495bf26164..b0ac63b42e 100755 --- a/scripts/generate.sh +++ b/scripts/generate.sh @@ -60,8 +60,8 @@ echo "--- Generating client ..." mvn -f "${SCRIPT_ROOT}/pom.xml" clean generate-sources -Dgenerator.spec.path="${SCRIPT_ROOT}/swagger.json" -Dgenerator.output.path="${CLIENT_ROOT}" -Dgenerator.package.name=${PACKAGE_NAME} echo "--- Patching generated code..." -cp "${CLIENT_ROOT}/README.md" "${CLIENT_ROOT}/GEN_README.md" -cp "${SCRIPT_ROOT}/ROOT_README.md" "${CLIENT_ROOT}/README.md" +mv "${CLIENT_ROOT}/README.md" "${CLIENT_ROOT}/GEN_README.md" +mv "${CLIENT_ROOT}/ROOT_README.md" "${CLIENT_ROOT}/README.md" cp "${SCRIPT_ROOT}/LICENSE" "${CLIENT_ROOT}" rm -rf "${CLIENT_ROOT}/test" diff --git a/scripts/pom.xml b/scripts/pom.xml index 30f377017c..e6c6ed4d6a 100644 --- a/scripts/pom.xml +++ b/scripts/pom.xml @@ -19,6 +19,7 @@ ${generator.spec.path} python + /Users/mehdy/projects/swagger-codegen/modules/swagger-codegen/src/main/resources/python ${generator.package.name} true