Skip to content

Commit 688f401

Browse files
committed
Add optional timeoutMs parameter to makeApiClient
1 parent a69d851 commit 688f401

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/config.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ import {
2828
AuthMethodsConfiguration,
2929
Configuration,
3030
createConfiguration,
31+
Observable,
3132
SecurityAuthentication,
3233
ServerConfiguration,
3334
} from './gen/index.js';
35+
import { ResponseContext } from './gen/http/http.js';
3436
import { OpenIDConnectAuth } from './oidc_auth.js';
3537
import WebSocket from 'isomorphic-ws';
3638
import child_process from 'node:child_process';
3739
import { SocksProxyAgent } from 'socks-proxy-agent';
3840
import { HttpProxyAgent, HttpProxyAgentOptions, HttpsProxyAgent, HttpsProxyAgentOptions } from 'hpagent';
41+
import fetch from 'node-fetch';
42+
import { from } from './gen/rxjsStub.js';
3943

4044
const SERVICEACCOUNT_ROOT: string = '/var/run/secrets/kubernetes.io/serviceaccount';
4145
const SERVICEACCOUNT_CA_PATH: string = SERVICEACCOUNT_ROOT + '/ca.crt';
@@ -473,7 +477,12 @@ export class KubeConfig implements SecurityAuthentication {
473477
);
474478
}
475479

476-
public makeApiClient<T extends ApiType>(apiClientType: ApiConstructor<T>): T {
480+
public makeApiClient<T extends ApiType>(
481+
apiClientType: ApiConstructor<T>,
482+
options?: {
483+
timeoutMs?: number;
484+
},
485+
): T {
477486
const cluster = this.getCurrentCluster();
478487
if (!cluster) {
479488
throw new Error('No active cluster!');
@@ -485,6 +494,35 @@ export class KubeConfig implements SecurityAuthentication {
485494
const config: Configuration = createConfiguration({
486495
baseServer: baseServerConfig,
487496
authMethods: authConfig,
497+
// This HTTP API implementation is a copy of ./gen/http/isomorphic-fetch.ts,
498+
// extended with the timeout pass-through code.
499+
httpApi: {
500+
send(request: RequestContext): Observable<ResponseContext> {
501+
const method = request.getHttpMethod().toString();
502+
const body = request.getBody();
503+
504+
const resultPromise = fetch(request.getUrl(), {
505+
method: method,
506+
body: body as any,
507+
headers: request.getHeaders(),
508+
agent: request.getAgent(),
509+
timeout: options?.timeoutMs,
510+
}).then((resp: any) => {
511+
const headers: { [name: string]: string } = {};
512+
resp.headers.forEach((value: string, name: string) => {
513+
headers[name] = value;
514+
});
515+
516+
const body = {
517+
text: () => resp.text(),
518+
binary: () => resp.buffer(),
519+
};
520+
return new ResponseContext(resp.status, headers, body);
521+
});
522+
523+
return from<Promise<ResponseContext>>(resultPromise);
524+
},
525+
},
488526
});
489527

490528
const apiClient = new apiClientType(config);

0 commit comments

Comments
 (0)