Skip to content

Commit 428bff4

Browse files
committed
Fixes #82 Add a client for the pod read-log endpoint
1 parent 864ef9d commit 428bff4

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

Diff for: src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './exec';
77
export * from './portforward';
88
export * from './types';
99
export * from './yaml';
10+
export * from './log';

Diff for: src/log.ts

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { Options } from 'request';
2+
import { Writable } from 'stream';
3+
4+
import { KubeConfig } from './config';
5+
import { RequestInterface, DefaultRequest } from './watch';
6+
7+
export interface LogOptions {
8+
/**
9+
* The container for which to stream logs. Defaults to only container if there is one container in the pod.
10+
*/
11+
container?: string;
12+
13+
/**
14+
* Follow the log stream of the pod. Defaults to false.
15+
*/
16+
follow?: boolean;
17+
18+
/**
19+
* If set, the number of bytes to read from the server before terminating the log output. This may not display a
20+
* complete final line of logging, and may return slightly more or slightly less than the specified limit.
21+
*/
22+
limitBytes?: number;
23+
24+
/**
25+
* If true, then the output is pretty printed.
26+
*/
27+
pretty?: boolean;
28+
29+
/**
30+
* Return previous terminated container logs. Defaults to false.
31+
*/
32+
previous?: boolean;
33+
34+
/**
35+
* A relative time in seconds before the current time from which to show logs. If this value precedes the time a
36+
* pod was started, only logs since the pod start will be returned. If this value is in the future, no logs will
37+
* be returned. Only one of sinceSeconds or sinceTime may be specified.
38+
*/
39+
sinceSeconds?: number;
40+
41+
/**
42+
* If set, the number of lines from the end of the logs to show. If not specified, logs are shown from the creation
43+
* of the container or sinceSeconds or sinceTime
44+
*/
45+
tailLines?: number;
46+
47+
/**
48+
* If true, add an RFC3339 or RFC3339Nano timestamp at the beginning of every line of log output. Defaults to false.
49+
*/
50+
timestamps?: boolean;
51+
}
52+
53+
export class Log {
54+
public config: KubeConfig;
55+
private readonly requestImpl: RequestInterface;
56+
57+
public constructor(config: KubeConfig, requestImpl?: RequestInterface) {
58+
this.config = config;
59+
if (requestImpl) {
60+
this.requestImpl = requestImpl;
61+
} else {
62+
this.requestImpl = new DefaultRequest();
63+
}
64+
}
65+
66+
public log(
67+
namespace: string,
68+
podName: string,
69+
stream: Writable,
70+
done: (err: any) => void,
71+
options: LogOptions = {},
72+
): any {
73+
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log`;
74+
75+
const cluster = this.config.getCurrentCluster();
76+
if (!cluster) {
77+
throw new Error('No currently active cluster');
78+
}
79+
const url = cluster.server + path;
80+
81+
const requestOptions: Options = {
82+
method: 'GET',
83+
qs: options,
84+
uri: url,
85+
};
86+
this.config.applyToRequest(requestOptions);
87+
88+
const req = this.requestImpl.webRequest(requestOptions, (error, response, body) => {
89+
if (error) {
90+
done(error);
91+
} else {
92+
done(null);
93+
}
94+
});
95+
req.pipe(stream);
96+
return req;
97+
}
98+
}

0 commit comments

Comments
 (0)