Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion __mocks__/@podman-desktop/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
***********************************************************************/
import { vi } from 'vitest';
import { EventEmitter } from 'node:events';
import { parse } from 'node:path';

/**
* Mock the extension API for vitest.
Expand Down Expand Up @@ -75,6 +74,10 @@ const plugin = {
process: {
exec: vi.fn(),
},
kubernetes: {
onDidUpdateKubeconfig: vi.fn(),
getKubeconfig: vi.fn(),
}
};

module.exports = plugin;
2 changes: 2 additions & 0 deletions packages/extension/__mocks__/fs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { fs } = require('memfs')
module.exports = fs
2 changes: 2 additions & 0 deletions packages/extension/__mocks__/fs/promises.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { fs } = require('memfs')
module.exports = fs.promises
1 change: 1 addition & 0 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"eslint-plugin-no-null": "^1.0.2",
"eslint-plugin-redundant-undefined": "^1.0.0",
"eslint-plugin-sonarjs": "^3.0.2",
"memfs": "^4.17.2",
"prettier": "^3.6.1",
"typescript": "5.8.3",
"vite": "^7.0",
Expand Down
81 changes: 70 additions & 11 deletions packages/extension/src/dashboard-extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,101 @@
***********************************************************************/

import type { WebviewPanel, ExtensionContext } from '@podman-desktop/api';
import { Uri, window } from '@podman-desktop/api';
import { beforeEach, test, vi } from 'vitest';
import { kubernetes, Uri, window } from '@podman-desktop/api';
import { assert, beforeEach, describe, expect, test, vi } from 'vitest';
import { DashboardExtension } from './dashboard-extension';
import { vol } from 'memfs';

