2023-09-20 07:33:43 +00:00
|
|
|
import Route from '@/assets/ico/route.svg?c';
|
2022-09-26 19:43:24 +00:00
|
|
|
|
2023-02-14 08:19:41 +00:00
|
|
|
import { confirm } from '@@/modals/confirm';
|
|
|
|
import { ModalType } from '@@/modals';
|
2022-09-26 19:43:24 +00:00
|
|
|
import { Datatable } from '@@/datatables';
|
|
|
|
import { Button, ButtonGroup } from '@@/buttons';
|
2022-11-22 12:16:34 +00:00
|
|
|
import { createPersistedStore } from '@@/datatables/types';
|
2023-02-14 08:19:41 +00:00
|
|
|
import { buildConfirmButton } from '@@/modals/utils';
|
2023-05-02 06:42:16 +00:00
|
|
|
import { useTableState } from '@@/datatables/useTableState';
|
2023-09-20 07:33:43 +00:00
|
|
|
import { TextTip } from '@@/Tip/TextTip';
|
2022-09-26 19:43:24 +00:00
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
import { IngressControllerClassMap } from '../types';
|
2022-09-26 19:43:24 +00:00
|
|
|
|
2023-05-02 06:42:16 +00:00
|
|
|
import { columns } from './columns';
|
2022-09-26 19:43:24 +00:00
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
const storageKey = 'ingressClasses';
|
2023-09-04 09:33:07 +00:00
|
|
|
const settingsStore = createPersistedStore(storageKey, 'name');
|
2022-09-26 19:43:24 +00:00
|
|
|
|
|
|
|
interface Props {
|
2023-10-11 19:32:02 +00:00
|
|
|
onChange: (controllerClassMap: IngressControllerClassMap[]) => void; // angular function to save the ingress class list
|
2022-09-26 19:43:24 +00:00
|
|
|
description: string;
|
2023-10-11 19:32:02 +00:00
|
|
|
values: IngressControllerClassMap[] | undefined;
|
|
|
|
initialValues: IngressControllerClassMap[] | undefined;
|
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({
|
2023-10-11 19:32:02 +00:00
|
|
|
onChange,
|
2022-09-26 19:43:24 +00:00
|
|
|
description,
|
2023-10-11 19:32:02 +00:00
|
|
|
initialValues,
|
|
|
|
values,
|
2022-10-03 23:13:56 +00:00
|
|
|
isLoading,
|
2022-09-26 19:43:24 +00:00
|
|
|
noIngressControllerLabel,
|
|
|
|
view,
|
|
|
|
}: Props) {
|
2023-05-02 06:42:16 +00:00
|
|
|
const tableState = useTableState(settingsStore, storageKey);
|
|
|
|
|
2022-09-26 19:43:24 +00:00
|
|
|
return (
|
|
|
|
<div className="-mx-[15px]">
|
|
|
|
<Datatable
|
2023-05-02 06:42:16 +00:00
|
|
|
settingsManager={tableState}
|
2023-10-11 19:32:02 +00:00
|
|
|
dataset={values || []}
|
2022-09-26 19:43:24 +00:00
|
|
|
columns={columns}
|
2022-10-03 23:13:56 +00:00
|
|
|
isLoading={isLoading}
|
2022-09-26 19:43:24 +00:00
|
|
|
emptyContentLabel={noIngressControllerLabel}
|
2022-11-22 12:16:34 +00:00
|
|
|
title="Ingress Controllers"
|
2023-09-20 07:33:43 +00:00
|
|
|
titleIcon={Route}
|
2022-09-26 19:43:24 +00:00
|
|
|
getRowId={(row) => `${row.Name}-${row.ClassName}-${row.Type}`}
|
|
|
|
renderTableActions={(selectedRows) => renderTableActions(selectedRows)}
|
|
|
|
description={renderIngressClassDescription()}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
function renderTableActions(selectedRows: IngressControllerClassMap[]) {
|
2022-09-26 19:43:24 +00:00
|
|
|
return (
|
|
|
|
<div className="flex items-start">
|
|
|
|
<ButtonGroup>
|
|
|
|
<Button
|
|
|
|
disabled={
|
|
|
|
selectedRows.filter((row) => row.Availability === true).length ===
|
|
|
|
0
|
|
|
|
}
|
|
|
|
color="dangerlight"
|
|
|
|
size="small"
|
|
|
|
onClick={() =>
|
2023-10-11 19:32:02 +00:00
|
|
|
updateIngressControllers(selectedRows, values || [], false)
|
2022-09-26 19:43:24 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
Disallow selected
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
disabled={
|
|
|
|
selectedRows.filter((row) => row.Availability === false)
|
|
|
|
.length === 0
|
|
|
|
}
|
|
|
|
color="default"
|
|
|
|
size="small"
|
|
|
|
onClick={() =>
|
2023-10-11 19:32:02 +00:00
|
|
|
updateIngressControllers(selectedRows, values || [], true)
|
2022-09-26 19:43:24 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
Allow selected
|
|
|
|
</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderIngressClassDescription() {
|
|
|
|
return (
|
2023-02-12 21:04:24 +00:00
|
|
|
<div className="text-muted flex w-full flex-col !text-xs">
|
2022-09-26 19:43:24 +00:00
|
|
|
<div className="mt-1">{description}</div>
|
2023-10-11 19:32:02 +00:00
|
|
|
{initialValues && values && isUnsavedChanges(initialValues, values) && (
|
|
|
|
<TextTip>Unsaved changes.</TextTip>
|
|
|
|
)}
|
2022-09-26 19:43:24 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-14 08:19:41 +00:00
|
|
|
async function updateIngressControllers(
|
2023-10-11 19:32:02 +00:00
|
|
|
selectedRows: IngressControllerClassMap[],
|
|
|
|
values: IngressControllerClassMap[],
|
2022-09-26 19:43:24 +00:00
|
|
|
availability: boolean
|
|
|
|
) {
|
|
|
|
const updatedIngressControllers = getUpdatedIngressControllers(
|
|
|
|
selectedRows,
|
2023-10-11 19:32:02 +00:00
|
|
|
values || [],
|
2022-09-26 19:43:24 +00:00
|
|
|
availability
|
|
|
|
);
|
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
if (values && values.length) {
|
2022-09-26 19:43:24 +00:00
|
|
|
const newAllowed = updatedIngressControllers.map(
|
|
|
|
(ingController) => ingController.Availability
|
|
|
|
);
|
|
|
|
if (view === 'namespace') {
|
2023-10-11 19:32:02 +00:00
|
|
|
onChange(updatedIngressControllers);
|
2022-09-26 19:43:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
const usedControllersToDisallow = values.filter(
|
2022-09-26 19:43:24 +00:00
|
|
|
(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) {
|
2023-02-14 08:19:41 +00:00
|
|
|
const confirmed = await confirm({
|
2022-09-26 19:43:24 +00:00
|
|
|
title: 'Disallow in-use ingress controllers?',
|
2023-02-14 08:19:41 +00:00
|
|
|
modalType: ModalType.Warn,
|
|
|
|
message: (
|
2022-09-26 19:43:24 +00:00
|
|
|
<div>
|
2023-02-14 08:19:41 +00:00
|
|
|
<p>
|
|
|
|
There are ingress controllers you want to disallow that are in
|
|
|
|
use:
|
|
|
|
</p>
|
|
|
|
<ul className="ml-6">
|
|
|
|
{usedControllersToDisallow.map((controller) => (
|
2023-08-03 11:03:09 +00:00
|
|
|
<li key={controller.ClassName}>{controller.ClassName}</li>
|
2023-02-14 08:19:41 +00:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
<p>
|
|
|
|
No new ingress rules can be created for the disallowed
|
|
|
|
controllers.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
confirmButton: buildConfirmButton('Disallow', 'warning'),
|
2022-09-26 19:43:24 +00:00
|
|
|
});
|
2023-02-14 08:19:41 +00:00
|
|
|
|
|
|
|
if (!confirmed) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-26 19:43:24 +00:00
|
|
|
}
|
2023-10-11 19:32:02 +00:00
|
|
|
onChange(updatedIngressControllers);
|
2022-09-26 19:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isUnsavedChanges(
|
2023-10-11 19:32:02 +00:00
|
|
|
oldIngressControllers: IngressControllerClassMap[],
|
|
|
|
newIngressControllers: IngressControllerClassMap[]
|
2022-09-26 19:43:24 +00:00
|
|
|
) {
|
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(
|
2023-10-11 19:32:02 +00:00
|
|
|
selectedRows: IngressControllerClassMap[],
|
|
|
|
allRows: IngressControllerClassMap[],
|
2022-09-26 19:43:24 +00:00
|
|
|
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;
|
|
|
|
}
|