Skip to content

Commit

Permalink
refactor: cleanup unused kubernetes client code (podman-desktop#10458)
Browse files Browse the repository at this point in the history
* refactor: revert unnecessary part of podman-desktop#4858
Signed-off-by: Philippe Martin <[email protected]>

* refactor: revert unnecessary part of podman-desktop#4821
Signed-off-by: Philippe Martin <[email protected]>

* refactor: revert unnecessary part of podman-desktop#4975
Signed-off-by: Philippe Martin <[email protected]>

* refactor: remove unnecessary methods related to create resources
Signed-off-by: Philippe Martin <[email protected]>
  • Loading branch information
feloy authored Dec 23, 2024
1 parent 8fe30a7 commit 06dbe63
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 404 deletions.
12 changes: 0 additions & 12 deletions packages/main/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2360,22 +2360,10 @@ export class PluginSystem {
return kubernetesClient.listPods();
});

this.ipcHandle('kubernetes-client:listDeployments', async (): Promise<V1Deployment[]> => {
return kubernetesClient.listDeployments();
});

this.ipcHandle('kubernetes-client:listIngresses', async (): Promise<V1Ingress[]> => {
return kubernetesClient.listIngresses();
});

this.ipcHandle('kubernetes-client:listRoutes', async (): Promise<V1Route[]> => {
return kubernetesClient.listRoutes();
});

this.ipcHandle('kubernetes-client:listServices', async (): Promise<V1Service[]> => {
return kubernetesClient.listServices();
});

this.ipcHandle(
'kubernetes-client:readPodLog',
async (_listener, name: string, container: string, onDataId: number): Promise<void> => {
Expand Down
211 changes: 0 additions & 211 deletions packages/main/src/plugin/kubernetes/kubernetes-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,24 +419,6 @@ describe.each([
},
);

test('Create Kubernetes resources with v1 resource in error should return error', async () => {
const client = createTestClient();
const spy = vi.spyOn(client, 'createV1Resource').mockRejectedValue(new Error('V1Error'));
try {
await client.createResources('dummy', [{ apiVersion: 'v1', kind: 'Namespace' }]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
expect(spy).toBeCalled();
expect(err).to.be.a('Error');
expect(err.message).equal('V1Error');
expect(telemetry.track).toHaveBeenCalledWith('kubernetesSyncResources', {
action: 'create',
manifestsSize: 1,
error: new Error('V1Error'),
});
}
});

describe.each([
{
manifest: { apiVersion: 'group/v1', kind: 'Namespace' },
Expand Down Expand Up @@ -475,47 +457,6 @@ describe.each([
});
});

test('Create custom Kubernetes resources in error should return error', async () => {
const client = createTestClient();
const spy = vi.spyOn(client, 'createCustomResource').mockRejectedValue(new Error('CustomError'));
vi.spyOn(client, 'getAPIResource').mockReturnValue(
Promise.resolve({ name: 'namespaces', namespaced: true, kind: 'Namespace', singularName: 'namespace', verbs: [] }),
);
try {
await client.createResources('dummy', [{ apiVersion: 'group/v1', kind: 'Namespace' }]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
expect(spy).toBeCalled();
expect(err).to.be.a('Error');
expect(err.message).equal('CustomError');
expect(telemetry.track).toHaveBeenCalledWith('kubernetesSyncResources', {
action: 'create',
manifestsSize: 1,
error: new Error('CustomError'),
});
}
});

test('Create unknown custom Kubernetes resources should return error', async () => {
const client = createTestClient();
const createSpy = vi.spyOn(client, 'createCustomResource').mockResolvedValue(undefined);
const pluralSpy = vi.spyOn(client, 'getAPIResource').mockRejectedValue(new Error('CustomError'));
try {
await client.createResources('dummy', [{ apiVersion: 'group/v1', kind: 'Namespace' }]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
expect(createSpy).not.toBeCalled();
expect(pluralSpy).toBeCalled();
expect(err).to.be.a('Error');
expect(err.message).equal('CustomError');
expect(telemetry.track).toHaveBeenCalledWith('kubernetesSyncResources', {
action: 'create',
manifestsSize: 1,
error: new Error('CustomError'),
});
}
});

test('Check connection to Kubernetes cluster', async () => {
// Mock k8sApi.getCode() to return the version of the cluster
makeApiClientMock.mockReturnValue({
Expand Down Expand Up @@ -602,57 +543,6 @@ test('kube watcher', () => {
expect(createWatchObjectSpy).toBeCalled();
});

// Deployment
test('should return empty deployment list if there is no active namespace', async () => {
const client = createTestClient();

const list = await client.listDeployments();
expect(list.length).toBe(0);
});

test('should return empty deployment list if cannot connect to cluster', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.reject(new Error('K8sError')),
});

const list = await client.listDeployments();
expect(list.length).toBe(0);
});

test('should return empty deployment list if cannot execute call to cluster', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.resolve({ gitVersion: 'v1.20.0' }),
listNamespacedDeployent: () => Promise.reject(new Error('K8sError')),
});

const list = await client.listDeployments();
expect(list.length).toBe(0);
});

test('should return deployment list if connection to cluster is ok', async () => {
const v1Deployment: V1Deployment = {
apiVersion: 'networking.k8s.io/v1',
kind: 'Deployment',
metadata: {
name: 'deployment',
},
};
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.resolve({ gitVersion: 'v1.20.0' }),
listNamespacedDeployment: () =>
Promise.resolve({
items: [v1Deployment],
}),
});

const list = await client.listDeployments();
expect(list.length).toBe(1);
expect(list[0]?.metadata?.name).toEqual('deployment');
});

test('should throw error if cannot call the cluster (readNamespacedDeployment reject)', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
Expand Down Expand Up @@ -699,57 +589,6 @@ test('should return deployment if it exists', async () => {
expect(deployment?.metadata?.name).toEqual('deployment');
});

// Ingress
test('should return empty ingress list if there is no active namespace', async () => {
const client = createTestClient();

const list = await client.listIngresses();
expect(list.length).toBe(0);
});

test('should return empty ingress list if cannot connect to cluster', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.reject(new Error('K8sError')),
});

const list = await client.listIngresses();
expect(list.length).toBe(0);
});

test('should return empty ingress list if cannot execute call to cluster', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.resolve({ body: { gitVersion: 'v1.20.0' } }),
listNamespacedIngress: () => Promise.reject(new Error('K8sError')),
});

const list = await client.listIngresses();
expect(list.length).toBe(0);
});

test('should return ingress list if connection to cluster is ok', async () => {
const v1Ingress: V1Ingress = {
apiVersion: 'networking.k8s.io/v1',
kind: 'Ingress',
metadata: {
name: 'ingress',
},
};
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.resolve({ body: { gitVersion: 'v1.20.0' } }),
listNamespacedIngress: () =>
Promise.resolve({
items: [v1Ingress],
}),
});

const list = await client.listIngresses();
expect(list.length).toBe(1);
expect(list[0]?.metadata?.name).toEqual('ingress');
});

test('should throw error if cannot call the cluster (readNamespacedIngress reject)', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
Expand Down Expand Up @@ -1002,56 +841,6 @@ test('Expect deleteRoute to be called if there is active connection', async () =
expect(deleteRouteMock).toBeCalled();
});

test('should return empty service list if there is no active namespace', async () => {
const client = createTestClient();

const list = await client.listServices();
expect(list.length).toBe(0);
});

test('should return empty service list if cannot connect to cluster', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.reject(new Error('K8sError')),
});

const list = await client.listServices();
expect(list.length).toBe(0);
});

test('should return empty service list if cannot execute call to cluster', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.resolve({ body: { gitVersion: 'v1.20.0' } }),
listNamespacedService: () => Promise.reject(new Error('K8sError')),
});

const list = await client.listServices();
expect(list.length).toBe(0);
});

test('should return service list if connection to cluster is ok', async () => {
const v1Service: V1Service = {
apiVersion: 'k8s.io/v1',
kind: 'Service',
metadata: {
name: 'service',
},
};
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
getCode: () => Promise.resolve({ body: { gitVersion: 'v1.20.0' } }),
listNamespacedService: () =>
Promise.resolve({
items: [v1Service],
}),
});

const list = await client.listServices();
expect(list.length).toBe(1);
expect(list[0]?.metadata?.name).toEqual('service');
});

test('should throw error if cannot call the cluster (readNamespacedService reject)', async () => {
const client = createTestClient('default');
makeApiClientMock.mockReturnValue({
Expand Down
Loading

0 comments on commit 06dbe63

Please sign in to comment.