Skip to content

Commit d02abae

Browse files
committed
add createAgent method
1 parent 934d358 commit d02abae

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

src/config.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export class KubeConfig implements SecurityAuthentication {
173173

174174
public async applyToHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void> {
175175
const user = this.getCurrentUser();
176+
const cluster = this.getCurrentCluster();
176177

177178
await this.applyOptions(opts);
178179

@@ -207,7 +208,7 @@ export class KubeConfig implements SecurityAuthentication {
207208
agentOptions.secureProtocol = opts.secureProtocol;
208209
agentOptions.sessionIdContext = opts.sessionIdContext;
209210

210-
opts.agent = new https.Agent(agentOptions);
211+
opts.agent = this.createAgent(cluster, agentOptions);
211212
}
212213

213214
/**
@@ -250,27 +251,7 @@ export class KubeConfig implements SecurityAuthentication {
250251
agentOptions.passphrase = httpsOptions.passphrase;
251252
agentOptions.rejectUnauthorized = httpsOptions.rejectUnauthorized;
252253

253-
let agent: https.Agent | SocksProxyAgent | HttpProxyAgent | HttpsProxyAgent;
254-
255-
if (cluster && cluster.proxyUrl) {
256-
if (cluster.proxyUrl.startsWith('socks')) {
257-
agent = new SocksProxyAgent(cluster.proxyUrl, agentOptions);
258-
} else if (cluster.server.startsWith('https')) {
259-
const httpsProxyAgentOptions: HttpsProxyAgentOptions = agentOptions as HttpsProxyAgentOptions;
260-
httpsProxyAgentOptions.proxy = cluster.proxyUrl;
261-
agent = new HttpsProxyAgent(httpsProxyAgentOptions);
262-
} else if (cluster.server.startsWith('http')) {
263-
const httpProxyAgentOptions: HttpProxyAgentOptions = agentOptions as HttpProxyAgentOptions;
264-
httpProxyAgentOptions.proxy = cluster.proxyUrl;
265-
agent = new HttpProxyAgent(httpProxyAgentOptions);
266-
} else {
267-
throw new Error('Unsupported proxy type');
268-
}
269-
} else {
270-
agent = new https.Agent(agentOptions);
271-
}
272-
273-
context.setAgent(agent);
254+
context.setAgent(this.createAgent(cluster, agentOptions));
274255
}
275256

276257
/**
@@ -531,6 +512,32 @@ export class KubeConfig implements SecurityAuthentication {
531512
return this.getContextObject(this.currentContext);
532513
}
533514

515+
private createAgent(
516+
cluster: Cluster | null,
517+
agentOptions: https.AgentOptions,
518+
): https.Agent | SocksProxyAgent | HttpProxyAgent | HttpsProxyAgent {
519+
let agent: https.Agent | SocksProxyAgent | HttpProxyAgent | HttpsProxyAgent;
520+
521+
if (cluster && cluster.proxyUrl) {
522+
if (cluster.proxyUrl.startsWith('socks')) {
523+
agent = new SocksProxyAgent(cluster.proxyUrl, agentOptions);
524+
} else if (cluster.server.startsWith('https')) {
525+
const httpsProxyAgentOptions: HttpsProxyAgentOptions = agentOptions as HttpsProxyAgentOptions;
526+
httpsProxyAgentOptions.proxy = cluster.proxyUrl;
527+
agent = new HttpsProxyAgent(httpsProxyAgentOptions);
528+
} else if (cluster.server.startsWith('http')) {
529+
const httpProxyAgentOptions: HttpProxyAgentOptions = agentOptions as HttpProxyAgentOptions;
530+
httpProxyAgentOptions.proxy = cluster.proxyUrl;
531+
agent = new HttpProxyAgent(httpProxyAgentOptions);
532+
} else {
533+
throw new Error('Unsupported proxy type');
534+
}
535+
} else {
536+
agent = new https.Agent(agentOptions);
537+
}
538+
return agent;
539+
}
540+
534541
private applyHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): void {
535542
const cluster = this.getCurrentCluster();
536543
const user = this.getCurrentUser();

0 commit comments

Comments
 (0)