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: adding BuildOption #5533

Merged
merged 6 commits into from Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/extension-api/src/extension-api.d.ts
Expand Up @@ -1919,6 +1919,14 @@ declare module '@podman-desktop/api' {
id: string;
}

export interface BuildImageOptions {
containerFile?: string;
target?: string;
platform?: string;
provider?: ProviderContainerConnectionInfo | containerDesktopAPI.ContainerProviderConnection;
abortController?: AbortController;
}

export interface NetworkCreateOptions {
Name: string;
}
Expand Down Expand Up @@ -1953,13 +1961,9 @@ declare module '@podman-desktop/api' {
export function stopContainer(engineId: string, id: string): Promise<void>;
export function deleteContainer(engineId: string, id: string): Promise<void>;
export function buildImage(
containerBuildContextDirectory: string,
relativeContainerfilePath: string,
imageName: string,
platform: string,
selectedProvider: ProviderContainerConnectionInfo | containerDesktopAPI.ContainerProviderConnection,
context: string,
axel7083 marked this conversation as resolved.
Show resolved Hide resolved
axel7083 marked this conversation as resolved.
Show resolved Hide resolved
eventCollect: (eventName: 'stream' | 'error' | 'finish', data: string) => void,
abortController?: AbortController,
option?: BuildImageOptions,
axel7083 marked this conversation as resolved.
Show resolved Hide resolved
);
export function saveImage(engineId: string, id: string, filename: string): Promise<void>;
export function listImages(): Promise<ImageInfo[]>;
Expand Down
25 changes: 18 additions & 7 deletions packages/main/src/plugin/container-registry.spec.ts
Expand Up @@ -981,11 +981,22 @@ describe('buildImage', () => {
lifecycleMethods: undefined,
status: 'started',
};
await expect(containerRegistry.buildImage('context', 'file', 'name', '', connection, () => {})).rejects.toThrow(
await expect(containerRegistry.buildImage('context', () => {}, 'file', 'name', '', connection)).rejects.toThrow(
'no running provider for the matching container',
);
});

test('called getFirstRunningConnection when undefined provider', async () => {
const getFirstRunningConnection = vi.spyOn(containerRegistry, 'getFirstRunningConnection');
getFirstRunningConnection.mockImplementation(() => {
throw new Error('mocked');
});

await expect(containerRegistry.buildImage('context', () => {})).rejects.toThrow('mocked');

expect(getFirstRunningConnection).toHaveBeenCalledOnce();
});

test('throw if there is no running provider with containerProviderConnection input', async () => {
const fakeDockerode = {} as Dockerode;

Expand All @@ -1009,7 +1020,7 @@ describe('buildImage', () => {
},
status: () => 'started',
};
await expect(containerRegistry.buildImage('context', 'file', 'name', '', connection, () => {})).rejects.toThrow(
await expect(containerRegistry.buildImage('context', () => {}, 'file', 'name', '', connection)).rejects.toThrow(
'no running provider for the matching container',
);
});
Expand Down Expand Up @@ -1046,7 +1057,7 @@ describe('buildImage', () => {
vi.spyOn(tar, 'pack').mockReturnValue({} as NodeJS.ReadableStream);
vi.spyOn(dockerAPI, 'buildImage').mockRejectedValue('human error message');

await expect(containerRegistry.buildImage('context', 'file', 'name', '', connection, () => {})).rejects.toThrow(
await expect(containerRegistry.buildImage('context', () => {}, 'file', 'name', '', connection)).rejects.toThrow(
'human error message',
);
});
Expand Down Expand Up @@ -1082,7 +1093,7 @@ describe('buildImage', () => {
vi.spyOn(tar, 'pack').mockReturnValue({} as NodeJS.ReadableStream);
vi.spyOn(dockerAPI, 'buildImage').mockRejectedValue('human error message');

await expect(containerRegistry.buildImage('context', 'file', 'name', '', connection, () => {})).rejects.toThrow(
await expect(containerRegistry.buildImage('context', () => {}, 'file', 'name', '', connection)).rejects.toThrow(
'human error message',
);
});
Expand Down Expand Up @@ -1122,7 +1133,7 @@ describe('buildImage', () => {
return f(null, []);
});

await containerRegistry.buildImage('context', '\\path\\file', 'name', '', connection, () => {});
await containerRegistry.buildImage('context', () => {}, '\\path\\file', 'name', '', connection);

expect(dockerAPI.buildImage).toBeCalledWith({} as NodeJS.ReadableStream, {
registryconfig: {},
Expand Down Expand Up @@ -1166,7 +1177,7 @@ describe('buildImage', () => {
return f(null, []);
});

await containerRegistry.buildImage('context', '\\path\\file', 'name', '', connection, () => {});
await containerRegistry.buildImage('context', () => {}, '\\path\\file', 'name', '', connection);

expect(dockerAPI.buildImage).toBeCalledWith({} as NodeJS.ReadableStream, {
registryconfig: {},
Expand Down Expand Up @@ -1211,7 +1222,7 @@ describe('buildImage', () => {
return f(null, []);
});

await containerRegistry.buildImage('context', '/dir/dockerfile', 'name', '', connection, () => {});
await containerRegistry.buildImage('context', () => {}, '/dir/dockerfile', 'name', '', connection);

expect(dockerAPI.buildImage).toBeCalledWith({} as NodeJS.ReadableStream, {
registryconfig: {},
Expand Down
20 changes: 13 additions & 7 deletions packages/main/src/plugin/container-registry.ts
Expand Up @@ -1946,17 +1946,23 @@ export class ContainerProviderRegistry {

async buildImage(
containerBuildContextDirectory: string,
relativeContainerfilePath: string,
imageName: string,
platform: string,
selectedProvider: ProviderContainerConnectionInfo | containerDesktopAPI.ContainerProviderConnection,
eventCollect: (eventName: 'stream' | 'error' | 'finish', data: string) => void,
relativeContainerfilePath?: string,
imageName?: string,
platform?: string,
selectedProvider?: ProviderContainerConnectionInfo | containerDesktopAPI.ContainerProviderConnection,
abortController?: AbortController,
): Promise<unknown> {
let telemetryOptions = {};
try {
// grab all connections
const matchingContainerProviderApi = this.getMatchingEngineFromConnection(selectedProvider);
let matchingContainerProviderApi: Dockerode;
if (selectedProvider !== undefined) {
// grab all connections
matchingContainerProviderApi = this.getMatchingEngineFromConnection(selectedProvider);
} else {
// Get the first running connection (preference for podman)
matchingContainerProviderApi = this.getFirstRunningConnection()[1];
}

// grab auth for all registries
const registryconfig = this.imageRegistry.getRegistryConfig();
Expand All @@ -1965,7 +1971,7 @@ export class ContainerProviderRegistry {
`Uploading the build context from ${containerBuildContextDirectory}...Can take a while...\r\n`,
);
const tarStream = tar.pack(containerBuildContextDirectory);
if (isWindows()) {
if (isWindows() && relativeContainerfilePath !== undefined) {
relativeContainerfilePath = relativeContainerfilePath.replace(/\\/g, '/');
}

Expand Down
20 changes: 8 additions & 12 deletions packages/main/src/plugin/extension-loader.ts
Expand Up @@ -907,22 +907,18 @@ export class ExtensionLoader {
return containerProviderRegistry.deleteContainer(engineId, id);
},
buildImage(
containerBuildContextDirectory: string,
relativeContainerfilePath: string,
imageName: string,
platform: string,
selectedProvider: ProviderContainerConnectionInfo | containerDesktopAPI.ContainerProviderConnection,
context: string,
eventCollect: (eventName: 'stream' | 'error' | 'finish', data: string) => void,
abortController?: AbortController,
option?: containerDesktopAPI.BuildImageOptions,
) {
return containerProviderRegistry.buildImage(
containerBuildContextDirectory,
relativeContainerfilePath,
imageName,
platform,
selectedProvider,
context,
eventCollect,
abortController,
option?.containerFile,
option?.target,
option?.platform,
option?.provider,
option?.abortController,
);
},
listImages(): Promise<containerDesktopAPI.ImageInfo[]> {
Expand Down
8 changes: 4 additions & 4 deletions packages/main/src/plugin/index.ts
Expand Up @@ -1207,10 +1207,6 @@ export class PluginSystem {

return containerProviderRegistry.buildImage(
containerBuildContextDirectory,
relativeContainerfilePath,
imageName,
platform,
selectedProvider,
(eventName: string, data: string) => {
this.getWebContentsSender().send(
'container-provider-registry:buildImage-onData',
Expand All @@ -1219,6 +1215,10 @@ export class PluginSystem {
data,
);
},
relativeContainerfilePath,
imageName,
platform,
selectedProvider,
abortController,
);
},
Expand Down