|
1 | 1 | import request = require('request');
|
2 | 2 | import { Writable } from 'stream';
|
3 | 3 | import { KubeConfig } from './config';
|
4 |
| -import { HttpError, ObjectSerializer } from './gen/api'; |
5 |
| - |
| 4 | +import { ApiException } from './api'; |
| 5 | +import { RequestOptions } from 'https'; |
| 6 | +import { V1Status } from './gen'; |
| 7 | +import fetch from 'node-fetch' |
| 8 | +import { URL, URLSearchParams } from 'url'; |
6 | 9 | export interface LogOptions {
|
7 | 10 | /**
|
8 | 11 | * Follow the log stream of the pod. Defaults to false.
|
@@ -44,6 +47,26 @@ export interface LogOptions {
|
44 | 47 | timestamps?: boolean;
|
45 | 48 | }
|
46 | 49 |
|
| 50 | +export function AddOptionsToSearchParams(options: LogOptions | undefined, searchParams: URLSearchParams) { |
| 51 | + if (!options) { |
| 52 | + return |
| 53 | + } |
| 54 | + searchParams.append('follow', options?.follow?.toString() || 'false'); |
| 55 | + if (options?.limitBytes) { |
| 56 | + searchParams.set('limitBytes', options.limitBytes.toString()); |
| 57 | + } |
| 58 | + searchParams.set('pretty', options?.follow?.toString() || 'false'); |
| 59 | + searchParams.set('previous', options?.previous?.toString() || 'false'); |
| 60 | + if (options?.sinceSeconds) { |
| 61 | + searchParams.set('sinceSeconds', options?.sinceSeconds?.toString() || 'false'); |
| 62 | + } |
| 63 | + if (options?.tailLines) { |
| 64 | + searchParams.set('tailLines', options?.tailLines?.toString() || 'false'); |
| 65 | + } |
| 66 | + searchParams.set('timestamps', options?.timestamps?.toString() || 'false'); |
| 67 | + return searchParams |
| 68 | +} |
| 69 | + |
47 | 70 | export class Log {
|
48 | 71 | public config: KubeConfig;
|
49 | 72 |
|
@@ -90,38 +113,33 @@ export class Log {
|
90 | 113 | }
|
91 | 114 | const url = cluster.server + path;
|
92 | 115 |
|
93 |
| - const requestOptions: request.Options = { |
94 |
| - method: 'GET', |
95 |
| - qs: { |
96 |
| - ...options, |
97 |
| - container: containerName, |
98 |
| - }, |
99 |
| - uri: url, |
100 |
| - }; |
101 |
| - await this.config.applyToRequest(requestOptions); |
102 |
| - |
103 |
| - return new Promise((resolve, reject) => { |
104 |
| - const req = request(requestOptions, (error, response, body) => { |
105 |
| - if (error) { |
106 |
| - reject(error); |
107 |
| - done(error); |
108 |
| - } else if (response.statusCode !== 200) { |
109 |
| - try { |
110 |
| - const deserializedBody = ObjectSerializer.deserialize(JSON.parse(body), 'V1Status'); |
111 |
| - reject(new HttpError(response, deserializedBody, response.statusCode)); |
112 |
| - } catch (e) { |
113 |
| - reject(new HttpError(response, body, response.statusCode)); |
| 116 | + const requestOptions: RequestOptions = {}; |
| 117 | + |
| 118 | + const requestURL = new URL(cluster.server + path); |
| 119 | + |
| 120 | + var searchParams = requestURL.searchParams; |
| 121 | + AddOptionsToSearchParams(options, searchParams); |
| 122 | + |
| 123 | + await this.config.applytoHTTPSOptions(requestOptions); |
| 124 | + |
| 125 | + const req = await fetch(requestURL.toString(), requestOptions) |
| 126 | + .then(response => { |
| 127 | + const status = response.status; |
| 128 | + if (status === 200) { |
| 129 | + response.body.pipe(stream) // TODO: the follow search param still has the stream close prematurely based on my testing |
| 130 | + } else if (status === 500) { |
| 131 | + const v1status = response.body as V1Status; |
| 132 | + const v1code = v1status.code; |
| 133 | + const v1message = v1status.message; |
| 134 | + if (v1code !== undefined && v1message !== undefined) { |
| 135 | + throw new ApiException<undefined | V1Status>(v1code, v1message, v1status, response.headers.raw()) |
114 | 136 | }
|
115 |
| - done(body); |
116 | 137 | } else {
|
117 |
| - done(null); |
118 |
| - } |
119 |
| - }).on('response', (response) => { |
120 |
| - if (response.statusCode === 200) { |
121 |
| - req.pipe(stream); |
122 |
| - resolve(req); |
| 138 | + throw new ApiException<undefined>(status, "Error occurred in log request", undefined, response.headers.raw()) |
123 | 139 | }
|
| 140 | + }).catch((err) => { |
| 141 | + throw new ApiException<undefined>(err, "Error occurred in log request", undefined, err) |
124 | 142 | });
|
125 |
| - }); |
| 143 | + return req; |
126 | 144 | }
|
127 | 145 | }
|
0 commit comments