Skip to content

Commit ac8f465

Browse files
committed
chore: check for kubeconfig file on startup before watcher events
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
1 parent 39f4775 commit ac8f465

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

packages/main/src/plugin/kubernetes/kubernetes-client.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import type { PodCreationSource, ScalableControllerType } from './kubernetes-cli
6161
import { KubernetesClient } from './kubernetes-client.js';
6262
import { ResizableTerminalWriter } from './kubernetes-exec-transmitter.js';
6363

64+
vi.mock(import('node:fs'));
65+
6466
const _onDidChangeConfiguration = new Emitter<IConfigurationChangeEvent>();
6567
const configurationRegistry: ConfigurationRegistry = {
6668
onDidChangeConfiguration: _onDidChangeConfiguration.event,
@@ -1122,6 +1124,7 @@ test('Expect applyResourcesFromYAML to correctly call applyResources after loadi
11221124
});
11231125

11241126
test('setupWatcher sends kubernetes-context-update when kubeconfig file changes', async () => {
1127+
vi.mocked(fs.existsSync).mockReturnValue(true);
11251128
const client = createTestClient();
11261129
const fileSystemMonitoringSpy = vi.spyOn(fileSystemMonitoring, 'createFileSystemWatcher');
11271130
const onDidChangeMock = vi.fn();

packages/main/src/plugin/kubernetes/kubernetes-client.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ export class KubernetesClient {
261261
if (!val?.trim()) {
262262
val = defaultKubeconfigPath;
263263
}
264-
this.setupWatcher(val);
265264
await this.setKubeconfig(Uri.file(val));
265+
this.setupWatcher(val);
266266
}
267267
});
268268
}
@@ -278,6 +278,17 @@ export class KubernetesClient {
278278
this.kubeConfigWatcher = this.fileSystemMonitoring.createFileSystemWatcher(kubeconfigFile);
279279

280280
const location = Uri.file(kubeconfigFile);
281+
const notifyKubeConfigExist = async (): Promise<void> => {
282+
await this.refresh();
283+
this._onDidUpdateKubeconfig.fire({ type: 'CREATE', location });
284+
this.apiSender.send('kubernetes-context-update');
285+
};
286+
287+
if (fs.existsSync(kubeconfigFile)) {
288+
notifyKubeConfigExist().catch((error: unknown) => {
289+
console.error('Error while checking kubeconfig', error);
290+
});
291+
}
281292

282293
// needs to refresh
283294
this.kubeConfigWatcher.onDidChange(async () => {
@@ -287,9 +298,7 @@ export class KubernetesClient {
287298
});
288299

289300
this.kubeConfigWatcher.onDidCreate(async () => {
290-
await this.refresh();
291-
this._onDidUpdateKubeconfig.fire({ type: 'CREATE', location });
292-
this.apiSender.send('kubernetes-context-update');
301+
await notifyKubeConfigExist();
293302
});
294303

295304
this.kubeConfigWatcher.onDidDelete(() => {

0 commit comments

Comments
 (0)