import { readFile } from 'node:fs/promises';
import type { ContextsManager } from './manager/contexts-manager';
import type { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher';

let extensionContextMock: ExtensionContext;
let dashboardExtension: DashboardExtension;
let contextsManagerMock: ContextsManager;
let contextsStatesDispatcher: ContextsStatesDispatcher;

vi.mock(import('node:fs'));
vi.mock(import('node:fs/promises'));
vi.mock(import('@kubernetes/client-node'));

beforeEach(() => {
vi.restoreAllMocks();
vi.resetAllMocks();
vol.reset();

vi.mocked(window.createWebviewPanel).mockReturnValue({
webview: {
html: '',
onDidReceiveMessage: vi.fn(),
},
} as unknown as WebviewPanel);
vi.mocked(Uri.joinPath).mockReturnValue({ fsPath: '' } as unknown as Uri);
vi.mocked(readFile).mockResolvedValue('<html></html>');
vi.mocked(Uri.joinPath).mockReturnValue({ fsPath: '/path/to/extension/index.html' } as unknown as Uri);
// Create a mock for the ExtensionContext
extensionContextMock = {
subscriptions: [],
} as unknown as ExtensionContext;
dashboardExtension = new DashboardExtension(extensionContextMock);
// Create a mock for the contextsManager
contextsManagerMock = {
update: vi.fn(),
} as unknown as ContextsManager;
contextsStatesDispatcher = {
init: vi.fn(),
} as unknown as ContextsStatesDispatcher;
dashboardExtension = new DashboardExtension(extensionContextMock, contextsManagerMock, contextsStatesDispatcher);
vi.mocked(kubernetes.getKubeconfig).mockReturnValue({
path: '/path/to/kube/config',
} as Uri);
});

test('should activate correctly', async () => {
await dashboardExtension.activate();
describe('a kubeconfig file is not present', () => {
beforeEach(() => {
vol.fromJSON({
'/path/to/extension/index.html': '<html></html>',
});
});

test('should activate correctly and calls contextsManager every time the kubeconfig file changes', async () => {
await dashboardExtension.activate();
expect(contextsManagerMock.update).not.toHaveBeenCalled();

const callback = vi.mocked(kubernetes.onDidUpdateKubeconfig).mock.lastCall?.[0];
assert(callback);
vi.mocked(contextsManagerMock.update).mockClear();
callback({ type: 'UPDATE', location: { path: '/path/to/kube/config' } as Uri });
expect(contextsManagerMock.update).toHaveBeenCalledOnce();

expect(contextsStatesDispatcher.init).toHaveBeenCalledOnce();
});

test('should deactivate correctly', async () => {
await dashboardExtension.activate();
await dashboardExtension.deactivate();
});
});

test('should deactivate correctly', async () => {
await dashboardExtension.activate();
await dashboardExtension.deactivate();
describe('a kubeconfig file is present', () => {
beforeEach(() => {
vol.fromJSON({
'/path/to/extension/index.html': '<html></html>',
'/path/to/kube/config': '{}',
});
});

test('should activate correctly and calls contextsManager every time the kubeconfig file changes', async () => {
await dashboardExtension.activate();
expect(contextsManagerMock.update).toHaveBeenCalledOnce();

const callback = vi.mocked(kubernetes.onDidUpdateKubeconfig).mock.lastCall?.[0];
assert(callback);
vi.mocked(contextsManagerMock.update).mockClear();
callback({ type: 'UPDATE', location: { path: '/path/to/kube/config' } as Uri });
expect(contextsManagerMock.update).toHaveBeenCalledOnce();

expect(contextsStatesDispatcher.init).toHaveBeenCalledOnce();
});

test('should deactivate correctly', async () => {
await dashboardExtension.activate();
await dashboardExtension.deactivate();
});
});
77 changes: 65 additions & 12 deletions packages/extension/src/dashboard-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,55 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { Uri, window, type ExtensionContext } from '@podman-desktop/api';
import type { WebviewPanel, ExtensionContext, KubeconfigUpdateEvent } from '@podman-desktop/api';
import { kubernetes, Uri, window } from '@podman-desktop/api';

import { RpcExtension } from '/@common/rpc/rpc';

import { readFile } from 'node:fs/promises';
import type { ContextsManager } from './manager/contexts-manager';
import { existsSync } from 'node:fs';
import { KubeConfig } from '@kubernetes/client-node';
import type { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher';

export class DashboardExtension {
#extensionContext: ExtensionContext;

constructor(readonly extensionContext: ExtensionContext) {
#contextsManager: ContextsManager;
#contextsStatesDispatcher: ContextsStatesDispatcher;

constructor(
readonly extensionContext: ExtensionContext,
readonly contextManager: ContextsManager,
readonly contextsStatesDispatcher: ContextsStatesDispatcher,
) {
this.#extensionContext = extensionContext;
this.#contextsManager = contextManager;
this.#contextsStatesDispatcher = contextsStatesDispatcher;
}

async activate(): Promise<void> {
const panel = await this.createWebview();

// Register webview communication for this webview
const rpcExtension = new RpcExtension(panel.webview);
rpcExtension.init();
this.#extensionContext.subscriptions.push(rpcExtension);

const now = performance.now();

const afterFirst = performance.now();

console.log('activation time:', afterFirst - now);

await this.listenMonitoring();
await this.startMonitoring();
}

async deactivate(): Promise<void> {
console.log('deactivating Kubernetes Dashboard extension');
}

private async createWebview(): Promise<WebviewPanel> {
const panel = window.createWebviewPanel('kubernetes-dashboard', 'Kubernetes', {
localResourceRoots: [Uri.joinPath(this.#extensionContext.extensionUri, 'media')],
});
Expand Down Expand Up @@ -70,19 +105,37 @@ export class DashboardExtension {
// Update the webview panel with the new index.html file with corrected links.
panel.webview.html = indexHtml;

// Register webview communication for this webview
const rpcExtension = new RpcExtension(panel.webview);
rpcExtension.init();
this.#extensionContext.subscriptions.push(rpcExtension);
return panel;
}

const now = performance.now();
private async listenMonitoring(): Promise<void> {
this.#contextsStatesDispatcher.init();
}

const afterFirst = performance.now();
private async startMonitoring(): Promise<void> {
this.#extensionContext.subscriptions.push(this.#contextsManager);

console.log('activation time:', afterFirst - now);
const kubeconfigWatcher = kubernetes.onDidUpdateKubeconfig(this.onKubeconfigUpdate.bind(this));
this.#extensionContext.subscriptions.push(kubeconfigWatcher);

// initial state is not sent by watcher, let's get it explicitely
const kubeconfig = kubernetes.getKubeconfig();
if (existsSync(kubeconfig.path)) {
await this.onKubeconfigUpdate({
location: kubeconfig,
type: 'CREATE',
});
}
}

async deactivate(): Promise<void> {
console.log('deactivating Kubernetes Dashboard extension');
private async onKubeconfigUpdate(event: KubeconfigUpdateEvent): Promise<void> {
if (event.type === 'DELETE') {
// update with an empty KubeConfig
await this.#contextsManager.update(new KubeConfig());
return;
}
const kubeConfig = new KubeConfig();
kubeConfig.loadFromFile(event.location.path);
await this.#contextsManager.update(kubeConfig);
}
}
11 changes: 10 additions & 1 deletion packages/extension/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@
import type { ExtensionContext } from '@podman-desktop/api';

import { DashboardExtension } from './dashboard-extension';
import { ContextsManager } from './manager/contexts-manager';
import { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher';

let dashboardExtension: DashboardExtension | undefined;

// Initialize the activation of the extension.
export async function activate(extensionContext: ExtensionContext): Promise<void> {
dashboardExtension ??= new DashboardExtension(extensionContext);
const contextsManager = new ContextsManager();
const apiSender = {
send: (channel: string, data?: unknown): void => {
console.log(`==> recv data "${data}" on channel ${channel}`);
},
};
const contextsStatesDispatcher = new ContextsStatesDispatcher(contextsManager, apiSender);
dashboardExtension ??= new DashboardExtension(extensionContext, contextsManager, contextsStatesDispatcher);

await dashboardExtension.activate();
}
Expand Down
Loading