Skip to content

Commit c992781

Browse files
committed
fix: apply http agent if needed
1 parent 3283cb1 commit c992781

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs';
22
import https from 'node:https';
3+
import http from 'node:http';
34
import yaml from 'js-yaml';
45
import net from 'node:net';
56
import path from 'node:path';
@@ -544,6 +545,8 @@ export class KubeConfig implements SecurityAuthentication {
544545
} else {
545546
throw new Error('Unsupported proxy type');
546547
}
548+
} else if (cluster && cluster.server.startsWith('http:')) {
549+
agent = new http.Agent(agentOptions);
547550
} else {
548551
agent = new https.Agent(agentOptions);
549552
}

src/config_test.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { deepEqual, deepStrictEqual, notStrictEqual, rejects, strictEqual, throw
33
import child_process from 'node:child_process';
44
import { readFileSync } from 'node:fs';
55
import https from 'node:https';
6+
import http from 'node:http';
67
import { Agent, RequestOptions } from 'node:https';
78
import path, { dirname, join } from 'node:path';
89
import { fileURLToPath } from 'node:url';
@@ -298,7 +299,7 @@ describe('KubeConfig', () => {
298299
});
299300
});
300301

301-
describe('applyHTTPSOptions', () => {
302+
describe.only('applyHTTPSOptions', () => {
302303
it('should apply tls-server-name to https.RequestOptions', async () => {
303304
const kc = new KubeConfig();
304305
kc.loadFromFile(kcTlsServerNameFileName);
@@ -448,6 +449,30 @@ describe('KubeConfig', () => {
448449
message: 'Unsupported proxy type',
449450
});
450451
});
452+
it('should apply http agent if cluster.server starts with http and no proxy-url is provided', async () => {
453+
const kc = new KubeConfig();
454+
kc.loadFromFile(kcProxyUrl);
455+
kc.setCurrentContext('contextE');
456+
457+
const testServerName = 'http://example.com';
458+
const rc = new RequestContext(testServerName, HttpMethod.GET);
459+
460+
await kc.applySecurityAuthentication(rc);
461+
462+
strictEqual(rc.getAgent() instanceof http.Agent, true);
463+
});
464+
it('should apply https agent if cluster.server starts with https and no proxy-url is provided', async () => {
465+
const kc = new KubeConfig();
466+
kc.loadFromFile(kcProxyUrl);
467+
kc.setCurrentContext('contextF');
468+
469+
const testServerName = 'https://example.com';
470+
const rc = new RequestContext(testServerName, HttpMethod.GET);
471+
472+
await kc.applySecurityAuthentication(rc);
473+
474+
strictEqual(rc.getAgent() instanceof https.Agent, true);
475+
});
451476
});
452477

453478
describe('loadClusterConfigObjects', () => {

0 commit comments

Comments
 (0)