fixed error with pvc size calculation in Mi and Gi

This commit is contained in:
biersoeckli
2024-11-14 14:13:03 +00:00
parent 0968a4fb7b
commit 5e85391dd8
2 changed files with 23 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ import { ServiceException } from "@/model/service.exception.model";
import { AppVolume } from "@prisma/client";
import { StringUtils } from "../utils/string.utils";
import { Constants } from "../utils/constants";
import { MemoryCalcUtils } from "../utils/memory-caluclation.utils";
class PvcService {
@@ -14,7 +15,7 @@ class PvcService {
for (const appVolume of app.appVolumes) {
const pvcName = StringUtils.toPvcName(appVolume.id);
const existingPvc = existingPvcs.find(pvc => pvc.metadata?.name === pvcName);
if (existingPvc && existingPvc.spec!.resources!.requests!.storage !== `${appVolume.size}Mi`) {
if (existingPvc && existingPvc.spec!.resources!.requests!.storage !== MemoryCalcUtils.formatSize(appVolume.size)) {
return true;
}
}
@@ -72,7 +73,7 @@ class PvcService {
storageClassName: 'longhorn',
resources: {
requests: {
storage: `${appVolume.size}Mi`,
storage: MemoryCalcUtils.formatSize(appVolume.size),
},
},
},
@@ -80,21 +81,21 @@ class PvcService {
const existingPvc = existingPvcs.find(pvc => pvc.metadata?.name === pvcName);
if (existingPvc) {
if (existingPvc.spec!.resources!.requests!.storage === `${appVolume.size}Mi`) {
if (existingPvc.spec!.resources!.requests!.storage === MemoryCalcUtils.formatSize(appVolume.size)) {
console.log(`PVC ${pvcName} for app ${app.id} already exists with the same size`);
continue;
}
// Only the Size of PVC can be updated, so we need to delete and recreate the PVC
// update PVC size
existingPvc.spec!.resources!.requests!.storage = `${appVolume.size}Mi`;
existingPvc.spec!.resources!.requests!.storage = MemoryCalcUtils.formatSize(appVolume.size);
await k3s.core.replaceNamespacedPersistentVolumeClaim(pvcName, app.projectId, existingPvc);
console.log(`Updated PVC ${pvcName} for app ${app.id}`);
// wait until persisten volume ist resized
console.log(`Waiting for PV ${existingPvc.spec!.volumeName} to be resized`);
console.log(`Waiting for PV ${existingPvc.spec!.volumeName} to be resized to ${existingPvc.spec!.resources!.requests!.storage}...`);
await this.waitUntilPvResized(existingPvc.spec!.volumeName!, appVolume.size);
console.log(`PV ${existingPvc.spec!.volumeName} resized to ${appVolume.size}Mi`);
console.log(`PV ${existingPvc.spec!.volumeName} resized to ${MemoryCalcUtils.formatSize(appVolume.size)}`);
} else {
await k3s.core.createNamespacedPersistentVolumeClaim(app.projectId, pvcDefinition);
console.log(`Created PVC ${pvcName} for app ${app.id}`);
@@ -123,10 +124,10 @@ class PvcService {
private async waitUntilPvResized(persistentVolumeName: string, size: number) {
let iterationCount = 0;
let pv = await k3s.core.readPersistentVolume(persistentVolumeName);
while (pv.body.spec!.capacity!.storage !== `${size}Mi`) {
while (pv.body.spec!.capacity!.storage !== MemoryCalcUtils.formatSize(size)) {
if (iterationCount > 30) {
console.error(`Timeout: PV ${persistentVolumeName} not resized to ${size}Mi`);
throw new ServiceException(`Timeout: Volume could not be resized to ${size}Mi`);
console.error(`Timeout: PV ${persistentVolumeName} not resized to ${MemoryCalcUtils.formatSize(size)}`);
throw new ServiceException(`Timeout: Volume could not be resized to ${MemoryCalcUtils.formatSize(size)}`);
}
await new Promise(resolve => setTimeout(resolve, 3000)); // wait 5 Seconds, so that the PV is resized
pv = await k3s.core.readPersistentVolume(persistentVolumeName);

View File

@@ -0,0 +1,13 @@
export class MemoryCalcUtils {
/**
* Formats the given size in megabytes to a Kubernetes readable format.
*/
static formatSize(megabytes: number): string {
if (megabytes % 1024 === 0) {
return `${Math.round(megabytes / 1024)}Gi`;
} else {
return `${Math.round(megabytes)}Mi`;
}
}
}