Skip to content

Commit

Permalink
fix: handle kubernetes connection in startProvider function (#8629)
Browse files Browse the repository at this point in the history
Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi authored Aug 30, 2024
1 parent 13577e1 commit 2b320e5
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 17 deletions.
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

0 comments on commit 2b320e5

Please sign in to comment.