-
Notifications
You must be signed in to change notification settings - Fork 542
/
Copy pathfollow-logs.js
40 lines (31 loc) · 917 Bytes
/
follow-logs.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
const stream = require('stream');
const k8s = require('@kubernetes/client-node');
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const namespace = 'default';
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const log = new k8s.Log(kc);
const logStream = new stream.PassThrough();
logStream.on('data', (chunk) => {
// use write rather than console.log to prevent double line feed
process.stdout.write(chunk);
});
const main = async () => {
try {
const req = await log.log(namespace, 'pod1', 'container', logStream, {
follow: true,
tailLines: 50,
pretty: false,
timestamps: false,
});
if (req) {
// Disconnect after 5 seconds
await delay(5000);
req.abort();
}
} catch (err) {
console.error(err);
process.exit(1);
}
};
main();