Skip to content

Commit a091fb6

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

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
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';

src/log.ts

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

0 commit comments

Comments
 (0)