@controlplane/cli
Version:
Control Plane Corporation CLI
168 lines • 6.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.K8sPersistentVolumeClaimHandler = void 0;
const helper_1 = require("../../util/helper");
const storage_class_1 = require("../../types/storage-class");
const extraTypes_1 = require("../../../rest/extraTypes");
class K8sPersistentVolumeClaimHandler {
constructor(filePath, pvc, mapper) {
this.filePath = filePath;
this.pvc = pvc;
this.mapper = mapper;
this.volumeSet = {
kind: 'volumeset',
name: pvc.metadata.name,
spec: {
initialCapacity: 10,
performanceClass: 'general-purpose-ssd',
fileSystemType: extraTypes_1.VolumeSetDefaultFsType,
},
};
}
/*** Public Methods ***/
handle() {
// Validate K8s persistent volume claim
this.validate();
// Process initial capacity
this.processCapacity();
// Process Performance Class
this.processPerformanceClass();
// Process File System type
this.processFileSystemType();
return this.volumeSet;
}
/*** Private Methods ***/
processCapacity() {
// Extract capacity from persistent volume
if (this.pvc.spec.volumeName) {
const pv = this.mapper.pvNameToObject.get(this.pvc.spec.volumeName);
// Extract capacity
this.extractCapacity(pv.spec.capacity);
return;
}
// Extract capacity from resources
if (this.pvc.spec.resources) {
// Extract capacity
this.extractCapacity(this.pvc.spec.resources.requests);
return;
}
}
processPerformanceClass() {
if (!this.pvc.spec.storageClassName || !this.mapper.storageClassNameToObject.has(this.pvc.spec.storageClassName)) {
return;
}
const storageClass = this.mapper.storageClassNameToObject.get(this.pvc.spec.storageClassName);
// There is nothing to do if there were no parameters
if (!storageClass.parameters) {
return;
}
for (const type of storage_class_1.performanceClassTypes) {
if (!storageClass.parameters.hasOwnProperty(type)) {
continue;
}
// If the storage class type is a known high performance ssd type, then update performance class
if (storage_class_1.knownHighPerformanceSsds.includes(storageClass.parameters[type])) {
this.volumeSet.spec.performanceClass = 'high-throughput-ssd';
break;
}
}
}
processFileSystemType() {
// Extract file system type from persistent volume if specified
if (this.pvc.spec.volumeName) {
const pv = this.mapper.pvNameToObject.get(this.pvc.spec.volumeName);
this.volumeSet.spec.fileSystemType = this.findPvFsType(pv);
return;
}
// Extract file system type from storage class if specified
if (this.pvc.spec.storageClassName && this.mapper.storageClassNameToObject.has(this.pvc.spec.storageClassName)) {
const storageClass = this.mapper.storageClassNameToObject.get(this.pvc.spec.storageClassName);
if (storageClass.parameters && storageClass.parameters.hasOwnProperty('fsType')) {
this.volumeSet.spec.fileSystemType = this.getSafeFsType({ fsType: storageClass.parameters['fsType'] });
return;
}
}
}
// Helpers //
extractCapacity(capacity) {
if (!capacity || !capacity.hasOwnProperty('storage')) {
return;
}
const storage = capacity['storage'];
try {
this.volumeSet.spec.initialCapacity = parseInt(storage, 10);
}
catch (e) {
this.raiseCustomK8sError(`The specified capacity '${storage}' is invalid and cannot be parsed`);
}
}
findPvFsType(pv) {
if (pv.spec.local) {
return this.getSafeFsType(pv.spec.local);
}
if (pv.spec.awsElasticBlockStore) {
return this.getSafeFsType(pv.spec.awsElasticBlockStore);
}
if (pv.spec.azureDisk) {
return this.getSafeFsType(pv.spec.azureDisk);
}
if (pv.spec.cinder) {
return this.getSafeFsType(pv.spec.cinder);
}
if (pv.spec.csi) {
return this.getSafeFsType(pv.spec.csi);
}
if (pv.spec.fc) {
return this.getSafeFsType(pv.spec.fc);
}
if (pv.spec.flexVolume) {
return this.getSafeFsType(pv.spec.flexVolume);
}
if (pv.spec.gcePersistentDisk) {
return this.getSafeFsType(pv.spec.gcePersistentDisk);
}
if (pv.spec.iscsi) {
return this.getSafeFsType(pv.spec.iscsi);
}
if (pv.spec.photonPersistentDisk) {
return this.getSafeFsType(pv.spec.photonPersistentDisk);
}
if (pv.spec.portworxVolume) {
return this.getSafeFsType(pv.spec.portworxVolume);
}
if (pv.spec.rbd) {
return this.getSafeFsType(pv.spec.rbd);
}
if (pv.spec.scaleIO) {
return this.getSafeFsType(pv.spec.scaleIO);
}
if (pv.spec.storageos) {
return this.getSafeFsType(pv.spec.storageos);
}
if (pv.spec.vsphereVolume) {
return this.getSafeFsType(pv.spec.vsphereVolume);
}
return extraTypes_1.VolumeSetDefaultFsType;
}
getSafeFsType(hasFileSystemType) {
if (!hasFileSystemType.fsType || (hasFileSystemType.fsType != 'xfs' && hasFileSystemType.fsType != 'ext4')) {
return extraTypes_1.VolumeSetDefaultFsType;
}
return hasFileSystemType.fsType;
}
// Validators //
validate() {
if (!this.pvc.spec) {
(0, helper_1.ensurePropertyPresence)('spec', this.filePath, this.pvc);
}
// Check if there is a persistent volume with the given name
if (this.pvc.spec.volumeName && !this.mapper.pvNameToObject.has(this.pvc.spec.volumeName)) {
this.raiseCustomK8sError(`The referenced persistent volume '${this.pvc.spec.volumeName}' was not found`);
}
}
raiseCustomK8sError(message) {
(0, helper_1.raiseCustomK8sError)(message, this.filePath, this.pvc);
}
}
exports.K8sPersistentVolumeClaimHandler = K8sPersistentVolumeClaimHandler;
//# sourceMappingURL=pvc.js.map