forked from godaddy/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanary-controller.js
More file actions
69 lines (62 loc) · 2.13 KB
/
canary-controller.js
File metadata and controls
69 lines (62 loc) · 2.13 KB
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
64
65
66
67
68
69
/* eslint no-console:0 */
//
// Simple canary controller example: check Pod logs for any log messages with
// level "error"; on error, re-label the Pod to remove it from a Service.
//
// You can create a Deployment and Service to experiment with using the
// coalmine example:
//
// $ kubectl apply -f examples/coalmine-deploy.json
// $ kubectl expose deployment coalmine --type=NodePort --selector='app=coalmine,state=stable'
// $ minikube service coalmine --url
//
const Client = require('kubernetes-client').Client;
const config = require('kubernetes-client').config;
const JSONStream = require('json-stream');
const namespace = 'default';
const deployment = 'coalmine';
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });
function watchPod(pod) {
const podClient = client.api.v1.namespaces(namespace).pods(pod);
const stream = podClient.log.getStream({ qs: { follow: true }});
const jsonStream = new JSONStream();
stream.pipe(jsonStream);
jsonStream.on('data', async object => {
console.log('Log event:', JSON.stringify(object, null, 2));
if (object.level === 'error') {
console.warn(`Error in ${ pod }`);
await podClient.patch({
body: {
metadata: {
labels: {
state: 'failed'
}
}
}
});
stream.abort();
}
});
//
// Watch logs for 60 seconds.
//
const timeout = setTimeout(() => stream.abort(), 60 * 1000);
jsonStream.on('end', () => clearTimeout(timeout));
}
async function main() {
try {
//
// Get the Pod names associated with the Deployment.
//
const manifest = await client.apis.apps.v1.namespaces(namespace).deployments(deployment).get();
const matchLabels = manifest.body.spec.selector.matchLabels;
const matchQuery = Object.keys(matchLabels)
.map(label => `${ label }=${ matchLabels[label] }`)
.join(',');
const pods = await client.api.v1.namespaces(namespace).pods.get({ qs: { labelSelector: matchQuery }});
pods.body.items.map(podManifest => podManifest.metadata.name).forEach(watchPod);
} catch (err) {
console.error('Error: ', err);
}
}
main();