1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-22 14:59:41 +02:00

feat(k8s/node): Add the ability to apply taints and labels to nodes (#4176)

* feat(node): Add the ability to apply taints and labels to nodes

* feat(k8s/node): minor UI update

* feat(k8s/node): UI update and disable system labels

* feat(k8s/node): minor UI update

* fix(node): fix add first taint

* refacto(node): add KubernetesNodeHelper

* feat(node): add used label to labels

* feat(node): add node update modals

* fix(node): modal when used label changes

* feat(k8s/node): minor UI update

Co-authored-by: Anthony Lapenna <lapenna.anthony@gmail.com>
This commit is contained in:
Maxime Bajeux 2020-08-12 01:42:55 +02:00 committed by GitHub
parent 1f614ee95a
commit 1bf97426bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 568 additions and 8 deletions

View file

@ -3,6 +3,11 @@ import _ from 'lodash-es';
import KubernetesResourceReservationHelper from 'Kubernetes/helpers/resourceReservationHelper';
import { KubernetesResourceReservation } from 'Kubernetes/models/resource-reservation/models';
import KubernetesEventHelper from 'Kubernetes/helpers/eventHelper';
import KubernetesNodeConverter from 'Kubernetes/node/converter';
import { KubernetesNodeLabelFormValues, KubernetesNodeTaintFormValues } from 'Kubernetes/node/formValues';
import { KubernetesNodeTaintEffects } from 'Kubernetes/node/models';
import KubernetesFormValidationHelper from 'Kubernetes/helpers/formValidationHelper';
import { KubernetesNodeHelper } from 'Kubernetes/node/helper';
class KubernetesNodeController {
/* @ngInject */
@ -11,6 +16,7 @@ class KubernetesNodeController {
$state,
Notifications,
LocalStorage,
ModalService,
KubernetesNodeService,
KubernetesEventService,
KubernetesPodService,
@ -21,6 +27,7 @@ class KubernetesNodeController {
this.$state = $state;
this.Notifications = Notifications;
this.LocalStorage = LocalStorage;
this.ModalService = ModalService;
this.KubernetesNodeService = KubernetesNodeService;
this.KubernetesEventService = KubernetesEventService;
this.KubernetesPodService = KubernetesPodService;
@ -33,12 +40,136 @@ class KubernetesNodeController {
this.getEventsAsync = this.getEventsAsync.bind(this);
this.getApplicationsAsync = this.getApplicationsAsync.bind(this);
this.getEndpointsAsync = this.getEndpointsAsync.bind(this);
this.updateNodeAsync = this.updateNodeAsync.bind(this);
}
selectTab(index) {
this.LocalStorage.storeActiveTab('node', index);
}
/* #region taint */
onChangeTaintKey(index) {
this.state.duplicateTaintKeys = KubernetesFormValidationHelper.getDuplicates(
_.map(this.formValues.Taints, (taint) => {
if (taint.NeedsDeletion) {
return undefined;
}
return taint.Key;
})
);
this.state.hasDuplicateTaintKeys = Object.keys(this.state.duplicateTaintKeys).length > 0;
this.onChangeTaint(index);
}
onChangeTaint(index) {
if (this.formValues.Taints[index]) {
this.formValues.Taints[index].IsChanged = true;
}
}
addTaint() {
const taint = new KubernetesNodeTaintFormValues();
taint.IsNew = true;
taint.Effect = KubernetesNodeTaintEffects.NOSCHEDULE;
this.formValues.Taints.push(taint);
}
removeTaint(index) {
const taint = this.formValues.Taints[index];
if (taint.IsNew) {
this.formValues.Taints.splice(index, 1);
} else {
taint.NeedsDeletion = true;
}
this.onChangeTaintKey();
}
restoreTaint(index) {
this.formValues.Taints[index].NeedsDeletion = false;
this.onChangeTaintKey();
}
computeTaintsWarning() {
return _.filter(this.formValues.Taints, (taint) => {
return taint.Effect === KubernetesNodeTaintEffects.NOEXECUTE && (taint.IsNew || taint.IsChanged);
}).length;
}
/* #endregion */
/* #region label */
onChangeLabelKey(index) {
this.state.duplicateLabelKeys = KubernetesFormValidationHelper.getDuplicates(
_.map(this.formValues.Labels, (label) => {
if (label.NeedsDeletion) {
return undefined;
}
return label.Key;
})
);
this.state.hasDuplicateLabelKeys = Object.keys(this.state.duplicateLabelKeys).length > 0;
this.onChangeLabel(index);
}
onChangeLabel(index) {
if (this.formValues.Labels[index]) {
this.formValues.Labels[index].IsChanged = true;
}
}
addLabel() {
const label = new KubernetesNodeLabelFormValues();
label.IsNew = true;
this.formValues.Labels.push(label);
}
removeLabel(index) {
const label = this.formValues.Labels[index];
if (label.IsNew) {
this.formValues.Labels.splice(index, 1);
} else {
label.NeedsDeletion = true;
}
this.onChangeLabelKey();
}
restoreLabel(index) {
this.formValues.Labels[index].NeedsDeletion = false;
this.onChangeLabelKey();
}
isSystemLabel(index) {
return KubernetesNodeHelper.isSystemLabel(this.formValues.Labels[index]);
}
computeLabelsWarning() {
return _.filter(this.formValues.Labels, (label) => {
return label.IsUsed && (label.NeedsDeletion || label.IsChanged);
}).length;
}
/* #endregion */
/* #region actions */
isNoChangesMade() {
const newNode = KubernetesNodeConverter.formValuesToNode(this.node, this.formValues);
const payload = KubernetesNodeConverter.patchPayload(this.node, newNode);
return !payload.length;
}
isFormValid() {
return !this.state.hasDuplicateTaintKeys && !this.state.hasDuplicateLabelKeys && !this.isNoChangesMade();
}
resetFormValues() {
this.formValues = KubernetesNodeConverter.nodeToFormValues(this.node);
}
/* #endregion */
async getEndpointsAsync() {
try {
const endpoints = await this.KubernetesEndpointService.get();
@ -63,6 +194,52 @@ class KubernetesNodeController {
return this.$async(this.getEndpointsAsync);
}
async updateNodeAsync() {
try {
await this.KubernetesNodeService.patch(this.node, this.formValues);
this.Notifications.success('Node updated successfully');
this.$state.reload();
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to update node');
}
}
updateNode() {
const taintsWarning = this.computeTaintsWarning();
const labelsWarning = this.computeLabelsWarning();
if (taintsWarning && !labelsWarning) {
this.ModalService.confirmUpdate(
'Changes to taints will immediately deschedule applications running on this node without the corresponding tolerations. Do you wish to continue?',
(confirmed) => {
if (confirmed) {
return this.$async(this.updateNodeAsync);
}
}
);
} else if (!taintsWarning && labelsWarning) {
this.ModalService.confirmUpdate(
'Removing or changing a label that is used might prevent applications from being scheduled on this node in the future. Do you wish to continue?',
(confirmed) => {
if (confirmed) {
return this.$async(this.updateNodeAsync);
}
}
);
} else if (taintsWarning && labelsWarning) {
this.ModalService.confirmUpdate(
'Changes to taints will immediately deschedule applications running on this node without the corresponding tolerations.<br/></br/>Removing or changing a label that is used might prevent applications from scheduling on this node in the future.\n\nDo you wish to continue?',
(confirmed) => {
if (confirmed) {
return this.$async(this.updateNodeAsync);
}
}
);
} else {
return this.$async(this.updateNodeAsync);
}
}
async getNodeAsync() {
try {
this.state.dataLoading = true;
@ -147,6 +324,10 @@ class KubernetesNodeController {
showEditorTab: false,
viewReady: false,
eventWarningCount: 0,
duplicateTaintKeys: [],
hasDuplicateTaintKeys: false,
duplicateLabelKeys: [],
hasDuplicateLabelKeys: false,
};
this.state.activeTab = this.LocalStorage.getActiveTab('node');
@ -156,6 +337,11 @@ class KubernetesNodeController {
await this.getApplications();
await this.getEndpoints();
this.availableEffects = _.values(KubernetesNodeTaintEffects);
this.formValues = KubernetesNodeConverter.nodeToFormValues(this.node);
this.formValues.Labels = KubernetesNodeHelper.reorderLabels(this.formValues.Labels);
this.formValues.Labels = KubernetesNodeHelper.computeUsedLabels(this.applications, this.formValues.Labels);
this.state.viewReady = true;
}