-
Notifications
You must be signed in to change notification settings - Fork 544
Added FollowLogs class #214
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,116 @@ | ||
import { LineStream } from 'byline'; | ||
import request = require('request'); | ||
import { KubeConfig } from './config'; | ||
|
||
interface FollowLogsOptions { | ||
/** | ||
* The container for which to stream logs. Defaults to only container if there is one container in the pod. | ||
*/ | ||
container?: string; | ||
|
||
/** | ||
* 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?: string; | ||
|
||
/** | ||
* 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 FollowLogs { | ||
public config: KubeConfig; | ||
|
||
public constructor(config: KubeConfig) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* read log of the specified Pod | ||
* @param name name of the Pod | ||
* @param namespace object name and auth scope, such as for teams and projects | ||
* @param {FollowLogsOptions} options | ||
*/ | ||
public followPodLog( | ||
name: 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. Can we please make this consistent with the other api methods? See @brendandburns I would actually be in favour of requiring container name since logs operator on container similar to 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. that's fine with me too. consistent api makes sense. thanks for higlighting that. |
||
namespace: string, | ||
callback: (line: string) => void, | ||
done: (err: any) => void, | ||
options: FollowLogsOptions = {}, | ||
): request.Request { | ||
// verify required parameter 'name' | ||
if (!name) { | ||
throw new Error( | ||
'Required parameter name was null or undefined when calling readNamespacedPodLog.', | ||
); | ||
} | ||
|
||
// verify required parameter 'namespace' | ||
if (!namespace) { | ||
throw new Error( | ||
'Required parameter namespace was null or undefined when calling readNamespacedPodLog.', | ||
); | ||
} | ||
|
||
// Build URI | ||
const cluster = this.config.getCurrentCluster(); | ||
if (!cluster) { | ||
throw new Error('No currently active cluster'); | ||
} | ||
const uri = cluster.server + `/api/v1/namespaces/${namespace}/pods/${name}/log`; | ||
|
||
const requestOptions: request.Options = { | ||
method: 'GET', | ||
qs: { | ||
...options, | ||
follow: true, | ||
}, | ||
headers: {}, | ||
uri, | ||
useQuerystring: true, | ||
json: true, | ||
}; | ||
this.config.applyToRequest(requestOptions); | ||
|
||
const stream = new LineStream(); | ||
stream.on('data', (data) => { | ||
callback(data.toString()); | ||
}); | ||
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. Probably should have |
||
|
||
const req = request(requestOptions, (error, response, body) => { | ||
if (error) { | ||
done(error); | ||
} else { | ||
done(null); | ||
} | ||
}); | ||
req.pipe(stream); | ||
|
||
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.
use newer import syntax everywhere.