Skip to content

Commit

Permalink
Fixes #82 Add a client for the pod read-log endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dinesh-bolkensteyn-sonarsource committed Aug 15, 2018
1 parent a6d42ca commit 78281b8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions node-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './api';
export * from './attach';
export * from './watch';
export * from './exec';
export * from './log';
36 changes: 36 additions & 0 deletions node-client/src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import querystring = require('querystring');
import stream = require('stream');

import { KubeConfig } from './config';
import { WebSocketHandler } from './web-socket-handler';

export class Log {
public 'handler': WebSocketHandler;

public constructor(config: KubeConfig) {
this.handler = new WebSocketHandler(config);
}

// TODO add support for limitBytes, previous, sinceSeconds and tailLines
public log(namespace: string, podName: string, containerName: string,
follow: boolean,
timestamps: boolean,
stream: stream.Writable): Promise<void> {
const query = {
container: containerName,
follow: follow,
timestamps: timestamps,
};
const queryStr = querystring.stringify(query);
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log?${queryStr}`;
const promise = this.handler.connect(path, null, (streamNum: number, buff: Buffer) => {
const charBuff = Buffer.allocUnsafe(1)
charBuff.writeInt8(streamNum, 0)
stream.write(Buffer.concat([charBuff, buff]));
});
const result = new Promise<void>((resolvePromise, reject) => {
promise.then(() => resolvePromise(), (err) => reject(err));
});
return result;
}
}
3 changes: 2 additions & 1 deletion node-client/src/web-socket-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const protocols = [
'v3.channel.k8s.io',
'v2.channel.k8s.io',
'channel.k8s.io',
'binary.k8s.io',
];

export class WebSocketHandler {
Expand Down Expand Up @@ -87,7 +88,7 @@ export class WebSocketHandler {
textHandler(message.utf8Data);
}
} else if (message.type === 'binary') {
if (binaryHandler) {
if (binaryHandler && message.binaryData.length > 0) {
const streamNum = message.binaryData.readInt8(0);
binaryHandler(streamNum, message.binaryData.slice(1));
}
Expand Down

0 comments on commit 78281b8

Please sign in to comment.