Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46d979e

Browse files
committed
Add notebook on how to watch changes to an object
1 parent 5e268fb commit 46d979e

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"How to watch changes to an object\n",
8+
"==================\n",
9+
"\n",
10+
"In this notebook, we learn how to watch changes to a Kubernetes resource endpoint."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"metadata": {
17+
"collapsed": true,
18+
"deletable": true,
19+
"editable": true
20+
},
21+
"outputs": [],
22+
"source": [
23+
"from kubernetes import client, config, watch"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"### Create API endpoint instance as well as API resource instances."
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {
37+
"collapsed": true,
38+
"deletable": true,
39+
"editable": true
40+
},
41+
"outputs": [],
42+
"source": [
43+
"config.load_kube_config()\n",
44+
"v1 = client.CoreV1Api()\n",
45+
"v1ext = client.ExtensionsV1beta1Api()"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"### Run a watch on the Pods endpoint. "
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"metadata": {
59+
"collapsed": false,
60+
"deletable": true,
61+
"editable": true
62+
},
63+
"outputs": [],
64+
"source": [
65+
"count = len(v1.list_pod_for_all_namespaces().items)\n",
66+
"w = watch.Watch()\n",
67+
"for event in w.stream(v1.list_pod_for_all_namespaces):\n",
68+
" print(\"Event: %s %s %s\" % (event['type'],event['object'].kind, event['object'].metadata.name))\n",
69+
" count -= 1\n",
70+
" if not count:\n",
71+
" w.stop()\n",
72+
"\n",
73+
"print(\"Ended.\")"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {
80+
"collapsed": true,
81+
"deletable": true,
82+
"editable": true
83+
},
84+
"outputs": [],
85+
"source": []
86+
}
87+
],
88+
"metadata": {
89+
"kernelspec": {
90+
"display_name": "Python 2",
91+
"language": "python",
92+
"name": "python2"
93+
},
94+
"language_info": {
95+
"codemirror_mode": {
96+
"name": "ipython",
97+
"version": 2
98+
},
99+
"file_extension": ".py",
100+
"mimetype": "text/x-python",
101+
"name": "python",
102+
"nbconvert_exporter": "python",
103+
"pygments_lexer": "ipython2",
104+
"version": "2.7.13"
105+
}
106+
},
107+
"nbformat": 4,
108+
"nbformat_minor": 2
109+
}

0 commit comments

Comments
 (0)