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

convert log to use fetch streaming #822

Merged
Changes from 1 commit
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
80 changes: 49 additions & 31 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import request = require('request');
import { Writable } from 'stream';
import { KubeConfig } from './config';
import { HttpError, ObjectSerializer } from './gen/api';

import { ApiException } from './api';
import { RequestOptions } from 'https';
import { V1Status } from './gen';
import fetch from 'node-fetch'
import { URL, URLSearchParams } from 'url';
export interface LogOptions {
/**
* Follow the log stream of the pod. Defaults to false.
Expand Down Expand Up @@ -44,6 +47,26 @@ export interface LogOptions {
timestamps?: boolean;
}

export function AddOptionsToSearchParams(options: LogOptions | undefined, searchParams: URLSearchParams) {
if (!options) {
return
}
searchParams.append('follow', options?.follow?.toString() || 'false');
if (options?.limitBytes) {
searchParams.set('limitBytes', options.limitBytes.toString());
}
searchParams.set('pretty', options?.follow?.toString() || 'false');
searchParams.set('previous', options?.previous?.toString() || 'false');
if (options?.sinceSeconds) {
searchParams.set('sinceSeconds', options?.sinceSeconds?.toString() || 'false');
}
if (options?.tailLines) {
searchParams.set('tailLines', options?.tailLines?.toString() || 'false');
}
searchParams.set('timestamps', options?.timestamps?.toString() || 'false');
return searchParams
}

export class Log {
public config: KubeConfig;

Expand Down Expand Up @@ -90,38 +113,33 @@ export class Log {
}
const url = cluster.server + path;

const requestOptions: request.Options = {
method: 'GET',
qs: {
...options,
container: containerName,
},
uri: url,
};
await this.config.applyToRequest(requestOptions);

return new Promise((resolve, reject) => {
const req = request(requestOptions, (error, response, body) => {
if (error) {
reject(error);
done(error);
} else if (response.statusCode !== 200) {
try {
const deserializedBody = ObjectSerializer.deserialize(JSON.parse(body), 'V1Status');
reject(new HttpError(response, deserializedBody, response.statusCode));
} catch (e) {
reject(new HttpError(response, body, response.statusCode));
const requestOptions: RequestOptions = {};

const requestURL = new URL(cluster.server + path);

var searchParams = requestURL.searchParams;
AddOptionsToSearchParams(options, searchParams);

await this.config.applytoHTTPSOptions(requestOptions);
console.log(requestURL.toString())
davidgamero marked this conversation as resolved.
Show resolved Hide resolved
const req = await fetch(requestURL.toString(), requestOptions)
.then(response => {
const status = response.status;
if (status == 200) {
davidgamero marked this conversation as resolved.
Show resolved Hide resolved
response.body.pipe(stream) // TODO: the follow search param still has the stream close prematurely based on my testing
} else if (status === 500) {
const v1status = response.body as V1Status;
const v1code = v1status.code;
const v1message = v1status.message;
if (v1code !== undefined && v1message !== undefined) {
throw new ApiException<undefined | V1Status>(v1code, v1message, v1status, response.headers.raw())
}
done(body);
} else {
done(null);
}
}).on('response', (response) => {
if (response.statusCode === 200) {
req.pipe(stream);
resolve(req);
throw new ApiException<undefined>(status, "Error occurred in log request", undefined, response.headers.raw())
}
}).catch((err) => {
throw new ApiException<undefined>(err, "Error occurred in log request", undefined, err)
});
});
return req;
}
}