2020-07-05 23:21:03 +00:00
|
|
|
import _ from 'lodash-es';
|
|
|
|
import filesizeParser from 'filesize-parser';
|
|
|
|
import { KubernetesResourceQuotaDefaults } from 'Kubernetes/models/resource-quota/models';
|
|
|
|
import KubernetesResourceReservationHelper from 'Kubernetes/helpers/resourceReservationHelper';
|
2021-04-27 17:51:13 +00:00
|
|
|
import {
|
|
|
|
KubernetesResourcePoolFormValues,
|
|
|
|
KubernetesResourcePoolIngressClassAnnotationFormValue,
|
|
|
|
KubernetesResourcePoolIngressClassHostFormValue,
|
2022-04-20 04:45:20 +00:00
|
|
|
KubernetesResourcePoolNginxRewriteAnnotationFormValue,
|
|
|
|
KubernetesResourcePoolNginxUseregexAnnotationFormValue,
|
|
|
|
KubernetesResourcePoolTraefikRewriteAnnotationFormValue,
|
2021-04-27 17:51:13 +00:00
|
|
|
} from 'Kubernetes/models/resource-pool/formValues';
|
2020-08-20 00:51:14 +00:00
|
|
|
import { KubernetesIngressConverter } from 'Kubernetes/ingress/converter';
|
|
|
|
import KubernetesFormValidationHelper from 'Kubernetes/helpers/formValidationHelper';
|
2021-02-26 15:50:33 +00:00
|
|
|
import { KubernetesFormValidationReferences } from 'Kubernetes/models/application/formValues';
|
2020-08-20 09:24:12 +00:00
|
|
|
import { KubernetesIngressClassTypes } from 'Kubernetes/ingress/constants';
|
2020-07-05 23:21:03 +00:00
|
|
|
|
2021-12-14 19:14:53 +00:00
|
|
|
import { FeatureId } from '@/portainer/feature-flags/enums';
|
2021-10-06 06:24:26 +00:00
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
class KubernetesCreateResourcePoolController {
|
2020-08-20 00:51:14 +00:00
|
|
|
/* #region CONSTRUCTOR */
|
2020-07-05 23:21:03 +00:00
|
|
|
/* @ngInject */
|
2021-12-14 19:14:53 +00:00
|
|
|
constructor($async, $state, $scope, Notifications, KubernetesNodeService, KubernetesResourcePoolService, KubernetesIngressService, Authentication, EndpointService) {
|
2021-07-14 09:15:21 +00:00
|
|
|
Object.assign(this, {
|
|
|
|
$async,
|
|
|
|
$state,
|
2021-12-14 19:14:53 +00:00
|
|
|
$scope,
|
2021-07-14 09:15:21 +00:00
|
|
|
Notifications,
|
|
|
|
KubernetesNodeService,
|
|
|
|
KubernetesResourcePoolService,
|
|
|
|
KubernetesIngressService,
|
|
|
|
Authentication,
|
|
|
|
EndpointService,
|
|
|
|
});
|
2020-07-05 23:21:03 +00:00
|
|
|
|
2020-08-20 09:24:12 +00:00
|
|
|
this.IngressClassTypes = KubernetesIngressClassTypes;
|
2021-12-14 19:14:53 +00:00
|
|
|
this.LBQuotaFeatureId = FeatureId.K8S_RESOURCE_POOL_LB_QUOTA;
|
|
|
|
|
|
|
|
this.onToggleStorageQuota = this.onToggleStorageQuota.bind(this);
|
|
|
|
this.onToggleLoadBalancerQuota = this.onToggleLoadBalancerQuota.bind(this);
|
2022-08-01 04:45:28 +00:00
|
|
|
this.onToggleResourceQuota = this.onToggleResourceQuota.bind(this);
|
2020-07-05 23:21:03 +00:00
|
|
|
}
|
2020-08-20 00:51:14 +00:00
|
|
|
/* #endregion */
|
2020-07-05 23:21:03 +00:00
|
|
|
|
2021-12-14 19:14:53 +00:00
|
|
|
onToggleStorageQuota(storageClassName, enabled) {
|
|
|
|
this.$scope.$evalAsync(() => {
|
|
|
|
this.formValues.StorageClasses = this.formValues.StorageClasses.map((sClass) => (sClass.Name !== storageClassName ? sClass : { ...sClass, Selected: enabled }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onToggleLoadBalancerQuota(enabled) {
|
|
|
|
this.$scope.$evalAsync(() => {
|
|
|
|
this.formValues.UseLoadBalancersQuota = enabled;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-01 04:45:28 +00:00
|
|
|
onToggleResourceQuota(enabled) {
|
|
|
|
this.$scope.$evalAsync(() => {
|
|
|
|
this.formValues.HasQuota = enabled;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-20 00:51:14 +00:00
|
|
|
onChangeIngressHostname() {
|
|
|
|
const state = this.state.duplicates.ingressHosts;
|
2021-04-27 17:51:13 +00:00
|
|
|
const hosts = _.flatMap(this.formValues.IngressClasses, 'Hosts');
|
2021-09-24 11:02:06 +00:00
|
|
|
const hostnames = _.compact(hosts.map((h) => h.Host));
|
2021-04-27 17:51:13 +00:00
|
|
|
const hostnamesWithoutRemoved = _.filter(hostnames, (h) => !h.NeedsDeletion);
|
|
|
|
const allHosts = _.flatMap(this.allIngresses, 'Hosts');
|
|
|
|
const formDuplicates = KubernetesFormValidationHelper.getDuplicates(hostnamesWithoutRemoved);
|
|
|
|
_.forEach(hostnames, (host, idx) => {
|
|
|
|
if (host !== undefined && _.includes(allHosts, host)) {
|
|
|
|
formDuplicates[idx] = host;
|
2020-08-20 00:51:14 +00:00
|
|
|
}
|
|
|
|
});
|
2021-04-27 17:51:13 +00:00
|
|
|
const duplicates = {};
|
|
|
|
let count = 0;
|
|
|
|
_.forEach(this.formValues.IngressClasses, (ic) => {
|
|
|
|
duplicates[ic.IngressClass.Name] = {};
|
|
|
|
_.forEach(ic.Hosts, (hostFV, hostIdx) => {
|
|
|
|
if (hostFV.Host === formDuplicates[count]) {
|
|
|
|
duplicates[ic.IngressClass.Name][hostIdx] = hostFV.Host;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
});
|
2020-08-20 00:51:14 +00:00
|
|
|
state.refs = duplicates;
|
2021-04-27 17:51:13 +00:00
|
|
|
state.hasRefs = false;
|
|
|
|
_.forIn(duplicates, (value) => {
|
|
|
|
if (Object.keys(value).length > 0) {
|
|
|
|
state.hasRefs = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addHostname(ingressClass) {
|
|
|
|
ingressClass.Hosts.push(new KubernetesResourcePoolIngressClassHostFormValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
removeHostname(ingressClass, index) {
|
|
|
|
ingressClass.Hosts.splice(index, 1);
|
|
|
|
this.onChangeIngressHostname();
|
2020-08-20 00:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* #region ANNOTATIONS MANAGEMENT */
|
|
|
|
addAnnotation(ingressClass) {
|
|
|
|
ingressClass.Annotations.push(new KubernetesResourcePoolIngressClassAnnotationFormValue());
|
|
|
|
}
|
|
|
|
|
2022-04-20 04:45:20 +00:00
|
|
|
addRewriteAnnotation(ingressClass) {
|
|
|
|
if (ingressClass.IngressClass.Type === this.IngressClassTypes.NGINX) {
|
|
|
|
ingressClass.Annotations.push(new KubernetesResourcePoolNginxRewriteAnnotationFormValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ingressClass.IngressClass.Type === this.IngressClassTypes.TRAEFIK) {
|
|
|
|
ingressClass.Annotations.push(new KubernetesResourcePoolTraefikRewriteAnnotationFormValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addUseregexAnnotation(ingressClass) {
|
|
|
|
ingressClass.Annotations.push(new KubernetesResourcePoolNginxUseregexAnnotationFormValue());
|
|
|
|
}
|
|
|
|
|
2020-08-20 00:51:14 +00:00
|
|
|
removeAnnotation(ingressClass, index) {
|
|
|
|
ingressClass.Annotations.splice(index, 1);
|
|
|
|
}
|
|
|
|
/* #endregion */
|
|
|
|
|
|
|
|
isCreateButtonDisabled() {
|
2022-01-25 10:59:09 +00:00
|
|
|
return (
|
|
|
|
this.state.actionInProgress ||
|
|
|
|
(this.formValues.HasQuota && !this.isQuotaValid()) ||
|
|
|
|
this.state.isAlreadyExist ||
|
|
|
|
this.state.hasPrefixKube ||
|
|
|
|
this.state.duplicates.ingressHosts.hasRefs
|
|
|
|
);
|
2020-07-05 23:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onChangeName() {
|
|
|
|
this.state.isAlreadyExist = _.find(this.resourcePools, (resourcePool) => resourcePool.Namespace.Name === this.formValues.Name) !== undefined;
|
2022-01-25 10:59:09 +00:00
|
|
|
this.state.hasPrefixKube = /^kube-/.test(this.formValues.Name);
|
2020-07-05 23:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isQuotaValid() {
|
|
|
|
if (
|
|
|
|
this.state.sliderMaxCpu < this.formValues.CpuLimit ||
|
|
|
|
this.state.sliderMaxMemory < this.formValues.MemoryLimit ||
|
|
|
|
(this.formValues.CpuLimit === 0 && this.formValues.MemoryLimit === 0)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDefaults() {
|
|
|
|
if (this.formValues.CpuLimit < this.defaults.CpuLimit) {
|
|
|
|
this.formValues.CpuLimit = this.defaults.CpuLimit;
|
|
|
|
}
|
|
|
|
if (this.formValues.MemoryLimit < KubernetesResourceReservationHelper.megaBytesValue(this.defaults.MemoryLimit)) {
|
|
|
|
this.formValues.MemoryLimit = KubernetesResourceReservationHelper.megaBytesValue(this.defaults.MemoryLimit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 08:12:34 +00:00
|
|
|
/* #region CREATE NAMESPACE */
|
2020-07-05 23:21:03 +00:00
|
|
|
createResourcePool() {
|
2021-07-14 09:15:21 +00:00
|
|
|
return this.$async(async () => {
|
|
|
|
this.state.actionInProgress = true;
|
|
|
|
try {
|
|
|
|
this.checkDefaults();
|
|
|
|
this.formValues.Owner = this.Authentication.getUserDetails().username;
|
|
|
|
await this.KubernetesResourcePoolService.create(this.formValues);
|
|
|
|
this.Notifications.success('Namespace successfully created', this.formValues.Name);
|
|
|
|
this.$state.go('kubernetes.resourcePools');
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to create namespace');
|
|
|
|
} finally {
|
|
|
|
this.state.actionInProgress = false;
|
|
|
|
}
|
|
|
|
});
|
2020-07-05 23:21:03 +00:00
|
|
|
}
|
2020-08-20 00:51:14 +00:00
|
|
|
/* #endregion */
|
|
|
|
|
|
|
|
/* #region GET INGRESSES */
|
|
|
|
getIngresses() {
|
2021-07-14 09:15:21 +00:00
|
|
|
return this.$async(async () => {
|
|
|
|
try {
|
|
|
|
this.allIngresses = await this.KubernetesIngressService.get();
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to retrieve ingresses.');
|
|
|
|
}
|
|
|
|
});
|
2020-08-20 00:51:14 +00:00
|
|
|
}
|
|
|
|
/* #endregion */
|
2020-07-05 23:21:03 +00:00
|
|
|
|
2021-04-27 08:12:34 +00:00
|
|
|
/* #region GET NAMESPACES */
|
2020-07-05 23:21:03 +00:00
|
|
|
getResourcePools() {
|
2021-07-14 09:15:21 +00:00
|
|
|
return this.$async(async () => {
|
|
|
|
try {
|
|
|
|
this.resourcePools = await this.KubernetesResourcePoolService.get();
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to retrieve namespaces');
|
|
|
|
}
|
|
|
|
});
|
2020-07-05 23:21:03 +00:00
|
|
|
}
|
2020-08-20 00:51:14 +00:00
|
|
|
/* #endregion */
|
2020-07-05 23:21:03 +00:00
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
/* #region GET REGISTRIES */
|
|
|
|
getRegistries() {
|
|
|
|
return this.$async(async () => {
|
|
|
|
try {
|
|
|
|
this.registries = await this.EndpointService.registries(this.endpoint.Id);
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to retrieve registries');
|
2020-08-12 23:30:23 +00:00
|
|
|
}
|
2021-07-14 09:15:21 +00:00
|
|
|
});
|
2020-07-05 23:21:03 +00:00
|
|
|
}
|
2021-07-14 09:15:21 +00:00
|
|
|
/* #endregion */
|
2020-07-05 23:21:03 +00:00
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
/* #region ON INIT */
|
2020-07-05 23:21:03 +00:00
|
|
|
$onInit() {
|
2021-07-14 09:15:21 +00:00
|
|
|
return this.$async(async () => {
|
|
|
|
try {
|
|
|
|
const endpoint = this.endpoint;
|
|
|
|
this.defaults = KubernetesResourceQuotaDefaults;
|
|
|
|
this.formValues = new KubernetesResourcePoolFormValues(this.defaults);
|
|
|
|
this.formValues.EndpointId = this.endpoint.Id;
|
|
|
|
this.formValues.HasQuota = true;
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
actionInProgress: false,
|
|
|
|
sliderMaxMemory: 0,
|
|
|
|
sliderMaxCpu: 0,
|
|
|
|
viewReady: false,
|
|
|
|
isAlreadyExist: false,
|
2022-01-25 10:59:09 +00:00
|
|
|
hasPrefixKube: false,
|
2021-07-14 09:15:21 +00:00
|
|
|
canUseIngress: endpoint.Kubernetes.Configuration.IngressClasses.length,
|
|
|
|
duplicates: {
|
|
|
|
ingressHosts: new KubernetesFormValidationReferences(),
|
|
|
|
},
|
|
|
|
isAdmin: this.Authentication.isAdmin(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const nodes = await this.KubernetesNodeService.get();
|
|
|
|
|
|
|
|
_.forEach(nodes, (item) => {
|
|
|
|
this.state.sliderMaxMemory += filesizeParser(item.Memory);
|
|
|
|
this.state.sliderMaxCpu += item.CPU;
|
|
|
|
});
|
|
|
|
this.state.sliderMaxMemory = KubernetesResourceReservationHelper.megaBytesValue(this.state.sliderMaxMemory);
|
|
|
|
await this.getResourcePools();
|
|
|
|
if (this.state.canUseIngress) {
|
|
|
|
await this.getIngresses();
|
|
|
|
const ingressClasses = endpoint.Kubernetes.Configuration.IngressClasses;
|
|
|
|
this.formValues.IngressClasses = KubernetesIngressConverter.ingressClassesToFormValues(ingressClasses);
|
|
|
|
}
|
|
|
|
_.forEach(this.formValues.IngressClasses, (ic) => {
|
|
|
|
if (ic.Hosts.length === 0) {
|
|
|
|
ic.Hosts.push(new KubernetesResourcePoolIngressClassHostFormValue());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.getRegistries();
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to load view data');
|
|
|
|
} finally {
|
|
|
|
this.state.viewReady = true;
|
|
|
|
}
|
|
|
|
});
|
2020-07-05 23:21:03 +00:00
|
|
|
}
|
2020-08-20 00:51:14 +00:00
|
|
|
/* #endregion */
|
2020-07-05 23:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default KubernetesCreateResourcePoolController;
|