forked from kubernetes-client/javascript
/
util.ts
145 lines (133 loc) · 5.16 KB
/
util.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { isNumber } from 'underscore';
import { CoreV1Api, V1Container, V1Pod } from './gen/api';
export async function podsForNode(api: CoreV1Api, nodeName: string): Promise<V1Pod[]> {
const allPods = await api.listPodForAllNamespaces();
return allPods.body.items.filter((pod: V1Pod) => pod.spec!.nodeName === nodeName);
}
export function findSuffix(quantity: string): string {
let ix = quantity.length - 1;
while (ix >= 0 && !/[\.0-9]/.test(quantity.charAt(ix))) {
ix--;
}
return ix === -1 ? '' : quantity.substring(ix + 1);
}
export function quantityToScalar(quantity: string): number | bigint {
if (!quantity) {
return 0;
}
const suffix = findSuffix(quantity);
if (suffix === '') {
const num = Number(quantity).valueOf();
if (isNaN(num)) {
throw new Error('Unknown quantity ' + quantity);
}
return num;
}
switch (suffix) {
case 'n':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000_000.0;
case 'u':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000.0;
case 'm':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1000.0;
case 'k':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000);
case 'M':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000);
case 'G':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000);
case 'T':
return (
BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000)
);
case 'P':
return (
BigInt(quantity.substr(0, quantity.length - 1)) *
BigInt(1000 * 1000 * 1000) *
BigInt(1000 * 1000)
);
case 'E':
return (
BigInt(quantity.substr(0, quantity.length - 1)) *
BigInt(1000 * 1000 * 1000) *
BigInt(1000 * 1000 * 1000)
);
case 'Ki':
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024);
case 'Mi':
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024);
case 'Gi':
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024);
case 'Ti':
return (
BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024)
);
case 'Pi':
return (
BigInt(quantity.substr(0, quantity.length - 2)) *
BigInt(1024 * 1024 * 1024) *
BigInt(1024 * 1024)
);
case 'Ei':
return (
BigInt(quantity.substr(0, quantity.length - 2)) *
BigInt(1024 * 1024 * 1024) *
BigInt(1024 * 1024 * 1024)
);
default:
throw new Error(`Unknown suffix: ${suffix}`);
}
}
export class ResourceStatus {
constructor(
public readonly request: bigint | number,
public readonly limit: bigint | number,
public readonly resourceType: string,
) {}
}
export function totalCPUForContainer(container: V1Container): ResourceStatus {
return containerTotalForResource(container, 'cpu');
}
export function totalMemoryForContainer(container: V1Container): ResourceStatus {
return containerTotalForResource(container, 'memory');
}
export function totalCPU(pod: V1Pod): ResourceStatus {
return totalForResource(pod, 'cpu');
}
export function totalMemory(pod: V1Pod): ResourceStatus {
return totalForResource(pod, 'memory');
}
export function add(n1: number | bigint, n2: number | bigint): number | bigint {
if (isNumber(n1) && isNumber(n2)) {
return n1 + n2;
}
if (isNumber(n1)) {
return BigInt(Math.round(n1)) + (n2 as bigint);
} else if (isNumber(n2)) {
return (n1 as bigint) + BigInt(Math.round(n2));
}
return ((n1 as bigint) + n2) as bigint;
}
export function containerTotalForResource(container: V1Container, resource: string): ResourceStatus {
let reqTotal: number | bigint = 0;
let limitTotal: number | bigint = 0;
if (container.resources) {
if (container.resources.requests) {
reqTotal = add(reqTotal, quantityToScalar(container.resources.requests[resource]));
}
if (container.resources.limits) {
limitTotal = add(limitTotal, quantityToScalar(container.resources.limits[resource]));
}
}
return new ResourceStatus(reqTotal, limitTotal, resource);
}
export function totalForResource(pod: V1Pod, resource: string): ResourceStatus {
let reqTotal: number | bigint = 0;
let limitTotal: number | bigint = 0;
pod.spec!.containers.forEach((container: V1Container) => {
const containerTotal = containerTotalForResource(container, resource);
reqTotal = add(reqTotal, containerTotal.request);
limitTotal = add(limitTotal, containerTotal.limit);
});
return new ResourceStatus(reqTotal, limitTotal, resource);
}