Skip to content

Commit 78281b8

Browse files
Fixes #82 Add a client for the pod read-log endpoint
1 parent a6d42ca commit 78281b8

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

node-client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './api';
33
export * from './attach';
44
export * from './watch';
55
export * from './exec';
6+
export * from './log';

node-client/src/log.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import querystring = require('querystring');
2+
import stream = require('stream');
3+
4+
import { KubeConfig } from './config';
5+
import { WebSocketHandler } from './web-socket-handler';
6+
7+
export class Log {
8+
public 'handler': WebSocketHandler;
9+
10+
public constructor(config: KubeConfig) {
11+
this.handler = new WebSocketHandler(config);
12+
}
13+
14+
// TODO add support for limitBytes, previous, sinceSeconds and tailLines
15+
public log(namespace: string, podName: string, containerName: string,
16+
follow: boolean,
17+
timestamps: boolean,
18+
stream: stream.Writable): Promise<void> {
19+
const query = {
20+
container: containerName,
21+
follow: follow,
22+
timestamps: timestamps,
23+
};
24+
const queryStr = querystring.stringify(query);
25+
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log?${queryStr}`;
26+
const promise = this.handler.connect(path, null, (streamNum: number, buff: Buffer) => {
27+
const charBuff = Buffer.allocUnsafe(1)
28+
charBuff.writeInt8(streamNum, 0)
29+
stream.write(Buffer.concat([charBuff, buff]));
30+
});
31+
const result = new Promise<void>((resolvePromise, reject) => {
32+
promise.then(() => resolvePromise(), (err) => reject(err));
33+
});
34+
return result;
35+
}
36+
}

node-client/src/web-socket-handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const protocols = [
1010
'v3.channel.k8s.io',
1111
'v2.channel.k8s.io',
1212
'channel.k8s.io',
13+
'binary.k8s.io',
1314
];
1415

1516
export class WebSocketHandler {
@@ -87,7 +88,7 @@ export class WebSocketHandler {
8788
textHandler(message.utf8Data);
8889
}
8990
} else if (message.type === 'binary') {
90-
if (binaryHandler) {
91+
if (binaryHandler && message.binaryData.length > 0) {
9192
const streamNum = message.binaryData.readInt8(0);
9293
binaryHandler(streamNum, message.binaryData.slice(1));
9394
}

0 commit comments

Comments
 (0)