1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 13:29:41 +02:00
portainer/app/kubernetes/converters/persistentVolumeClaim.js
Maxime Bajeux 61f97469ab
feat(application): Add the ability to use existing volumes when creating an application (#4044)
* feat(applications): update UI to use existing volumes

* feat(application): Add the ability to use existing volumes when creating an application

* feat(application): Existing persisted folders should default to associated volumes

* feat(application): add form validation to existing volume

* feat(application): remove the ability to use an existing volume with statefulset application

* feat(k8s/applications): minor UI update

* feat(k8s/application): minor UI update

* feat(volume): allow to increase volume size and few other things

* feat(volumes): add the ability to allow volume expansion

* fix(storage): fix the storage patch request

* fix(k8s/applications): remove conflict leftover

* feat(k8s/configure): minor UI update

* feat(k8s/volume): minor UI update

* fix(storage): change few things

Co-authored-by: Anthony Lapenna <lapenna.anthony@gmail.com>
2020-08-07 16:40:24 +12:00

80 lines
3.5 KiB
JavaScript

import _ from 'lodash-es';
import * as JsonPatch from 'fast-json-patch';
import { KubernetesPersistentVolumeClaim } from 'Kubernetes/models/volume/models';
import { KubernetesPersistentVolumClaimCreatePayload } from 'Kubernetes/models/volume/payloads';
import { KubernetesPortainerApplicationOwnerLabel, KubernetesPortainerApplicationNameLabel } from 'Kubernetes/models/application/models';
class KubernetesPersistentVolumeClaimConverter {
static apiToPersistentVolumeClaim(data, storageClasses, yaml) {
const res = new KubernetesPersistentVolumeClaim();
res.Id = data.metadata.uid;
res.Name = data.metadata.name;
res.Namespace = data.metadata.namespace;
res.CreationDate = data.metadata.creationTimestamp;
res.Storage = data.spec.resources.requests.storage.replace('i', 'B');
res.StorageClass = _.find(storageClasses, { Name: data.spec.storageClassName });
res.Yaml = yaml ? yaml.data : '';
res.ApplicationOwner = data.metadata.labels ? data.metadata.labels[KubernetesPortainerApplicationOwnerLabel] : '';
res.ApplicationName = data.metadata.labels ? data.metadata.labels[KubernetesPortainerApplicationNameLabel] : '';
return res;
}
/**
* Generate KubernetesPersistentVolumeClaim list from KubernetesApplicationFormValues
* @param {KubernetesApplicationFormValues} formValues
*/
static applicationFormValuesToVolumeClaims(formValues) {
_.remove(formValues.PersistedFolders, (item) => item.NeedsDeletion);
const res = _.map(formValues.PersistedFolders, (item) => {
const pvc = new KubernetesPersistentVolumeClaim();
if (!_.isEmpty(item.ExistingVolume)) {
const existantPVC = item.ExistingVolume.PersistentVolumeClaim;
pvc.Name = existantPVC.Name;
if (item.PersistentVolumeClaimName) {
pvc.PreviousName = item.PersistentVolumeClaimName;
}
pvc.StorageClass = existantPVC.StorageClass;
pvc.Storage = existantPVC.Storage.charAt(0) + 'i';
pvc.CreationDate = existantPVC.CreationDate;
pvc.Id = existantPVC.Id;
} else {
if (item.PersistentVolumeClaimName) {
pvc.Name = item.PersistentVolumeClaimName;
pvc.PreviousName = item.PersistentVolumeClaimName;
} else {
pvc.Name = formValues.Name + '-' + pvc.Name;
}
pvc.Storage = '' + item.Size + item.SizeUnit.charAt(0) + 'i';
pvc.StorageClass = item.StorageClass;
}
pvc.MountPath = item.ContainerPath;
pvc.Namespace = formValues.ResourcePool.Namespace.Name;
pvc.ApplicationOwner = formValues.ApplicationOwner;
pvc.ApplicationName = formValues.Name;
return pvc;
});
return res;
}
static createPayload(pvc) {
const res = new KubernetesPersistentVolumClaimCreatePayload();
res.metadata.name = pvc.Name;
res.metadata.namespace = pvc.Namespace;
res.spec.resources.requests.storage = pvc.Storage;
res.spec.storageClassName = pvc.StorageClass.Name;
res.metadata.labels.app = pvc.ApplicationName;
res.metadata.labels[KubernetesPortainerApplicationOwnerLabel] = pvc.ApplicationOwner;
res.metadata.labels[KubernetesPortainerApplicationNameLabel] = pvc.ApplicationName;
return res;
}
static patchPayload(oldPVC, newPVC) {
const oldPayload = KubernetesPersistentVolumeClaimConverter.createPayload(oldPVC);
const newPayload = KubernetesPersistentVolumeClaimConverter.createPayload(newPVC);
const payload = JsonPatch.compare(oldPayload, newPayload);
return payload;
}
}
export default KubernetesPersistentVolumeClaimConverter;