-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathMetalk8sLocalVolumeProvider.ts
More file actions
83 lines (73 loc) · 2.5 KB
/
Metalk8sLocalVolumeProvider.ts
File metadata and controls
83 lines (73 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { CoreV1Api, V1PersistentVolume } from '@kubernetes/client-node';
import * as ApiK8s from './api';
import {
Metalk8sV1alpha1VolumeClient,
Result,
} from './Metalk8sVolumeClient.generated';
function isError<T>(result: Result<T>): result is { error: any } {
return (result as { error: any }).error !== undefined;
}
export enum VolumeType {
Hardware = 'Hardware',
Virtual = 'Virtual',
}
export type LocalPersistentVolume = V1PersistentVolume & {
IP: string;
devicePath: string;
nodeName: string;
volumeType: VolumeType;
};
export default class Metalk8sLocalVolumeProvider {
volumeClient: Metalk8sV1alpha1VolumeClient;
k8sClient: CoreV1Api;
constructor(url: string, token: string) {
const { coreV1, customObjects } = ApiK8s.updateApiServerConfig(url, token);
this.volumeClient = new Metalk8sV1alpha1VolumeClient(customObjects);
this.k8sClient = coreV1;
}
public listLocalPersistentVolumes = async (
serverName: string,
): Promise<LocalPersistentVolume[]> => {
try {
const nodes = await this.k8sClient.listNode();
const nodeIP = nodes.body.items
.find((node) => node.metadata.name === serverName)
?.status.addresses.find((address) => address.type === 'InternalIP');
if (!nodeIP) {
throw new Error(`Failed to find IP for node ${serverName}`);
}
const volumes = await this.volumeClient.getMetalk8sV1alpha1VolumeList();
if (!isError(volumes)) {
const nodeVolumes = volumes.body.items.filter(
(volume) => volume.spec.nodeName === serverName,
);
const pv = await this.k8sClient.listPersistentVolume();
const localPv = nodeVolumes.reduce((acc, item) => {
const isLocalPv = pv.body.items.find(
(p) => p.metadata.name === item.metadata['name'],
);
return [
...acc,
{
...isLocalPv,
IP: nodeIP.address,
devicePath:
item.spec?.rawBlockDevice?.devicePath || item.metadata['name'],
nodeName: item.spec.nodeName,
volumeType: item.spec.rawBlockDevice
? VolumeType.Hardware
: VolumeType.Virtual,
},
];
}, [] as LocalPersistentVolume[]);
return localPv;
} else {
throw new Error(`Failed to fetch metalk8s volumes: ${volumes.error}`);
}
} catch (error) {
throw new Error(
`Failed to fetch local persistent volumes: ${error.message}`,
);
}
};
}