1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 13:29:41 +02:00
portainer/app/kubernetes/views/volumes/edit/volumeController.js
Chaim Lev-Ari 0f3c7b1424
refactor(home): migrate view to react [EE-1810] (#6314)
* refactor(http): parse axios errors (#6325)

* refactor(home): use endpoint-list as react component [EE-1814] (#6060)

* refactor(home): use endpoint-list as react component

fix(home): add missing features and refactors

- kubebutton
- group name
- poll when endpoint is off
- state management

refactor(endpoints): use stat component

fix(endpoints): add space between items

refactor(endpoints): move stats to components

refactor(endpoints): fetch time

refactor(home): move logic

refactor(home): move fe render logic

refactor(settings): use vanilla js for publicSettings

refactor(kube): remove angular from kube config service

feat(home): add kubeconfig button

feat(home): send analytics when opening kubeconfig modal

fix(home): memoize footer

refactor(home): use react-query for loading

fix(home): show correct control for kubeconfig modal

refactor(home): use debounce

refactor(home): use new components

refactor(home): replace endpoints with environments

refactor(home): move endpoint-list component to home

fix(home): show group name

refactor(home): use switch for environment icon

fix(kubeconfig): fix default case

refactor(axios): use parse axios error

refactor(home): use link components for navigate

fix(home): align azure icon

refactor(home): refactor stats

refactor(home): export envstatusbadge

refactor(home): remove unused bindings

* chore(home): write tests for edge indicator

* chore(home): basic stories for environment item

* style(settings): reformat

* fix(environments): add publicurl

* refactor(home): use table components

* refactor(datatables): merge useSearchBarState

* refactor(home): fetch group in env item

* chore(tests): basic tests

* chore(home): test when no envs

* refactor(tags): use axios for tagService

* refactor(env-groups): use axios for getGroups

* feat(app): ui-state context provider

* refactor(home): create MotdPanel

* refactor(app): create InformationPanel

* feat(endpoints): fetch number of total endpoints

* refactor(app): merge hooks

* refactor(home): migrate view to react [EE-1810]

fixes [EE-1810]

refactor(home): wip use react view

feat(home): show message if no endpoints

refactor(home): show endpoint list

refactor(home): don't use home to manage link

refactor(home): move state

refactor(home): check if edge using util

refactor(home): move inf panels

chore(home): tests

refactor(home): load groups and tags in env-item

refactor(settings): revert publicSettings change

refactor(home): move confirm snapshot method

* fix(home): show tags

* fix(environments): handle missing snapshots

* fix(kube/volumes): fetch pesistent volume claims

* refactor(kube): remove use of endpointProvider

* refactor(endpoints): set current endpoint

* chore(home): add data-cy for tests

* chore(tests): mock axios-progress-bar

* refactor(home): move use env list to env module

* feat(app): sync home view changes with ee

* fix(home): sort page header

* fix(app): fix tests

* chore(github): use yarn cache

* refactor(environments): load list of groups

* chore(babel): remove auto 18n keys extraction

* chore(environments): fix tests

* refactor(k8s/application): use current endpoint

* fix(app/header): add margin to header

* refactor(app): remove unused types

* refactor(app): use rq onError handler

* refactor(home): wrap element with button
2022-03-08 14:14:23 +02:00

222 lines
7.2 KiB
JavaScript

import angular from 'angular';
import _ from 'lodash-es';
import filesizeParser from 'filesize-parser';
import KubernetesVolumeHelper from 'Kubernetes/helpers/volumeHelper';
import KubernetesEventHelper from 'Kubernetes/helpers/eventHelper';
import { KubernetesStorageClassAccessPolicies } from 'Kubernetes/models/storage-class/models';
import KubernetesNamespaceHelper from 'Kubernetes/helpers/namespaceHelper';
class KubernetesVolumeController {
/* @ngInject */
constructor(
$async,
$state,
Notifications,
LocalStorage,
KubernetesVolumeService,
KubernetesEventService,
KubernetesApplicationService,
KubernetesPersistentVolumeClaimService,
ModalService,
KubernetesPodService
) {
this.$async = $async;
this.$state = $state;
this.Notifications = Notifications;
this.LocalStorage = LocalStorage;
this.KubernetesVolumeService = KubernetesVolumeService;
this.KubernetesEventService = KubernetesEventService;
this.KubernetesApplicationService = KubernetesApplicationService;
this.KubernetesPersistentVolumeClaimService = KubernetesPersistentVolumeClaimService;
this.ModalService = ModalService;
this.KubernetesPodService = KubernetesPodService;
this.onInit = this.onInit.bind(this);
this.getVolume = this.getVolume.bind(this);
this.getVolumeAsync = this.getVolumeAsync.bind(this);
this.updateVolumeAsync = this.updateVolumeAsync.bind(this);
this.getEvents = this.getEvents.bind(this);
this.getEventsAsync = this.getEventsAsync.bind(this);
}
selectTab(index) {
this.LocalStorage.storeActiveTab('volume', index);
}
showEditor() {
this.state.showEditorTab = true;
this.selectTab(2);
}
isExternalVolume() {
return KubernetesVolumeHelper.isExternalVolume(this.volume);
}
isSystemNamespace() {
return KubernetesNamespaceHelper.isSystemNamespace(this.volume.ResourcePool.Namespace.Name);
}
isUsed() {
return KubernetesVolumeHelper.isUsed(this.volume);
}
onChangeSize() {
if (this.state.volumeSize) {
const size = filesizeParser(this.state.volumeSize + this.state.volumeSizeUnit, { base: 10 });
if (this.state.oldVolumeSize > size) {
this.state.errors.volumeSize = true;
} else {
this.state.errors.volumeSize = false;
}
}
}
sizeIsValid() {
return !this.state.errors.volumeSize && this.state.volumeSize && this.state.oldVolumeSize !== filesizeParser(this.state.volumeSize + this.state.volumeSizeUnit, { base: 10 });
}
/**
* VOLUME
*/
async updateVolumeAsync(redeploy) {
try {
this.volume.PersistentVolumeClaim.Storage = this.state.volumeSize + this.state.volumeSizeUnit.charAt(0);
await this.KubernetesPersistentVolumeClaimService.patch(this.oldVolume.PersistentVolumeClaim, this.volume.PersistentVolumeClaim);
this.Notifications.success('Volume successfully updated');
if (redeploy) {
const promises = _.flatten(
_.map(this.volume.Applications, (app) => {
return _.map(app.Pods, (item) => this.KubernetesPodService.delete(item));
})
);
await Promise.all(promises);
this.Notifications.success('Applications successfully redeployed');
}
this.$state.reload(this.$state.current);
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to update volume.');
}
}
updateVolume() {
if (KubernetesVolumeHelper.isUsed(this.volume)) {
this.ModalService.confirmRedeploy(
'One or multiple applications are currently using this volume.</br> For the change to be taken into account these applications will need to be redeployed. Do you want us to reschedule it now?',
(redeploy) => {
return this.$async(this.updateVolumeAsync, redeploy);
}
);
} else {
return this.$async(this.updateVolumeAsync, false);
}
}
async getVolumeAsync() {
const storageClasses = this.endpoint.Kubernetes.Configuration.StorageClasses;
try {
const [volume, applications] = await Promise.all([
this.KubernetesVolumeService.get(this.state.namespace, storageClasses, this.state.name),
this.KubernetesApplicationService.get(this.state.namespace),
]);
volume.Applications = KubernetesVolumeHelper.getUsingApplications(volume, applications);
this.volume = volume;
this.oldVolume = angular.copy(volume);
this.state.volumeSize = parseInt(volume.PersistentVolumeClaim.Storage.slice(0, -2), 10);
this.state.volumeSizeUnit = volume.PersistentVolumeClaim.Storage.slice(-2);
this.state.oldVolumeSize = filesizeParser(volume.PersistentVolumeClaim.Storage, { base: 10 });
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to retrieve volume');
}
}
getVolume() {
return this.$async(this.getVolumeAsync);
}
/**
* EVENTS
*/
hasEventWarnings() {
return this.state.eventWarningCount;
}
async getEventsAsync() {
try {
this.state.eventsLoading = true;
const events = await this.KubernetesEventService.get(this.state.namespace);
this.events = _.filter(events, (event) => event.Involved.uid === this.volume.PersistentVolumeClaim.Id);
this.state.eventWarningCount = KubernetesEventHelper.warningCount(this.events);
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to retrieve application related events');
} finally {
this.state.eventsLoading = false;
}
}
getEvents() {
return this.$async(this.getEventsAsync);
}
/**
* ON INIT
*/
async onInit() {
this.state = {
activeTab: 0,
currentName: this.$state.$current.name,
showEditorTab: false,
eventsLoading: true,
viewReady: false,
namespace: this.$transition$.params().namespace,
name: this.$transition$.params().name,
eventWarningCount: 0,
availableSizeUnits: ['MB', 'GB', 'TB'],
increaseSize: false,
volumeSize: 0,
volumeSizeUnit: 'GB',
volumeSharedAccessPolicy: '',
volumeSharedAccessPolicyTooltip: '',
errors: {
volumeSize: false,
},
};
this.state.activeTab = this.LocalStorage.getActiveTab('volume');
try {
await this.getVolume();
await this.getEvents();
if (this.volume.PersistentVolumeClaim.StorageClass !== undefined) {
this.state.volumeSharedAccessPolicy = this.volume.PersistentVolumeClaim.StorageClass.AccessModes[this.volume.PersistentVolumeClaim.StorageClass.AccessModes.length - 1];
let policies = KubernetesStorageClassAccessPolicies();
policies.forEach((policy) => {
if (policy.Name == this.state.volumeSharedAccessPolicy) {
this.state.volumeSharedAccessPolicyTooltip = policy.Description;
}
});
}
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to load view data');
} finally {
this.state.viewReady = true;
}
}
$onInit() {
return this.$async(this.onInit);
}
$onDestroy() {
if (this.state.currentName !== this.$state.$current.name) {
this.LocalStorage.storeActiveTab('volume', 0);
}
}
}
export default KubernetesVolumeController;
angular.module('portainer.kubernetes').controller('KubernetesVolumeController', KubernetesVolumeController);