Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add podman extension api package and use it in podman extension #8891

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions extensions/podman/packages/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@podman-desktop/podman-extension-api",
"version": "0.0.1",
"description": "API for Podman Desktop Podman extension",
"publishConfig": {
"access": "public"
},
"license": "Apache-2.0",
"types": "./src/podman-extension-api.d.ts",
"files": [
"src"
],
"scripts": {
"prepare": "",
"clean": "rimraf lib *.tsbuildinfo",
"build": "",
"watch": "",
"publish:next": "pnpm publish --registry=https://registry.npmjs.org/ --no-git-tag-version --new-version 0.0.1-\"$(date +%s)\""
},
"dependencies": {
"@podman-desktop/api": "workspace:*"
}
}
27 changes: 27 additions & 0 deletions extensions/podman/packages/api/src/podman-extension-api.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**********************************************************************
* Copyright (C) 2024 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 { ProviderContainerConnection, RunOptions, RunResult } from '@podman-desktop/api';

export interface PodmanRunOptions extends RunOptions {
connection?: ProviderContainerConnection;
}

export interface PodmanExtensionApi {
exec(args: string[], options?: PodmanRunOptions): Promise<RunResult>;
}
13 changes: 13 additions & 0 deletions extensions/podman/packages/api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@tsconfig/strictest/tsconfig.json",
"compilerOptions": {
dgolovin marked this conversation as resolved.
Show resolved Hide resolved
"target": "esnext",
"module": "node16",
"baseUrl": ".",
"types": ["node"],
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*.d.ts"]
}
41 changes: 41 additions & 0 deletions extensions/podman/packages/api/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**********************************************************************
* Copyright (C) 2024 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
***********************************************************************/
/* eslint-env node */
import { join } from 'path';
import { defineConfig } from 'vite';

const PACKAGE_ROOT = __dirname;

// https://vitejs.dev/config/
export default defineConfig({
mode: process.env.MODE,
root: PACKAGE_ROOT,
base: '',
server: {
fs: {
strict: true,
},
},
build: {
sourcemap: true,
outDir: 'dist',
assetsDir: '.',
emptyOutDir: true,
reportCompressedSize: false,
},
});
34 changes: 34 additions & 0 deletions extensions/podman/packages/extension/src/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import * as extension from './extension';
import type { InstalledPodman } from './podman-cli';
import * as podmanCli from './podman-cli';
import { PodmanConfiguration } from './podman-configuration';
import type { UpdateCheck } from './podman-install';
import { PodmanInstall } from './podman-install';
import { getAssetsFolder, isLinux, isMac, isWindows, LIBKRUN_LABEL, LoggerDelegator, VMTYPE } from './util';

Expand Down Expand Up @@ -181,9 +182,24 @@ vi.mock('@podman-desktop/api', async () => {
provider: {
onDidRegisterContainerConnection: vi.fn(),
onDidUnregisterContainerConnection: vi.fn(),
createProvider: (): extensionApi.Provider =>
({
updateWarnings: vi.fn(),
onDidUpdateVersion: vi.fn(),
registerAutostart: vi.fn(),
}) as unknown as extensionApi.Provider,
},
registry: {
registerRegistryProvider: vi.fn(),
onDidRegisterRegistry: vi.fn(),
onDidUnregisterRegistry: vi.fn(),
onDidUpdateRegistry: vi.fn(),
},
proxy: {
isEnabled: (): boolean => false,
onDidUpdateProxy: vi.fn(),
onDidStateChange: vi.fn(),
getProxySettings: vi.fn(),
},
window: {
showErrorMessage: vi.fn(),
Expand Down Expand Up @@ -2285,3 +2301,21 @@ test('if a machine stopped is successfully reporting an error in telemetry', asy

expect(spyExecPromise).toBeCalledWith(podmanCli.getPodmanCli(), ['machine', 'stop', 'name'], expect.anything());
});

test('activate function returns an api implementation', async () => {
vi.spyOn(PodmanInstall.prototype, 'checkForUpdate').mockResolvedValue({
hasUpdate: false,
} as unknown as UpdateCheck);
const contextMock = {
subscriptions: [],
secrets: {
delete: vi.fn(),
get: vi.fn(),
onDidChange: vi.fn(),
store: vi.fn(),
},
} as unknown as extensionApi.ExtensionContext;
const api = await extension.activate(contextMock);
expect(api).toBeDefined();
expect(typeof api.exec).toBe('function');
});
11 changes: 10 additions & 1 deletion extensions/podman/packages/extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { ContainerEngineInfo, RunError } from '@podman-desktop/api';
import * as extensionApi from '@podman-desktop/api';
import { compareVersions } from 'compare-versions';

import type { PodmanExtensionApi, PodmanRunOptions } from '../../api/src/podman-extension-api';
import { getSocketCompatibility } from './compatibility-mode';
import { getDetectionChecks } from './detection-checks';
import { KrunkitHelper } from './krunkit-helper';
Expand Down Expand Up @@ -1246,7 +1247,11 @@ export function registerOnboardingRemoveUnsupportedMachinesCommand(): extensionA
});
}

export async function activate(extensionContext: extensionApi.ExtensionContext): Promise<void> {
async function exec(args: string[], options?: PodmanRunOptions): Promise<extensionApi.RunResult> {
return execPodman(args, options?.connection?.connection.vmTypeDisplayName, options);
}

export async function activate(extensionContext: extensionApi.ExtensionContext): Promise<PodmanExtensionApi> {
initExtensionContext(extensionContext);

initTelemetryLogger();
Expand Down Expand Up @@ -1658,6 +1663,10 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):

const podmanRemoteConnections = new PodmanRemoteConnections(extensionContext, provider);
podmanRemoteConnections.start();

return {
exec,
};
}

export async function calcPodmanMachineSetting(podmanConfiguration: PodmanConfiguration): Promise<void> {
Expand Down
7 changes: 6 additions & 1 deletion extensions/podman/packages/extension/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ const config = {
formats: ['cjs'],
},
rollupOptions: {
external: ['@podman-desktop/api', 'ssh2', ...builtinModules.flatMap(p => [p, `node:${p}`])],
external: [
'@podman-desktop/api',
'ssh2',
'@podman-desktop/podman-extension-api',
...builtinModules.flatMap(p => [p, `node:${p}`]),
],
output: {
entryFileNames: '[name].cjs',
},
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading