Skip to content

Commit e3d2d8d

Browse files
authored
Do not cache the service account token in the devWorkspace client (#1283)
1 parent 7fa2a86 commit e3d2d8d

22 files changed

+38
-154
lines changed

packages/dashboard-backend/src/__tests__/app.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const mockProcessExit = jest.fn();
2020
});
2121

2222
jest.mock('../routes/api/helpers/getDevWorkspaceClient.ts');
23+
jest.mock('../routes/api/helpers/getServiceAccountToken.ts');
2324
describe('App', () => {
2425
afterEach(() => {
2526
jest.clearAllMocks();

packages/dashboard-backend/src/app.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ import { registerEditorsRoutes } from '@/routes/api/editors';
3434
import { registerEventsRoutes } from '@/routes/api/events';
3535
import { registerGettingStartedSamplesRoutes } from '@/routes/api/gettingStartedSample';
3636
import { registerGitConfigRoutes } from '@/routes/api/gitConfig';
37-
import { getDevWorkspaceSingletonClient } from '@/routes/api/helpers/getDevWorkspaceClient';
37+
import { getDevWorkspaceClient } from '@/routes/api/helpers/getDevWorkspaceClient';
38+
import { getServiceAccountToken } from '@/routes/api/helpers/getServiceAccountToken';
3839
import { registerKubeConfigRoute } from '@/routes/api/kubeConfig';
3940
import { registerPersonalAccessTokenRoutes } from '@/routes/api/personalAccessToken';
4041
import { registerPodmanLoginRoute } from '@/routes/api/podmanLogin';
@@ -71,8 +72,8 @@ export default async function buildApp(server: FastifyInstance): Promise<unknown
7172
},
7273
);
7374

74-
const { devWorkspaceClusterServiceApi } = getDevWorkspaceSingletonClient();
75-
await devWorkspaceClusterServiceApi.watchInAllNamespaces();
75+
const devWorkspaceClient = getDevWorkspaceClient(getServiceAccountToken());
76+
await devWorkspaceClient.devWorkspaceClusterApi.watchInAllNamespaces();
7677

7778
server.register(import('@fastify/rate-limit'));
7879

packages/dashboard-backend/src/devworkspaceClient/__tests__/index.spec.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import * as mockClientNode from '@kubernetes/client-node';
1414
import { KubeConfig } from '@kubernetes/client-node';
1515

16+
import { DevWorkspaceClient } from '@/devworkspaceClient';
1617
import { DevWorkspaceApiService } from '@/devworkspaceClient/services/devWorkspaceApi';
1718
import { DevWorkspaceClusterApiService } from '@/devworkspaceClient/services/devWorkspaceClusterApiService';
1819
import { DevWorkspaceTemplateApiService } from '@/devworkspaceClient/services/devWorkspaceTemplateApi';
@@ -27,9 +28,7 @@ import { ServerConfigApiService } from '@/devworkspaceClient/services/serverConf
2728
import { SshKeysService } from '@/devworkspaceClient/services/sshKeysApi';
2829
import { UserProfileApiService } from '@/devworkspaceClient/services/userProfileApi';
2930

30-
import { DevWorkspaceClient, DevWorkspaceSingletonClient } from '..';
31-
32-
jest.mock('../services/devWorkspaceApi.ts');
31+
jest.mock('@/devworkspaceClient/services/devWorkspaceApi.ts');
3332

3433
describe('DevWorkspace client', () => {
3534
let config: KubeConfig;
@@ -50,6 +49,7 @@ describe('DevWorkspace client', () => {
5049
expect(client.devworkspaceApi).toBeInstanceOf(DevWorkspaceApiService);
5150
expect(client.dockerConfigApi).toBeInstanceOf(DockerConfigApiService);
5251
expect(client.eventApi).toBeInstanceOf(EventApiService);
52+
expect(client.devWorkspaceClusterApi).toBeInstanceOf(DevWorkspaceClusterApiService);
5353
expect(client.kubeConfigApi).toBeInstanceOf(KubeConfigApiService);
5454
expect(client.logsApi).toBeInstanceOf(LogsApiService);
5555
expect(client.podApi).toBeInstanceOf(PodApiService);
@@ -60,28 +60,3 @@ describe('DevWorkspace client', () => {
6060
expect(client.sshKeysApi).toBeInstanceOf(SshKeysService);
6161
});
6262
});
63-
64-
describe('DevWorkspace singleton client', () => {
65-
let config: KubeConfig;
66-
beforeEach(() => {
67-
const { KubeConfig } = mockClientNode;
68-
config = new KubeConfig();
69-
config.makeApiClient = jest.fn().mockImplementation(() => ({}));
70-
});
71-
72-
afterEach(() => {
73-
jest.clearAllMocks();
74-
});
75-
76-
test('client', () => {
77-
const client = DevWorkspaceSingletonClient.getInstance(config);
78-
79-
expect(client.devWorkspaceClusterServiceApi).toBeInstanceOf(DevWorkspaceClusterApiService);
80-
});
81-
82-
test('return same instance', () => {
83-
const client = DevWorkspaceSingletonClient.getInstance(config);
84-
85-
expect(client.devWorkspaceClusterServiceApi).toEqual(client.devWorkspaceClusterServiceApi);
86-
});
87-
});

packages/dashboard-backend/src/devworkspaceClient/index.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
IDevWorkspaceApi,
3636
IDevWorkspaceClient,
3737
IDevWorkspaceClusterApi,
38-
IDevWorkspaceSingletonClient,
3938
IDevWorkspaceTemplateApi,
4039
IDockerConfigApi,
4140
IEditorsApi,
@@ -110,6 +109,10 @@ export class DevWorkspaceClient implements IDevWorkspaceClient {
110109
return new GitConfigApiService(this.kubeConfig);
111110
}
112111

112+
get devWorkspaceClusterApi(): IDevWorkspaceClusterApi {
113+
return new DevWorkspaceClusterApiService(this.kubeConfig);
114+
}
115+
113116
get gettingStartedSampleApi(): IGettingStartedSampleApi {
114117
return new GettingStartedSamplesApiService(this.kubeConfig);
115118
}
@@ -130,22 +133,3 @@ export class DevWorkspaceClient implements IDevWorkspaceClient {
130133
return new WorkspacePreferencesApiService(this.kubeConfig);
131134
}
132135
}
133-
134-
let devWorkspaceSingletonClient: DevWorkspaceSingletonClient;
135-
136-
/**
137-
* Singleton client for devworkspace services.
138-
*/
139-
export class DevWorkspaceSingletonClient implements IDevWorkspaceSingletonClient {
140-
static getInstance(kc: k8s.KubeConfig): DevWorkspaceSingletonClient {
141-
if (!devWorkspaceSingletonClient) {
142-
devWorkspaceSingletonClient = new DevWorkspaceSingletonClient(kc);
143-
}
144-
return devWorkspaceSingletonClient;
145-
}
146-
147-
public readonly devWorkspaceClusterServiceApi: IDevWorkspaceClusterApi;
148-
private constructor(kc: k8s.KubeConfig) {
149-
this.devWorkspaceClusterServiceApi = new DevWorkspaceClusterApiService(kc);
150-
}
151-
}

packages/dashboard-backend/src/routes/__tests__/factoryAcceptanceRedirect.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { FastifyInstance } from 'fastify';
1515
import { setup, teardown } from '@/utils/appBuilder';
1616

1717
jest.mock('../api/helpers/getDevWorkspaceClient.ts');
18+
jest.mock('../api/helpers/getServiceAccountToken.ts');
1819
describe('Factory Acceptance Redirect', () => {
1920
let app: FastifyInstance;
2021

packages/dashboard-backend/src/routes/api/__tests__/clusterInfo.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { baseApiPath } from '@/constants/config';
1717
import { setup, teardown } from '@/utils/appBuilder';
1818

1919
jest.mock('../helpers/getDevWorkspaceClient.ts');
20+
jest.mock('../helpers/getServiceAccountToken.ts');
2021
describe('Cluster Info Route', () => {
2122
let app: FastifyInstance;
2223
const clusterConsoleUrl = 'cluster-console-url';

packages/dashboard-backend/src/routes/api/__tests__/dataResolver.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { setup, teardown } from '@/utils/appBuilder';
1919

2020
jest.mock('@/routes/api/helpers/getCertificateAuthority');
2121
jest.mock('../helpers/getDevWorkspaceClient.ts');
22+
jest.mock('../helpers/getServiceAccountToken.ts');
2223
const axiosInstanceMock = jest.fn();
2324
(axiosInstance.get as jest.Mock).mockImplementation(axiosInstanceMock);
2425
const defaultAxiosInstanceMock = jest.fn();

packages/dashboard-backend/src/routes/api/__tests__/devworkspaceTemplates.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import { setup, teardown } from '@/utils/appBuilder';
2222

2323
jest.mock('../helpers/getDevWorkspaceClient.ts');
24+
jest.mock('../helpers/getServiceAccountToken.ts');
2425
jest.mock('../helpers/getToken.ts');
2526

2627
describe('DevWorkspaceTemplates Routes', () => {

packages/dashboard-backend/src/routes/api/__tests__/devworkspaces.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { setup, teardown } from '@/utils/appBuilder';
2222

2323
jest.mock('../helpers/getDevWorkspaceClient.ts');
2424
jest.mock('../helpers/getToken.ts');
25+
jest.mock('../helpers/getServiceAccountToken.ts');
2526

2627
describe('DevWorkspaces Routes', () => {
2728
let app: FastifyInstance;

packages/dashboard-backend/src/routes/api/__tests__/dockerConfig.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { setup, teardown } from '@/utils/appBuilder';
1818

1919
jest.mock('../helpers/getDevWorkspaceClient.ts');
2020
jest.mock('../helpers/getToken.ts');
21+
jest.mock('../helpers/getServiceAccountToken.ts');
2122

2223
describe('Docker Config Routes', () => {
2324
let app: FastifyInstance;

0 commit comments

Comments
 (0)