|
| 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 | +} |
0 commit comments