Skip to content

Commit

Permalink
Merge pull request #703 from Legion2/log-error
Browse files Browse the repository at this point in the history
Replace callback with promise in Log
  • Loading branch information
k8s-ci-robot authored Aug 2, 2021
2 parents 1adb9a7 + de1cbb5 commit f17e404
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/follow-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ logStream.on('data', (chunk) => {
process.stdout.write(chunk);
});

log.log('default', 'pod1', 'container1', logStream, (err) => {console.log(err)}, {follow: true, tailLines: 50, pretty: false, timestamps: false})
log.log('default', 'pod1', 'container1', logStream, {follow: true, tailLines: 50, pretty: false, timestamps: false})
.catch(err => {console.log(err)})
.then(req => {
// disconnects after 5 seconds
setTimeout(function(){
req.abort();
}, 5000);
});

61 changes: 44 additions & 17 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request = require('request');
import * as request from 'request';
import { Writable } from 'stream';

import { KubeConfig } from './config';
import { HttpError, ObjectSerializer } from './gen/api';

export interface LogOptions {
/**
Expand Down Expand Up @@ -51,14 +51,37 @@ export class Log {
this.config = config;
}

public async log(
namespace: string,
podName: string,
containerName: string,
stream: Writable,
options?: LogOptions,
): Promise<request.Request>;
/** @deprecated done callback is deprecated */
public async log(
namespace: string,
podName: string,
containerName: string,
stream: Writable,
done: (err: any) => void,
options: LogOptions = {},
options?: LogOptions,
): Promise<request.Request>;
public async log(
namespace: string,
podName: string,
containerName: string,
stream: Writable,
doneOrOptions?: ((err: any) => void) | LogOptions,
options?: LogOptions,
): Promise<request.Request> {
let done: (err: any) => void = () => undefined;
if (typeof doneOrOptions === 'function') {
done = doneOrOptions;
} else {
options = doneOrOptions;
}

const path = `/api/v1/namespaces/${namespace}/pods/${podName}/log`;

const cluster = this.config.getCurrentCluster();
Expand All @@ -77,20 +100,24 @@ export class Log {
};
await 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);
}
return new Promise((resolve, reject) => {
const req = request(requestOptions, (error, response, body) => {
if (error) {
reject(error);
done(error);
} else if (response.statusCode !== 200) {
const deserializedBody = ObjectSerializer.deserialize(JSON.parse(body), 'V1Status');
reject(new HttpError(response, deserializedBody, response.statusCode));
done(body);
} else {
done(null);
}
}).on('response', (response) => {
if (response.statusCode === 200) {
req.pipe(stream);
resolve(req);
}
});
});

return req;
}
}

0 comments on commit f17e404

Please sign in to comment.