Skip to content

Commit 2597986

Browse files
committed
refactor: complete node-fetch to undici migration
1 parent f97942f commit 2597986

14 files changed

Lines changed: 286 additions & 391 deletions

examples/raw-example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as k8s from '@kubernetes/client-node';
2-
import fetch from 'node-fetch';
2+
import { fetch } from 'undici';
33
import https from 'node:https';
44

55
const kc = new k8s.KubeConfig();

package-lock.json

Lines changed: 0 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,12 @@
5858
"dependencies": {
5959
"@types/js-yaml": "^4.0.1",
6060
"@types/node": "^25.0.0",
61-
"@types/node-fetch": "^2.6.13",
6261
"@types/stream-buffers": "^3.0.3",
6362
"form-data": "^4.0.0",
6463
"hpagent": "^1.2.0",
6564
"isomorphic-ws": "^5.0.0",
6665
"js-yaml": "^4.1.0",
6766
"jsonpath-plus": "^10.3.0",
68-
"node-fetch": "^2.7.0",
6967
"openid-client": "^6.1.3",
7068
"rfc4648": "^1.3.0",
7169
"socks": "^2.8.4",

src/config.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import yaml from 'js-yaml';
66
import net from 'node:net';
77
import path from 'node:path';
88

9-
import { Headers, RequestInit } from 'node-fetch';
109
import {
1110
Agent as UndiciAgent,
1211
ProxyAgent as UndiciProxyAgent,
@@ -229,29 +228,6 @@ export class KubeConfig implements SecurityAuthentication {
229228
this.makePathsAbsolute(rootDirectory);
230229
}
231230

232-
public async applyToFetchOptions(opts: https.RequestOptions): Promise<RequestInit> {
233-
await this.applyToHTTPSOptions(opts);
234-
const headers = new Headers();
235-
for (const [key, val] of Object.entries(opts.headers || {})) {
236-
if (Array.isArray(val)) {
237-
val.forEach((innerVal) => {
238-
headers.append(key, innerVal);
239-
});
240-
} else if (typeof val === 'number' || typeof val === 'string') {
241-
headers.set(key, val.toString());
242-
}
243-
}
244-
if (opts.auth) {
245-
headers.set('Authorization', 'Basic ' + Buffer.from(opts.auth).toString('base64'));
246-
}
247-
return {
248-
agent: opts.agent,
249-
headers,
250-
method: opts.method,
251-
timeout: opts.timeout,
252-
} as RequestInit;
253-
}
254-
255231
public async applyToHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void> {
256232
const user = this.getCurrentUser();
257233
const cluster = this.getCurrentCluster();

src/config_test.ts

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ import child_process from 'node:child_process';
1111
import { readFileSync } from 'node:fs';
1212
import https from 'node:https';
1313
import http from 'node:http';
14-
import { Agent, RequestOptions } from 'node:https';
14+
import { RequestOptions } from 'node:https';
1515
import path, { dirname, join } from 'node:path';
1616
import { fileURLToPath } from 'node:url';
1717

1818
import mockfs from 'mock-fs';
1919

2020
import { Authenticator } from './auth.js';
21-
import fetch, { Headers } from 'node-fetch';
2221
import { HttpMethod } from './index.js';
23-
import { assertRequestAgentsEqual, assertRequestOptionsEqual } from './test/match-buffer.js';
22+
import { assertRequestOptionsEqual } from './test/match-buffer.js';
2423
import { CoreV1Api, RequestContext } from './api.js';
2524
import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolutePath } from './config.js';
2625
import { ActionOnInvalid, Cluster, newClusters, newContexts, newUsers, User } from './config_types.js';
@@ -276,42 +275,6 @@ describe('KubeConfig', () => {
276275
});
277276
});
278277

279-
describe('applytoFetchOptions', () => {
280-
it('should apply cert configs', async () => {
281-
const kc = new KubeConfig();
282-
kc.loadFromFile(kcFileName);
283-
kc.setCurrentContext('passwd');
284-
285-
const opts: https.RequestOptions = {
286-
method: 'POST',
287-
timeout: 5,
288-
headers: {
289-
number: 5,
290-
string: 'str',
291-
empty: undefined,
292-
list: ['a', 'b'],
293-
},
294-
};
295-
const requestInit = await kc.applyToFetchOptions(opts);
296-
const expectedCA = Buffer.from('CADATA2', 'utf-8');
297-
const expectedAgent = new https.Agent({
298-
ca: expectedCA,
299-
rejectUnauthorized: false,
300-
});
301-
302-
strictEqual(requestInit.method, 'POST');
303-
// timeout has been removed from the spec.
304-
strictEqual((requestInit as any).timeout, 5);
305-
const headers = requestInit.headers as Headers;
306-
strictEqual(Array.from(headers).length, 4);
307-
strictEqual(headers.get('Authorization'), 'Basic Zm9vOmJhcg==');
308-
strictEqual(headers.get('list'), 'a, b');
309-
strictEqual(headers.get('number'), '5');
310-
strictEqual(headers.get('string'), 'str');
311-
assertRequestAgentsEqual(requestInit.agent as Agent, expectedAgent);
312-
});
313-
});
314-
315278
describe('applyHTTPSOptions', () => {
316279
it('should apply tls-server-name to https.RequestOptions', async () => {
317280
const kc = new KubeConfig();
@@ -625,10 +588,6 @@ describe('KubeConfig', () => {
625588
strictEqual(namespaceList.kind, 'NamespaceList');
626589
strictEqual(namespaceList.items.length, 1);
627590
strictEqual(namespaceList.items[0].metadata?.name, 'default');
628-
629-
const res2 = await fetch(`https://${host}:${port}`, await kc.applyToFetchOptions({}));
630-
strictEqual(res2.status, 200);
631-
strictEqual(await res2.text(), 'ok');
632591
});
633592
});
634593

src/health.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import fetch from 'node-fetch';
1+
import { fetch } from 'undici';
22
import { KubeConfig } from './config.js';
33
import { RequestOptions } from 'node:https';
4+
import { HttpMethod, RequestContext } from './gen/http/http.js';
45

56
export class Health {
67
public config: KubeConfig;
@@ -27,15 +28,17 @@ export class Health {
2728
throw new Error('No currently active cluster');
2829
}
2930

30-
const requestURL = new URL(cluster.server + path);
31-
const requestInit = await this.config.applyToFetchOptions(opts);
32-
if (opts.signal) {
33-
requestInit.signal = opts.signal;
34-
}
35-
requestInit.method = 'GET';
31+
const requestURL = cluster.server + path;
32+
const ctx = new RequestContext(requestURL, HttpMethod.GET);
33+
await this.config.applySecurityAuthentication(ctx);
3634

3735
try {
38-
const response = await fetch(requestURL.toString(), requestInit);
36+
const response = await fetch(requestURL, {
37+
method: 'GET',
38+
headers: ctx.getHeaders(),
39+
dispatcher: ctx.getDispatcher(),
40+
signal: opts?.signal,
41+
});
3942
const status = response.status;
4043
if (status === 200) {
4144
return true;

0 commit comments

Comments
 (0)