Skip to content

Commit 223b689

Browse files
committed
replace token in Metalk8sLocalVolumeProvider by getToken
1 parent bb822f1 commit 223b689

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

ui/src/services/k8s/Metalk8sLocalVolumeProvider.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jest.mock('../k8s/api', () => ({
1212
describe('Metalk8sLocalVolumeProvider', () => {
1313
let provider: Metalk8sLocalVolumeProvider;
1414
const mockUrl = 'mock-url';
15-
const mockToken = 'mock-token';
15+
const mockToken = jest.fn(() => Promise.resolve('mock-token'));
1616

1717
const mockCustomObjectsApi = {
1818
listClusterCustomObject: jest.fn(),
@@ -39,8 +39,6 @@ describe('Metalk8sLocalVolumeProvider', () => {
3939
});
4040

4141
provider = new Metalk8sLocalVolumeProvider(mockUrl, mockToken);
42-
provider.k8sClient = mockCoreV1Api;
43-
provider.volumeClient = mockVolumeClient;
4442
});
4543

4644
describe('listLocalPersistentVolumes', () => {

ui/src/services/k8s/Metalk8sLocalVolumeProvider.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CoreV1Api, V1PersistentVolume } from '@kubernetes/client-node';
1+
import { V1PersistentVolume } from '@kubernetes/client-node';
22
import * as ApiK8s from './api';
33
import {
44
Metalk8sV1alpha1VolumeClient,
@@ -22,18 +22,24 @@ export type LocalPersistentVolume = V1PersistentVolume & {
2222
};
2323

2424
export default class Metalk8sLocalVolumeProvider {
25-
volumeClient: Metalk8sV1alpha1VolumeClient;
26-
k8sClient: CoreV1Api;
27-
constructor(url: string, token: string) {
28-
const { coreV1, customObjects } = ApiK8s.updateApiServerConfig(url, token);
29-
this.volumeClient = new Metalk8sV1alpha1VolumeClient(customObjects);
30-
this.k8sClient = coreV1;
25+
apiUrl: string;
26+
constructor(apiUrl: string, private getToken: () => Promise<string>) {
27+
this.apiUrl = apiUrl;
3128
}
29+
3230
public listLocalPersistentVolumes = async (
3331
serverName: string,
3432
): Promise<LocalPersistentVolume[]> => {
3533
try {
36-
const nodes = await this.k8sClient.listNode();
34+
const token = await this.getToken();
35+
const { coreV1, customObjects } = ApiK8s.updateApiServerConfig(
36+
this.apiUrl,
37+
token,
38+
);
39+
const volumeClient = new Metalk8sV1alpha1VolumeClient(customObjects);
40+
const k8sClient = coreV1;
41+
42+
const nodes = await k8sClient.listNode();
3743
const nodeIP = nodes.body.items
3844
.find((node) => node.metadata.name === serverName)
3945
?.status.addresses.find((address) => address.type === 'InternalIP');
@@ -42,13 +48,13 @@ export default class Metalk8sLocalVolumeProvider {
4248
throw new Error(`Failed to find IP for node ${serverName}`);
4349
}
4450

45-
const volumes = await this.volumeClient.getMetalk8sV1alpha1VolumeList();
51+
const volumes = await volumeClient.getMetalk8sV1alpha1VolumeList();
4652

4753
if (!isError(volumes)) {
4854
const nodeVolumes = volumes.body.items.filter(
4955
(volume) => volume.spec.nodeName === serverName,
5056
);
51-
const pv = await this.k8sClient.listPersistentVolume();
57+
const pv = await k8sClient.listPersistentVolume();
5258

5359
const localPv = nodeVolumes.reduce((acc, item) => {
5460
const isLocalPv = pv.body.items.find(

0 commit comments

Comments
 (0)