Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Mar 4, 2025
1 parent 30460bc commit 3b44c17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
15 changes: 4 additions & 11 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,13 @@ export class KubeConfig implements SecurityAuthentication {
}
if (platform === 'win32') {
try {
const envKubeconfigPathResult = this.spawnSync('wsl.exe', [
const envKubeconfigPathResult = child_process.spawnSync('wsl.exe', [
'bash',
'-c',
'printenv KUBECONFIG',
]);
if (envKubeconfigPathResult.status === 0 && envKubeconfigPathResult.stdout.length > 0) {
const result = this.spawnSync('wsl.exe', [
const result = child_process.spawnSync('wsl.exe', [
'cat',
envKubeconfigPathResult.stdout.toString('utf8'),
]);
Expand All @@ -438,10 +438,10 @@ export class KubeConfig implements SecurityAuthentication {
// Falling back to default kubeconfig
}
try {
const configResult = this.spawnSync('wsl.exe', ['cat', '~/.kube/config']);
const configResult = child_process.spawnSync('wsl.exe', ['cat', '~/.kube/config']);
if (configResult.status === 0) {
this.loadFromString(configResult.stdout.toString('utf8'), opts);
const result = this.spawnSync('wsl.exe', ['wslpath', '-w', '~/.kube']);
const result = child_process.spawnSync('wsl.exe', ['wslpath', '-w', '~/.kube']);
if (result.status === 0) {
this.makePathsAbsolute(result.stdout.toString('utf8'));
}
Expand Down Expand Up @@ -597,13 +597,6 @@ export class KubeConfig implements SecurityAuthentication {
this.applyHTTPSOptions(opts);
await this.applyAuthorizationHeader(opts);
}

private spawnSync(
command: string,
args: string[],
): { status: number | null; stdout: Buffer; stderr: Buffer } {
return child_process.spawnSync(command, args);
}
}

export type ApiConstructor<T extends ApiType> = new (config: Configuration) => T;
Expand Down
15 changes: 8 additions & 7 deletions src/config_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { deepEqual, deepStrictEqual, notStrictEqual, rejects, strictEqual, throws } from 'node:assert';
import child_process from 'node:child_process';
import { readFileSync } from 'node:fs';
import https from 'node:https';
import { Agent, RequestOptions } from 'node:https';
import path, { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { mock } from 'node:test';

import mockfs from 'mock-fs';

Expand Down Expand Up @@ -324,7 +326,6 @@ describe('KubeConfig', () => {
};

assertRequestOptionsEqual(opts, expectedOptions);
console.log(requestContext.getAgent());
strictEqual((requestContext.getAgent()! as any).options.servername, 'kube.example2.com');
});
it('should apply cert configs', async () => {
Expand Down Expand Up @@ -1637,10 +1638,10 @@ describe('KubeConfig', () => {
it('should try to load from WSL on Windows with wsl.exe not working', () => {
const kc = new KubeConfig();
const commands: { command: string; args: string[] }[] = [];
(kc as any).spawnSync = (cmd: string, args: string[]) => {
mock.method(child_process, 'spawnSync', (cmd: string, args: string[]) => {
commands.push({ command: cmd, args });
return { status: 1, stderr: 'some error' };
};
});
kc.loadFromDefault(undefined, false, 'win32');
strictEqual(commands.length, 2);
for (let i = 0; i < commands.length; i++) {
Expand All @@ -1657,10 +1658,10 @@ describe('KubeConfig', () => {
{ status: 0, stderr: '', stdout: configData.toString() },
];
let ix = 0;
(kc as any).spawnSync = (cmd: string, args: string[]) => {
mock.method(child_process, 'spawnSync', (cmd: string, args: string[]) => {
commands.push({ command: cmd, args });
return results[ix++];
};
});
kc.loadFromDefault(undefined, false, 'win32');
strictEqual(commands.length, 2);
for (let i = 0; i < commands.length; i++) {
Expand All @@ -1678,10 +1679,10 @@ describe('KubeConfig', () => {
{ status: 0, stderr: '', stdout: 'C:\\wsldata\\.kube' },
];
let ix = 0;
(kc as any).spawnSync = (cmd: string, args: string[]) => {
mock.method(child_process, 'spawnSync', (cmd: string, args: string[]) => {
commands.push({ command: cmd, args });
return results[ix++];
};
});
kc.loadFromDefault(undefined, false, 'win32');
strictEqual(commands.length, 3);
for (let i = 0; i < commands.length; i++) {
Expand Down

0 comments on commit 3b44c17

Please sign in to comment.