From 5e85391dd815a7c11857eb8b0111a1537f17e05a Mon Sep 17 00:00:00 2001 From: biersoeckli Date: Thu, 14 Nov 2024 14:13:03 +0000 Subject: [PATCH] fixed error with pvc size calculation in Mi and Gi --- src/server/services/pvc.service.ts | 19 ++++++++++--------- src/server/utils/memory-caluclation.utils.ts | 13 +++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 src/server/utils/memory-caluclation.utils.ts diff --git a/src/server/services/pvc.service.ts b/src/server/services/pvc.service.ts index 2160aab..2d0d360 100644 --- a/src/server/services/pvc.service.ts +++ b/src/server/services/pvc.service.ts @@ -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); diff --git a/src/server/utils/memory-caluclation.utils.ts b/src/server/utils/memory-caluclation.utils.ts new file mode 100644 index 0000000..d84005b --- /dev/null +++ b/src/server/utils/memory-caluclation.utils.ts @@ -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`; + } + } +} \ No newline at end of file