-
Notifications
You must be signed in to change notification settings - Fork 550
Fixes #82 Add a client for the pod read-log endpoint #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { default as request } from 'request'; | ||
import { Writable } from 'stream'; | ||
|
||
import { KubeConfig } from './config'; | ||
|
||
export interface LogOptions { | ||
/** | ||
* Follow the log stream of the pod. Defaults to false. | ||
*/ | ||
follow?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/** | ||
* If set, the number of bytes to read from the server before terminating the log output. This may not display a | ||
* complete final line of logging, and may return slightly more or slightly less than the specified limit. | ||
*/ | ||
limitBytes?: number; | ||
|
||
/** | ||
* If true, then the output is pretty printed. | ||
*/ | ||
pretty?: boolean; | ||
|
||
/** | ||
* Return previous terminated container logs. Defaults to false. | ||
*/ | ||
previous?: boolean; | ||
|
||
/** | ||
* A relative time in seconds before the current time from which to show logs. If this value precedes the time a | ||
* pod was started, only logs since the pod start will be returned. If this value is in the future, no logs will | ||
* be returned. Only one of sinceSeconds or sinceTime may be specified. | ||
*/ | ||
sinceSeconds?: number; | ||
|
||
/** | ||
* If set, the number of lines from the end of the logs to show. If not specified, logs are shown from the creation | ||
* of the container or sinceSeconds or sinceTime | ||
*/ | ||
tailLines?: number; | ||
|
||
/** | ||
* If true, add an RFC3339 or RFC3339Nano timestamp at the beginning of every line of log output. Defaults to false. | ||
*/ | ||
timestamps?: boolean; | ||
} | ||
|
||
export class Log { | ||
public config: KubeConfig; | ||
|
||
public constructor(config: KubeConfig) { | ||
this.config = config; | ||
} | ||
|
||
public log( | ||
namespace: string, | ||
podName: string, | ||
containerName: string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
stream: Writable, | ||
done: (err: any) => void, | ||
options: LogOptions = {}, | ||
): request.Request { | ||
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log`; | ||
|
||
const cluster = this.config.getCurrentCluster(); | ||
if (!cluster) { | ||
throw new Error('No currently active cluster'); | ||
} | ||
const url = cluster.server + path; | ||
|
||
const requestOptions: request.Options = { | ||
method: 'GET', | ||
qs: { | ||
...options, | ||
container: containerName, | ||
}, | ||
uri: url, | ||
}; | ||
this.config.applyToRequest(requestOptions); | ||
|
||
const req = request(requestOptions, (error, response, body) => { | ||
if (error) { | ||
done(error); | ||
} else if (response && response.statusCode !== 200) { | ||
done(body); | ||
} else { | ||
done(null); | ||
} | ||
}).on('response', (response) => { | ||
if (response.statusCode === 200) { | ||
req.pipe(stream); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unlike #214 this will only start to pipe the "logs" when a 200 response is returned (else Kubernetes error jsons might get treated as logs) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dbolkensteyn How to stop piping? I am successfully able to open the logs (follow option) but what is the right way to terminate underlying request? |
||
} | ||
}); | ||
|
||
return req; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is taken over from #214 as it seems as a nice way to avoid overly-parameterized methods