2022-10-24 20:41:30 +00:00
|
|
|
import { useEffect, useState } from 'react';
|
2022-09-26 19:43:24 +00:00
|
|
|
|
|
|
|
import { confirmWarn } from '@/portainer/services/modal.service/confirm';
|
|
|
|
|
|
|
|
import { Datatable } from '@@/datatables';
|
|
|
|
import { Button, ButtonGroup } from '@@/buttons';
|
|
|
|
import { Icon } from '@@/Icon';
|
|
|
|
|
|
|
|
import { IngressControllerClassMap } from '../types';
|
|
|
|
|
|
|
|
import { useColumns } from './columns';
|
|
|
|
import { createStore } from './datatable-store';
|
|
|
|
|
|
|
|
const useStore = createStore('ingressClasses');
|
|
|
|
|
|
|
|
interface Props {
|
2022-10-24 20:41:30 +00:00
|
|
|
onChangeControllers: (
|
2022-09-26 19:43:24 +00:00
|
|
|
controllerClassMap: IngressControllerClassMap[]
|
|
|
|
) => void; // angular function to save the ingress class list
|
|
|
|
description: string;
|
|
|
|
ingressControllers: IngressControllerClassMap[] | undefined;
|
2022-10-24 20:41:30 +00:00
|
|
|
allowNoneIngressClass: boolean;
|
2022-10-03 23:13:56 +00:00
|
|
|
isLoading: boolean;
|
2022-09-26 19:43:24 +00:00
|
|
|
noIngressControllerLabel: string;
|
|
|
|
view: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function IngressClassDatatable({
|
2022-10-24 20:41:30 +00:00
|
|
|
onChangeControllers,
|
2022-09-26 19:43:24 +00:00
|
|
|
description,
|
|
|
|
ingressControllers,
|
2022-10-24 20:41:30 +00:00
|
|
|
allowNoneIngressClass,
|
2022-10-03 23:13:56 +00:00
|
|
|
isLoading,
|
2022-09-26 19:43:24 +00:00
|
|
|
noIngressControllerLabel,
|
|
|
|
view,
|
|
|
|
}: Props) {
|
2022-10-24 20:41:30 +00:00
|
|
|
const [ingControllerFormValues, setIngControllerFormValues] = useState(
|
|
|
|
ingressControllers || []
|
|
|
|
);
|
2022-09-26 19:43:24 +00:00
|
|
|
const settings = useStore();
|
|
|
|
const columns = useColumns();
|
|
|
|
|
2022-10-24 20:41:30 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (allowNoneIngressClass === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let newIngFormValues: IngressControllerClassMap[];
|
|
|
|
const isCustomTypeExist = ingControllerFormValues.some(
|
|
|
|
(ic) => ic.Type === 'custom'
|
|
|
|
);
|
|
|
|
if (allowNoneIngressClass) {
|
|
|
|
newIngFormValues = [...ingControllerFormValues];
|
|
|
|
// add the ingress controller type 'custom' with a 'none' ingress class name
|
|
|
|
if (!isCustomTypeExist) {
|
|
|
|
newIngFormValues.push({
|
|
|
|
Name: 'none',
|
|
|
|
ClassName: 'none',
|
|
|
|
Type: 'custom',
|
|
|
|
Availability: true,
|
|
|
|
New: false,
|
|
|
|
Used: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newIngFormValues = ingControllerFormValues.filter(
|
|
|
|
(ingController) => ingController.ClassName !== 'none'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
setIngControllerFormValues(newIngFormValues);
|
|
|
|
onChangeControllers(newIngFormValues);
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [allowNoneIngressClass, onChangeControllers]);
|
|
|
|
|
2022-09-26 19:43:24 +00:00
|
|
|
return (
|
|
|
|
<div className="-mx-[15px]">
|
|
|
|
<Datatable
|
|
|
|
dataset={ingControllerFormValues || []}
|
|
|
|
storageKey="ingressClasses"
|
|
|
|
columns={columns}
|
|
|
|
settingsStore={settings}
|
2022-10-03 23:13:56 +00:00
|
|
|
isLoading={isLoading}
|
2022-09-26 19:43:24 +00:00
|
|
|
emptyContentLabel={noIngressControllerLabel}
|
|
|
|
titleOptions={{
|
|
|
|
icon: 'database',
|
|
|
|
title: 'Ingress controllers',
|
|
|
|
featherIcon: true,
|
|
|
|
}}
|
|
|
|
getRowId={(row) => `${row.Name}-${row.ClassName}-${row.Type}`}
|
|
|
|
renderTableActions={(selectedRows) => renderTableActions(selectedRows)}
|
|
|
|
description={renderIngressClassDescription()}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
function renderTableActions(selectedRows: IngressControllerClassMap[]) {
|
|
|
|
return (
|
|
|
|
<div className="flex items-start">
|
|
|
|
<ButtonGroup>
|
|
|
|
<Button
|
|
|
|
disabled={
|
|
|
|
selectedRows.filter((row) => row.Availability === true).length ===
|
|
|
|
0
|
|
|
|
}
|
|
|
|
color="dangerlight"
|
|
|
|
size="small"
|
|
|
|
onClick={() =>
|
|
|
|
updateIngressControllers(
|
|
|
|
selectedRows,
|
|
|
|
ingControllerFormValues || [],
|
|
|
|
false
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Disallow selected
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
disabled={
|
|
|
|
selectedRows.filter((row) => row.Availability === false)
|
|
|
|
.length === 0
|
|
|
|
}
|
|
|
|
color="default"
|
|
|
|
size="small"
|
|
|
|
onClick={() =>
|
|
|
|
updateIngressControllers(
|
|
|
|
selectedRows,
|
|
|
|
ingControllerFormValues || [],
|
|
|
|
true
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Allow selected
|
|
|
|
</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderIngressClassDescription() {
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col !text-xs text-muted w-full">
|
|
|
|
<div className="mt-1">{description}</div>
|
|
|
|
{ingressControllers &&
|
|
|
|
ingControllerFormValues &&
|
|
|
|
isUnsavedChanges(ingressControllers, ingControllerFormValues) && (
|
|
|
|
<span className="flex items-center text-warning mt-1">
|
|
|
|
<Icon icon="alert-triangle" feather className="!mr-1" />
|
|
|
|
<span className="text-warning">Unsaved changes.</span>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateIngressControllers(
|
|
|
|
selectedRows: IngressControllerClassMap[],
|
|
|
|
ingControllerFormValues: IngressControllerClassMap[],
|
|
|
|
availability: boolean
|
|
|
|
) {
|
|
|
|
const updatedIngressControllers = getUpdatedIngressControllers(
|
|
|
|
selectedRows,
|
|
|
|
ingControllerFormValues || [],
|
|
|
|
availability
|
|
|
|
);
|
|
|
|
|
|
|
|
if (ingressControllers && ingressControllers.length) {
|
|
|
|
const newAllowed = updatedIngressControllers.map(
|
|
|
|
(ingController) => ingController.Availability
|
|
|
|
);
|
|
|
|
if (view === 'namespace') {
|
|
|
|
setIngControllerFormValues(updatedIngressControllers);
|
2022-10-24 20:41:30 +00:00
|
|
|
onChangeControllers(updatedIngressControllers);
|
2022-09-26 19:43:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const usedControllersToDisallow = ingressControllers.filter(
|
|
|
|
(ingController, index) => {
|
|
|
|
// if any of the current controllers are allowed, and are used, then become disallowed, then add the controller to a new list
|
|
|
|
if (
|
|
|
|
ingController.Availability &&
|
|
|
|
ingController.Used &&
|
|
|
|
!newAllowed[index]
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (usedControllersToDisallow.length > 0) {
|
|
|
|
const usedControllerHtmlListItems = usedControllersToDisallow.map(
|
|
|
|
(controller) => `<li>${controller.ClassName}</li>`
|
|
|
|
);
|
|
|
|
const usedControllerHtmlList = `<ul class="ml-6">${usedControllerHtmlListItems.join(
|
|
|
|
''
|
|
|
|
)}</ul>`;
|
|
|
|
confirmWarn({
|
|
|
|
title: 'Disallow in-use ingress controllers?',
|
|
|
|
message: `
|
|
|
|
<div>
|
|
|
|
<p>There are ingress controllers you want to disallow that are in use:</p>
|
|
|
|
${usedControllerHtmlList}
|
|
|
|
<p>No new ingress rules can be created for the disallowed controllers.</p>
|
|
|
|
</div>`,
|
|
|
|
buttons: {
|
|
|
|
cancel: {
|
|
|
|
label: 'Cancel',
|
|
|
|
className: 'btn-default',
|
|
|
|
},
|
|
|
|
confirm: {
|
|
|
|
label: 'Disallow',
|
|
|
|
className: 'btn-warning',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
callback: (confirmed) => {
|
|
|
|
if (confirmed) {
|
|
|
|
setIngControllerFormValues(updatedIngressControllers);
|
2022-10-24 20:41:30 +00:00
|
|
|
onChangeControllers(updatedIngressControllers);
|
2022-09-26 19:43:24 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIngControllerFormValues(updatedIngressControllers);
|
2022-10-24 20:41:30 +00:00
|
|
|
onChangeControllers(updatedIngressControllers);
|
2022-09-26 19:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isUnsavedChanges(
|
|
|
|
oldIngressControllers: IngressControllerClassMap[],
|
|
|
|
newIngressControllers: IngressControllerClassMap[]
|
|
|
|
) {
|
2022-10-24 20:41:30 +00:00
|
|
|
if (oldIngressControllers.length !== newIngressControllers.length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < newIngressControllers.length; i += 1) {
|
2022-09-26 19:43:24 +00:00
|
|
|
if (
|
2022-10-24 20:41:30 +00:00
|
|
|
oldIngressControllers[i]?.Availability !==
|
|
|
|
newIngressControllers[i]?.Availability
|
2022-09-26 19:43:24 +00:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUpdatedIngressControllers(
|
|
|
|
selectedRows: IngressControllerClassMap[],
|
|
|
|
allRows: IngressControllerClassMap[],
|
|
|
|
allow: boolean
|
|
|
|
) {
|
|
|
|
const selectedRowClassNames = selectedRows.map((row) => row.ClassName);
|
|
|
|
const updatedIngressControllers = allRows?.map((row) => {
|
|
|
|
if (selectedRowClassNames.includes(row.ClassName)) {
|
|
|
|
return { ...row, Availability: allow };
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
return updatedIngressControllers;
|
|
|
|
}
|