Add examples

This commit is contained in:
mbohlool 2016-11-15 16:22:02 -08:00
parent 3ad3c5df94
commit db2f4fbcd8
6 changed files with 200 additions and 13 deletions

View File

@ -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

View File

@ -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

15
examples/__init__.py Normal file
View File

@ -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.

35
examples/example1.py Normal file
View File

@ -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()

39
examples/example2.py Normal file
View File

@ -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()

42
examples/example3.py Normal file
View File

@ -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()