Skip to content

Commit 2b320e5

Browse files
authored
fix: handle kubernetes connection in startProvider function (#8629)
Signed-off-by: lstocchi <[email protected]>
1 parent 13577e1 commit 2b320e5

File tree

2 files changed

+117
-17
lines changed

2 files changed

+117
-17
lines changed

packages/main/src/plugin/provider-registry.spec.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
KubernetesProviderConnection,
2626
ProviderCleanup,
2727
ProviderInstallation,
28+
ProviderLifecycle,
2829
ProviderUpdate,
2930
} from '@podman-desktop/api';
3031
import { beforeEach, describe, expect, test, vi } from 'vitest';
@@ -1000,3 +1001,99 @@ describe('runPreflightChecks', () => {
10001001
expect(run).toBeFalsy();
10011002
});
10021003
});
1004+
1005+
describe('startProvider', () => {
1006+
test('if providerLifecycles is registered for the provider call startProviderLifecycle', async () => {
1007+
const provider = providerRegistry.createProvider('id', 'name', {
1008+
id: 'internal',
1009+
name: 'internal',
1010+
status: 'installed',
1011+
});
1012+
const disposable = providerRegistry.registerLifecycle(provider as ProviderImpl, {} as ProviderLifecycle);
1013+
const startProviderLifecycleMock = vi
1014+
.spyOn(providerRegistry, 'startProviderLifecycle')
1015+
.mockImplementation(_id => Promise.resolve());
1016+
await providerRegistry.startProvider((provider as ProviderImpl).internalId);
1017+
expect(startProviderLifecycleMock).toBeCalledWith((provider as ProviderImpl).internalId);
1018+
disposable.dispose();
1019+
});
1020+
1021+
test('if the provider has no lifecycle and no connection, throw', async () => {
1022+
const provider = providerRegistry.createProvider('id', 'name', {
1023+
id: 'internal',
1024+
name: 'internal',
1025+
status: 'installed',
1026+
});
1027+
await expect(() => providerRegistry.startProvider((provider as ProviderImpl).internalId)).rejects.toThrowError(
1028+
'The provider does not have any connection to start',
1029+
);
1030+
});
1031+
1032+
test('if the provider has one connection without the start lifecycle, throw', async () => {
1033+
const provider = providerRegistry.createProvider('id', 'name', {
1034+
id: 'internal',
1035+
name: 'internal',
1036+
status: 'installed',
1037+
});
1038+
provider.registerContainerProviderConnection({
1039+
name: 'connection',
1040+
type: 'podman',
1041+
endpoint: {
1042+
socketPath: '/endpoint1.sock',
1043+
},
1044+
status() {
1045+
return 'stopped';
1046+
},
1047+
});
1048+
await expect(() => providerRegistry.startProvider((provider as ProviderImpl).internalId)).rejects.toThrowError(
1049+
'The connection connection does not support start lifecycle',
1050+
);
1051+
});
1052+
1053+
test('if the provider has one container connection with the start lifecycle, execute the start action', async () => {
1054+
const provider = providerRegistry.createProvider('id', 'name', {
1055+
id: 'internal',
1056+
name: 'internal',
1057+
status: 'installed',
1058+
});
1059+
const startMock = vi.fn();
1060+
provider.registerContainerProviderConnection({
1061+
name: 'connection',
1062+
type: 'podman',
1063+
lifecycle: {
1064+
start: startMock,
1065+
},
1066+
endpoint: {
1067+
socketPath: '/endpoint1.sock',
1068+
},
1069+
status() {
1070+
return 'stopped';
1071+
},
1072+
});
1073+
await providerRegistry.startProvider((provider as ProviderImpl).internalId);
1074+
expect(startMock).toBeCalled();
1075+
});
1076+
1077+
test('if the provider has one kubernetes connection with the start lifecycle, execute the start action', async () => {
1078+
const provider = providerRegistry.createProvider('id', 'name', {
1079+
id: 'internal',
1080+
name: 'internal',
1081+
status: 'installed',
1082+
});
1083+
const startMock = vi.fn();
1084+
provider.registerKubernetesProviderConnection({
1085+
name: 'connection',
1086+
lifecycle: {
1087+
start: startMock,
1088+
},
1089+
endpoint: {
1090+
apiURL: 'url',
1091+
},
1092+
status() {
1093+
return 'stopped';
1094+
},
1095+
});
1096+
await providerRegistry.startProvider((provider as ProviderImpl).internalId);
1097+
expect(startMock).toBeCalled();
1098+
});
1099+
});

packages/main/src/plugin/provider-registry.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -556,32 +556,35 @@ export class ProviderRegistry {
556556

557557
// start anything from the provider
558558
async startProvider(providerInternalId: string): Promise<void> {
559-
const provider = this.getMatchingProvider(providerInternalId);
560-
561559
// do we have a lifecycle attached to the provider ?
562560
if (this.providerLifecycles.has(providerInternalId)) {
563561
return this.startProviderLifecycle(providerInternalId);
564562
}
565563

564+
const provider = this.getMatchingProvider(providerInternalId);
565+
let connection: ContainerProviderConnection | KubernetesProviderConnection | undefined;
566+
566567
if (provider.containerConnections && provider.containerConnections.length > 0) {
567-
const connection = provider.containerConnections[0];
568-
const lifecycle = connection?.lifecycle;
569-
if (!lifecycle?.start) {
570-
throw new Error('The container connection does not support start lifecycle');
571-
}
572-
if (!connection) {
573-
throw new Error('The provider does not have a container connection to start');
574-
}
568+
connection = provider.containerConnections[0];
569+
} else if (provider.kubernetesConnections && provider.kubernetesConnections.length > 0) {
570+
connection = provider.kubernetesConnections[0];
571+
}
575572

576-
const context = this.connectionLifecycleContexts.get(connection);
577-
if (!context) {
578-
throw new Error('The connection does not have context to start');
579-
}
573+
if (!connection) {
574+
throw new Error('The provider does not have any connection to start');
575+
}
580576

581-
return lifecycle.start(context);
582-
} else {
583-
throw new Error('No container connection found for provider');
577+
const lifecycle = connection.lifecycle;
578+
if (!lifecycle?.start) {
579+
throw new Error(`The connection ${connection.name} does not support start lifecycle`);
580+
}
581+
582+
const context = this.connectionLifecycleContexts.get(connection);
583+
if (!context) {
584+
throw new Error(`The connection ${connection.name} does not have context to start`);
584585
}
586+
587+
return lifecycle.start(context);
585588
}
586589

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

0 commit comments

Comments
 (0)