|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "deletable": true, |
| 7 | + "editable": true |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "How to watch changes to an object\n", |
| 11 | + "==================\n", |
| 12 | + "\n", |
| 13 | + "In this notebook, we learn how kubernetes API resource Watch endpoint is used to observe resource changes. It can be used to get information about changes to any kubernetes object." |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": { |
| 20 | + "collapsed": true, |
| 21 | + "deletable": true, |
| 22 | + "editable": true |
| 23 | + }, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "from kubernetes import client, config, watch" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "metadata": {}, |
| 32 | + "source": [ |
| 33 | + "### Load config from default location." |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": null, |
| 39 | + "metadata": { |
| 40 | + "collapsed": true |
| 41 | + }, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "config.load_kube_config()" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": { |
| 50 | + "deletable": true, |
| 51 | + "editable": true |
| 52 | + }, |
| 53 | + "source": [ |
| 54 | + "### Create API instance" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "metadata": { |
| 61 | + "collapsed": true, |
| 62 | + "deletable": true, |
| 63 | + "editable": true |
| 64 | + }, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "api_instance = client.CoreV1Api()" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "metadata": { |
| 73 | + "deletable": true, |
| 74 | + "editable": true |
| 75 | + }, |
| 76 | + "source": [ |
| 77 | + "### Run a Watch on the Pods endpoint. \n", |
| 78 | + "Watch would be executed and produce output about changes to any Pod. After running the cell below, You can test this by running the Pod notebook [create_pod.ipynb](create_pod.ipynb) and observing the additional output here. You can stop the cell from running by restarting the kernel." |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": null, |
| 84 | + "metadata": { |
| 85 | + "collapsed": false, |
| 86 | + "deletable": true, |
| 87 | + "editable": true |
| 88 | + }, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "w = watch.Watch()\n", |
| 92 | + "for event in w.stream(api_instance.list_pod_for_all_namespaces):\n
8000
", |
| 93 | + " print(\"Event: %s %s %s\" % (event['type'],event['object'].kind, event['object'].metadata.name))" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": null, |
| 99 | + "metadata": { |
| 100 | + "collapsed": true, |
| 101 | + "deletable": true, |
| 102 | + "editable": true |
| 103 | + }, |
| 104 | + "outputs": [], |
| 105 | + "source": [] |
| 106 | + } |
| 107 | + ], |
| 108 | + "metadata": { |
| 109 | + "kernelspec": { |
| 110 | + "display_name": "Python 2", |
| 111 | + "language": "python", |
| 112 | + "name": "python2" |
| 113 | + }, |
| 114 | + "language_info": { |
| 115 | + "codemirror_mode": { |
| 116 | + "name": "ipython", |
| 117 | + "version": 2 |
| 118 | + }, |
| 119 | + "file_extension": ".py", |
| 120 | + "mimetype": "text/x-python", |
| 121 | + "name": "python", |
| 122 | + "nbconvert_exporter": "python", |
| 123 | + "pygments_lexer": "ipython2", |
| 124 | + "version": "2.7.6" |
| 125 | + } |
| 126 | + }, |
| 127 | + "nbformat": 4, |
| 128 | + "nbformat_minor": 2 |
| 129 | +} |
0 commit comments