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

fix: handle kubernetes connection in startProvider function #8629

Merged
merged 1 commit into from
Aug 30, 2024
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
97 changes: 97 additions & 0 deletions packages/main/src/plugin/provider-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
KubernetesProviderConnection,
ProviderCleanup,
ProviderInstallation,
ProviderLifecycle,
ProviderUpdate,
} from '@podman-desktop/api';
import { beforeEach, describe, expect, test, vi } from 'vitest';
Expand Down Expand Up @@ -1000,3 +1001,99 @@ describe('runPreflightChecks', () => {
expect(run).toBeFalsy();
});
});

describe('startProvider', () => {
test('if providerLifecycles is registered for the provider call startProviderLifecycle', async () => {
const provider = providerRegistry.createProvider('id', 'name', {
id: 'internal',
name: 'internal',
status: 'installed',
});
const disposable = providerRegistry.registerLifecycle(provider as ProviderImpl, {} as ProviderLifecycle);
const startProviderLifecycleMock = vi
.spyOn(providerRegistry, 'startProviderLifecycle')
.mockImplementation(_id => Promise.resolve());
await providerRegistry.startProvider((provider as ProviderImpl).internalId);
expect(startProviderLifecycleMock).toBeCalledWith((provider as ProviderImpl).internalId);
disposable.dispose();
});

test('if the provider has no lifecycle and no connection, throw', async () => {
const provider = providerRegistry.createProvider('id', 'name', {
id: 'internal',
name: 'internal',
status: 'installed',
});
await expect(() => providerRegistry.startProvider((provider as ProviderImpl).internalId)).rejects.toThrowError(
'The provider does not have any connection to start',
);
});

test('if the provider has one connection without the start lifecycle, throw', async () => {
const provider = providerRegistry.createProvider('id', 'name', {
id: 'internal',
name: 'internal',
status: 'installed',
});
provider.registerContainerProviderConnection({
name: 'connection',
type: 'podman',
endpoint: {
socketPath: '/endpoint1.sock',
},
status() {
return 'stopped';
},
});
await expect(() => providerRegistry.startProvider((provider as ProviderImpl).internalId)).rejects.toThrowError(
'The connection connection does not support start lifecycle',
);
});

test('if the provider has one container connection with the start lifecycle, execute the start action', async () => {
const provider = providerRegistry.createProvider('id', 'name', {
id: 'internal',
name: 'internal',
status: 'installed',
});
const startMock = vi.fn();
provider.registerContainerProviderConnection({
name: 'connection',
type: 'podman',
lifecycle: {
start: startMock,
},
endpoint: {
socketPath: '/endpoint1.sock',
},
status() {
return 'stopped';
},
});
await providerRegistry.startProvider((provider as ProviderImpl).internalId);
expect(startMock).toBeCalled();
});

test('if the provider has one kubernetes connection with the start lifecycle, execute the start action', async () => {
const provider = providerRegistry.createProvider('id', 'name', {
id: 'internal',
name: 'internal',
status: 'installed',
});
const startMock = vi.fn();
provider.registerKubernetesProviderConnection({
name: 'connection',
lifecycle: {
start: startMock,
},
endpoint: {
apiURL: 'url',
},
status() {
return 'stopped';
},
});
await providerRegistry.startProvider((provider as ProviderImpl).internalId);
expect(startMock).toBeCalled();
});
});
37 changes: 20 additions & 17 deletions packages/main/src/plugin/provider-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,32 +556,35 @@ export class ProviderRegistry {

// start anything from the provider
async startProvider(providerInternalId: string): Promise<void> {
const provider = this.getMatchingProvider(providerInternalId);

// do we have a lifecycle attached to the provider ?
if (this.providerLifecycles.has(providerInternalId)) {
return this.startProviderLifecycle(providerInternalId);
}

const provider = this.getMatchingProvider(providerInternalId);
let connection: ContainerProviderConnection | KubernetesProviderConnection | undefined;

if (provider.containerConnections && provider.containerConnections.length > 0) {
const connection = provider.containerConnections[0];
const lifecycle = connection?.lifecycle;
if (!lifecycle?.start) {
throw new Error('The container connection does not support start lifecycle');
}
if (!connection) {
throw new Error('The provider does not have a container connection to start');
}
connection = provider.containerConnections[0];
} else if (provider.kubernetesConnections && provider.kubernetesConnections.length > 0) {
connection = provider.kubernetesConnections[0];
}

const context = this.connectionLifecycleContexts.get(connection);
if (!context) {
throw new Error('The connection does not have context to start');
}
if (!connection) {
throw new Error('The provider does not have any connection to start');
}

return lifecycle.start(context);
} else {
throw new Error('No container connection found for provider');
const lifecycle = connection.lifecycle;
if (!lifecycle?.start) {
throw new Error(`The connection ${connection.name} does not support start lifecycle`);
}

const context = this.connectionLifecycleContexts.get(connection);
if (!context) {
throw new Error(`The connection ${connection.name} does not have context to start`);
}

return lifecycle.start(context);
}

// Initialize the provider (if there is something to initialize)
Expand Down