Skip to content

Commit db2f4fb

Browse files
committed
Add examples
1 parent 3ad3c5d commit db2f4fb

File tree

6 files changed

+200
-13
lines changed

6 files changed

+200
-13
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,55 @@ Python clients for talking to a [kubernetes](http://kubernetes.io/) cluster.
44

55
## Example
66

7-
```python
8-
from __future__ import absolute_import
7+
list all pods:
98

9+
```python
10+
import k8sutil
1011
import k8sclient
1112
import os
1213

14+
# Configs can be set in Configuration class directly or using helper utility
15+
k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config')
16+
1317
v1=k8sclient.CoreV1Api()
14-
print "Listing pods with their IPs:"
18+
print("Listing pods with their IPs:")
1519
ret = v1.list_pod_for_all_namespaces(watch=False)
1620
for i in ret.items:
17-
print "%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)
21+
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
22+
```
23+
24+
watch on namespace object:
25+
26+
```python
27+
import k8sutil
28+
import k8sclient
29+
import os
30+
31+
32+
# Configs can be set in Configuration class directly or using helper utility
33+
k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config')
34+
35+
v1 = k8sclient.CoreV1Api()
36+
count = 10
37+
watch = k8sutil.Watch()
38+
for event in watch.stream(v1.list_namespace, _request_timeout=60):
39+
print("Event: %s %s" % (event['type'], event['object'].metadata.name))
40+
count -= 1
41+
if not count:
42+
watch.stop()
43+
44+
print("Ended.")
1845
```
1946

47+
More examples can be found in [examples](examples/) folder. To run examples, run this command:
48+
49+
```shell
50+
python -m examples.example1
51+
```
52+
53+
(replace example1 with the example base filename)
54+
55+
2056
# Generated client README
2157

2258

README.prefix

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,44 @@ Python clients for talking to a [kubernetes](http://kubernetes.io/) cluster.
44

55
## Example
66

7-
```python
8-
from __future__ import absolute_import
7+
list all pods:
98

9+
```python
1010
import k8sutil
1111
import k8sclient
1212
import os
1313

1414
# Configs can be set in Configuration class directly or using helper utility
1515
k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config')
1616

17-
# Prior to python 3.4 hosts with ip-addresses cannot be verified for SSL. this
18-
# utility function fixes that.
19-
k8sutil.fix_ssl_hosts_with_ipaddress()
20-
2117
v1=k8sclient.CoreV1Api()
22-
print "Listing pods with their IPs:"
23-
ret = v1.list_pod_for_all_namespaces()
18+
print("Listing pods with their IPs:")
19+
ret = v1.list_pod_for_all_namespaces(watch=False)
2420
for i in ret.items:
25-
print "%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)
21+
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
22+
```
23+
24+
watch on namespace object:
25+
26+
```python
27+
import k8sutil
28+
import k8sclient
29+
import os
30+
31+
32+
# Configs can be set in Configuration class directly or using helper utility
33+
k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config')
34+
35+
v1 = k8sclient.CoreV1Api()
36+
count = 10
37+
watch = k8sutil.Watch()
38+
for event in watch.stream(v1.list_namespace, _request_timeout=60):
39+
print("Event: %s %s" % (event['type'], event['object'].metadata.name))
40+
count -= 1
41+
if not count:
42+
watch.stop()
43+
44+
print("Ended.")
2645
```
2746

2847
More examples can be found in [examples](examples/) folder. To run examples, run this command:
@@ -33,6 +52,7 @@ python -m examples.example1
3352

3453
(replace example1 with the example base filename)
3554

55+
3656
# Generated client README
3757

3858

examples/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Empty init file to make examples folder a python module.

examples/example1.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import k8sclient
18+
import k8sutil
19+
20+
21+
def main():
22+
# Configs can be set in Configuration class directly or using helper
23+
# utility
24+
k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config')
25+
26+
v1 = k8sclient.CoreV1Api()
27+
print("Listing pods with their IPs:")
28+
ret = v1.list_pod_for_all_namespaces(watch=False)
29+
for i in ret.items:
30+
print("%s\t%s\t%s" %
31+
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
32+
33+
34+
if __name__ == '__main__':
35+
main()

examples/example2.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import k8sclient
18+
import k8sutil
19+
20+
21+
def main():
22+
# Configs can be set in Configuration class directly or using helper
23+
# utility
24+
k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config')
25+
26+
v1 = k8sclient.CoreV1Api()
27+
count = 10
28+
watch = k8sutil.Watch()
29+
for event in watch.stream(v1.list_namespace, timeout_seconds=10):
30+
print("Event: %s %s" % (event['type'], event['object'].metadata.name))
31+
count -= 1
32+
if not count:
33+
watch.stop()
34+
35+
print("Ended.")
36+
37+
38+
if __name__ == '__main__':
39+
main()

examples/example3.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import k8sclient
18+
import k8sutil
19+
20+
21+
def main():
22+
# Configs can be set in Configuration class directly or using helper
23+
# utility
24+
k8sutil.load_kube_config(os.environ["HOME"] + '/.kube/config')
25+
26+
print("Supported APIs (* is preferred version):")
27+
print("%-20s %s" %
28+
("core", ",".join(k8sclient.CoreApi().get_api_versions().versions)))
29+
for api in k8sclient.ApisApi().get_api_versions().groups:
30+
versions = []
31+
for v in api.versions:
32+
name = ""
33+
if v.version == api.preferred_version.version and len(
34+
api.versions) > 1:
35+
name += "*"
36+
name += v.version
37+
versions.append(name)
38+
print("%-20s %s" % (api.name, ",".join(versions)))
39+
40+
41+
if __name__ == '__main__':
42+
main()

0 commit comments

Comments
 (0)