Skip to content

Commit

Permalink
Add notebook on how to watch changes to an object
Browse files Browse the repository at this point in the history
  • Loading branch information
djkonro committed Jul 6, 2017
1 parent 5e268fb commit 46d979e
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions examples/notebooks/watch_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How to watch changes to an object\n",
"==================\n",
"\n",
"In this notebook, we learn how to watch changes to a Kubernetes resource endpoint."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"from kubernetes import client, config, watch"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create API endpoint instance as well as API resource instances."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"config.load_kube_config()\n",
"v1 = client.CoreV1Api()\n",
"v1ext = client.ExtensionsV1beta1Api()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Run a watch on the Pods endpoint. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"count = len(v1.list_pod_for_all_namespaces().items)\n",
"w = watch.Watch()\n",
"for event in w.stream(v1.list_pod_for_all_namespaces):\n",
" print(\"Event: %s %s %s\" % (event['type'],event['object'].kind, event['object'].metadata.name))\n",
" count -= 1\n",
" if not count:\n",
" w.stop()\n",
"\n",
"print(\"Ended.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 46d979e

Please sign in to comment.