forked from godaddy/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.js
63 lines (54 loc) · 1.66 KB
/
watch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Watch changes to a Deployments in a specific Namespace.
//
const Client = require('../lib').Client;
const config = require('../lib').config;
const JSONStream = require('json-stream');
const deploymentManifest = require('./nginx-deployment.json');
async function cleanup(client) {
try {
await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).delete();
} catch (err) {
console.warn(`Unable to delete deployment ${ deploymentManifest.metadata.name }. Skipping.`);
}
}
async function triggerEvents(client) {
//
// Trigger some events
//
for (let count = 0; count < 3; count++) {
await new Promise(resolve => setTimeout(resolve, 1000));
await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest });
await new Promise(resolve => setTimeout(resolve, 1000));
await cleanup(client);
}
}
async function main() {
try {
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });
//
// Clean up our cluster and get ready to trigger events.
//
await cleanup(client);
//
// Get a JSON stream for Deployment events
//
const stream = client.apis.apps.v1beta.watch.namespaces('default').deployments.getStream();
const jsonStream = new JSONStream();
stream.pipe(jsonStream);
jsonStream.on('data', object => {
console.log('Event: ', JSON.stringify(object, null, 2));
});
//
// Cause Events to be written to `jsonStream`
//
await triggerEvents(client);
//
// Disconnect from the watch endpoint
//
stream.abort();
} catch (err) {
console.error('Error: ', err);
}
}
main();