Skip to content
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

Added FollowLogs class #214

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/follow_logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { LineStream } from 'byline';
import request = require('request');
Copy link
Contributor

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.

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please make this consistent with the other api methods? namespace, podName, containerName

See
https://github.com/kubernetes-client/javascript/blob/master/src/exec.ts#L34
https://github.com/kubernetes-client/javascript/blob/master/src/attach.ts#L20
This only makes use of pod
https://github.com/kubernetes-client/javascript/blob/master/src/portforward.ts#L25

@brendandburns I would actually be in favour of requiring container name since logs operator on container similar to exec we can always default it to the cluster/config default if undefined.

Copy link
Contributor

Choose a reason for hiding this comment

The 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());
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should have stream.on('error', ...)


const req = request(requestOptions, (error, response, body) => {
if (error) {
done(error);
} else {
done(null);
}
});
req.pipe(stream);

return req;
}
}