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
33 changes: 30 additions & 3 deletions packages/common/src/channels.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import { type DashboardApi } from './interface/dashboard-api';
/**********************************************************************
* Copyright (C) 2025 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import type { ActiveResourcesCountInfo } from './model/active-resources-count-info';
import type { ContextsHealthsInfo } from './model/contexts-healths-info';
import type { ContextsPermissionsInfo } from './model/contexts-permissions-info';
import type { ResourcesCountInfo } from './model/resources-count-info';
import type { UpdateResourceInfo } from './model/update-resource-info';

import { createRpcChannel } from './rpc';

// RPC channels (used by the webview to send requests to the extension)
export const API_DASHBOARD = createRpcChannel<DashboardApi>('DashboardApi');
// Broadcast events (sent by extension and received by the webview)
export const RESOURCES_COUNT = createRpcChannel<ResourcesCountInfo>('ResourcesCount');
export const ACTIVE_RESOURCES_COUNT = createRpcChannel<ActiveResourcesCountInfo>('ActiveResourcesCount');
export const CONTEXTS_HEALTHS = createRpcChannel<ContextsHealthsInfo>('ContextsHealths');
export const CONTEXTS_PERMISSIONS = createRpcChannel<ContextsPermissionsInfo>('ContextsPermissions');
export const UPDATE_RESOURCE = createRpcChannel<UpdateResourceInfo>('UpdateResource');
23 changes: 23 additions & 0 deletions packages/common/src/model/active-resources-count-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**********************************************************************
* Copyright (C) 2025 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import type { ResourceCount } from './kubernetes-resource-count';

export interface ActiveResourcesCountInfo {
counts: ResourceCount[];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (C) 2022-2024 Red Hat, Inc.
* Copyright (C) 2025 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

export type ApiSenderType = {
send: (channel: string, data?: unknown) => void;
};
import type { ContextHealth } from './kubernetes-contexts-healths';

export interface ContextsHealthsInfo {
healths: ContextHealth[];
}
23 changes: 23 additions & 0 deletions packages/common/src/model/contexts-permissions-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**********************************************************************
* Copyright (C) 2025 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import type { ContextPermission } from './kubernetes-contexts-permissions';

export interface ContextsPermissionsInfo {
permissions: ContextPermission[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

export const DashboardApi = Symbol.for('DashboardApi');
export interface DashboardApi {
ping(): Promise<string>;
import type { ResourceCount } from './kubernetes-resource-count';

export interface ResourcesCountInfo {
counts: ResourceCount[];
}
25 changes: 25 additions & 0 deletions packages/common/src/model/update-resource-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**********************************************************************
* Copyright (C) 2025 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import type { KubernetesContextResources } from './kubernetes-resources';

export interface UpdateResourceInfo {
contextName: string;
resourceName: string;
resources: KubernetesContextResources[];
}
21 changes: 14 additions & 7 deletions packages/extension/src/dashboard-extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ import { assert, beforeEach, describe, expect, test, vi } from 'vitest';
import { DashboardExtension } from './dashboard-extension';
import { vol } from 'memfs';

import type { ContextsManager } from './manager/contexts-manager';
import type { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher';
import { ContextsManager } from './manager/contexts-manager';
import { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher';

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

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

beforeEach(() => {
vi.restoreAllMocks();
Expand All @@ -50,14 +52,19 @@ beforeEach(() => {
extensionContextMock = {
subscriptions: [],
} as unknown as ExtensionContext;

// Create a mock for the contextsManager
contextsManagerMock = {
update: vi.fn(),
} as unknown as ContextsManager;
contextsStatesDispatcher = {
vi.mocked(ContextsManager).mockReturnValue(contextsManagerMock);

contextsStatesDispatcherMock = {
init: vi.fn(),
} as unknown as ContextsStatesDispatcher;
dashboardExtension = new DashboardExtension(extensionContextMock, contextsManagerMock, contextsStatesDispatcher);
vi.mocked(ContextsStatesDispatcher).mockReturnValue(contextsStatesDispatcherMock);

dashboardExtension = new DashboardExtension(extensionContextMock);
vi.mocked(kubernetes.getKubeconfig).mockReturnValue({
path: '/path/to/kube/config',
} as Uri);
Expand All @@ -80,7 +87,7 @@ describe('a kubeconfig file is not present', () => {
callback({ type: 'UPDATE', location: { path: '/path/to/kube/config' } as Uri });
expect(contextsManagerMock.update).toHaveBeenCalledOnce();

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

test('should deactivate correctly', async () => {
Expand All @@ -107,7 +114,7 @@ describe('a kubeconfig file is present', () => {
callback({ type: 'UPDATE', location: { path: '/path/to/kube/config' } as Uri });
expect(contextsManagerMock.update).toHaveBeenCalledOnce();

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

test('should deactivate correctly', async () => {
Expand Down
15 changes: 6 additions & 9 deletions packages/extension/src/dashboard-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,18 @@ 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 { ContextsManager } from './manager/contexts-manager';
import { existsSync } from 'node:fs';
import { KubeConfig } from '@kubernetes/client-node';
import type { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher';
import { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher';

export class DashboardExtension {
#extensionContext: ExtensionContext;
#contextsManager: ContextsManager;
#contextsStatesDispatcher: ContextsStatesDispatcher;

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

async activate(): Promise<void> {
Expand All @@ -50,6 +44,9 @@ export class DashboardExtension {
rpcExtension.init();
this.#extensionContext.subscriptions.push(rpcExtension);

this.#contextsManager = new ContextsManager();
this.#contextsStatesDispatcher = new ContextsStatesDispatcher(this.#contextsManager, rpcExtension);

const now = performance.now();

const afterFirst = performance.now();
Expand Down
11 changes: 1 addition & 10 deletions packages/extension/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,12 @@
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> {
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);
dashboardExtension ??= new DashboardExtension(extensionContext);

await dashboardExtension.activate();
}
Expand Down
Loading