You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/kubernetes/helpers/resourceReservationHelper.js

45 lines
1.2 KiB

import _ from 'lodash-es';
import filesizeParser from 'filesize-parser';
import { KubernetesResourceReservation } from 'Kubernetes/models/resource-reservation/models';
class KubernetesResourceReservationHelper {
static computeResourceReservation(pods) {
const containers = _.reduce(pods, (acc, pod) => _.concat(acc, pod.Containers), []);
return _.reduce(
containers,
(acc, container) => {
if (container.Requests) {
if (container.Requests.memory) {
acc.Memory += filesizeParser(container.Requests.memory, { base: 10 });
}
if (container.Requests.cpu) {
acc.CPU += KubernetesResourceReservationHelper.parseCPU(container.Requests.cpu);
}
}
return acc;
},
new KubernetesResourceReservation()
);
}
static parseCPU(cpu) {
let res = parseInt(cpu, 10);
if (_.endsWith(cpu, 'm')) {
res /= 1000;
}
return res;
}
static megaBytesValue(value) {
return Math.floor(filesizeParser(value) / 1000 / 1000);
}
static bytesValue(mem) {
return filesizeParser(mem) * 1000 * 1000;
}
}
export default KubernetesResourceReservationHelper;