Skip to content

Commit 1d18f90

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

File tree

2 files changed

+97
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)