remove kubernetes client caching

Signed-off-by: mclarke <mclarke@spotify.com>
This commit is contained in:
mclarke
2021-03-09 08:20:49 +00:00
parent 68ef627a4b
commit da59f00f5e
2 changed files with 5 additions and 140 deletions
@@ -45,42 +45,6 @@ describe('KubernetesClientProvider', () => {
expect(mockGetKubeConfig.mock.calls.length).toBe(1);
});
it('can get cached core client by cluster details', async () => {
const sut = new KubernetesClientProvider();
const mockGetKubeConfig = jest.fn(sut.getKubeConfig.bind({}));
sut.getKubeConfig = mockGetKubeConfig;
const result1 = sut.getCoreClientByClusterDetails({
name: 'cluster-name',
url: 'http://localhost:9999',
serviceAccountToken: 'TOKEN',
authProvider: 'serviceAccount',
});
const result2 = sut.getCoreClientByClusterDetails({
name: 'cluster-name',
url: 'http://localhost:9999',
serviceAccountToken: 'TOKEN',
authProvider: 'serviceAccount',
});
expect(result1.basePath).toBe('http://localhost:9999');
// These fields aren't on the type but are there
const auth1 = (result1 as any).authentications.default;
expect(auth1.users[0].token).toBe('TOKEN');
expect(auth1.clusters[0].name).toBe('cluster-name');
expect(result2.basePath).toBe('http://localhost:9999');
// These fields aren't on the type but are there
const auth2 = (result2 as any).authentications.default;
expect(auth2.users[0].token).toBe('TOKEN');
expect(auth2.clusters[0].name).toBe('cluster-name');
expect(mockGetKubeConfig.mock.calls.length).toBe(1);
});
it('can get apps client by cluster details', async () => {
const sut = new KubernetesClientProvider();
@@ -103,40 +67,4 @@ describe('KubernetesClientProvider', () => {
expect(mockGetKubeConfig.mock.calls.length).toBe(1);
});
it('can get cached apps client by cluster details', async () => {
const sut = new KubernetesClientProvider();
const mockGetKubeConfig = jest.fn(sut.getKubeConfig.bind({}));
sut.getKubeConfig = mockGetKubeConfig;
const result1 = sut.getAppsClientByClusterDetails({
name: 'cluster-name',
url: 'http://localhost:9999',
serviceAccountToken: 'TOKEN',
authProvider: 'serviceAccount',
});
const result2 = sut.getAppsClientByClusterDetails({
name: 'cluster-name',
url: 'http://localhost:9999',
serviceAccountToken: 'TOKEN',
authProvider: 'serviceAccount',
});
expect(result1.basePath).toBe('http://localhost:9999');
// These fields aren't on the type but are there
const auth1 = (result1 as any).authentications.default;
expect(auth1.users[0].token).toBe('TOKEN');
expect(auth1.clusters[0].name).toBe('cluster-name');
expect(result2.basePath).toBe('http://localhost:9999');
// These fields aren't on the type but are there
const auth2 = (result2 as any).authentications.default;
expect(auth2.users[0].token).toBe('TOKEN');
expect(auth2.clusters[0].name).toBe('cluster-name');
expect(mockGetKubeConfig.mock.calls.length).toBe(1);
});
});
@@ -24,29 +24,6 @@ import {
} from '@kubernetes/client-node';
export class KubernetesClientProvider {
private readonly coreClientMap: {
[key: string]: CoreV1Api;
};
private readonly appsClientMap: {
[key: string]: AppsV1Api;
};
private readonly autoscalingClientMap: {
[key: string]: AutoscalingV1Api;
};
private readonly networkingBeta1ClientMap: {
[key: string]: NetworkingV1beta1Api;
};
constructor() {
this.coreClientMap = {};
this.appsClientMap = {};
this.autoscalingClientMap = {};
this.networkingBeta1ClientMap = {};
}
// visible for testing
getKubeConfig(clusterDetails: ClusterDetails) {
const cluster = {
@@ -58,7 +35,7 @@ export class KubernetesClientProvider {
// TODO configure
const user = {
name: 'service-account',
name: 'backstage',
token: clusterDetails.serviceAccountToken,
};
@@ -79,66 +56,26 @@ export class KubernetesClientProvider {
}
getCoreClientByClusterDetails(clusterDetails: ClusterDetails) {
const clientMapKey = clusterDetails.name;
if (this.coreClientMap.hasOwnProperty(clientMapKey)) {
return this.coreClientMap[clientMapKey];
}
const kc = this.getKubeConfig(clusterDetails);
const client = kc.makeApiClient(CoreV1Api);
this.coreClientMap[clientMapKey] = client;
return client;
return kc.makeApiClient(CoreV1Api);
}
getAppsClientByClusterDetails(clusterDetails: ClusterDetails) {
const clientMapKey = clusterDetails.name;
if (this.appsClientMap.hasOwnProperty(clientMapKey)) {
return this.appsClientMap[clientMapKey];
}
const kc = this.getKubeConfig(clusterDetails);
const client = kc.makeApiClient(AppsV1Api);
this.appsClientMap[clientMapKey] = client;
return client;
return kc.makeApiClient(AppsV1Api);
}
getAutoscalingClientByClusterDetails(clusterDetails: ClusterDetails) {
const clientMapKey = clusterDetails.name;
if (this.autoscalingClientMap.hasOwnProperty(clientMapKey)) {
return this.autoscalingClientMap[clientMapKey];
}
const kc = this.getKubeConfig(clusterDetails);
const client = kc.makeApiClient(AutoscalingV1Api);
this.autoscalingClientMap[clientMapKey] = client;
return client;
return kc.makeApiClient(AutoscalingV1Api);
}
getNetworkingBeta1Client(clusterDetails: ClusterDetails) {
const clientMapKey = clusterDetails.name;
if (this.networkingBeta1ClientMap.hasOwnProperty(clientMapKey)) {
return this.networkingBeta1ClientMap[clientMapKey];
}
const kc = this.getKubeConfig(clusterDetails);
const client = kc.makeApiClient(NetworkingV1beta1Api);
this.networkingBeta1ClientMap[clientMapKey] = client;
return client;
return kc.makeApiClient(NetworkingV1beta1Api);
}
}