mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 20:35:25 +02:00
refactor(k8s): namespace core logic (#12142)
Co-authored-by: testA113 <aliharriss1995@gmail.com> Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io> Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com> Co-authored-by: Ali <83188384+testA113@users.noreply.github.com>
This commit is contained in:
parent
da010f3d08
commit
ea228c3d6d
276 changed files with 9241 additions and 3361 deletions
|
@ -42,7 +42,7 @@ export function KubeServicesForm({
|
|||
|
||||
// start loading ingresses and controllers early to reduce perceived loading time
|
||||
const environmentId = useEnvironmentId();
|
||||
useIngresses(environmentId, namespace ? [namespace] : []);
|
||||
useIngresses(environmentId, { withServices: true });
|
||||
useIngressControllers(environmentId, namespace);
|
||||
|
||||
// when the appName changes, update the names for each service
|
||||
|
|
|
@ -39,10 +39,7 @@ export function AppIngressPathsForm({
|
|||
isEditMode,
|
||||
}: Props) {
|
||||
const environmentId = useEnvironmentId();
|
||||
const ingressesQuery = useIngresses(
|
||||
environmentId,
|
||||
namespace ? [namespace] : undefined
|
||||
);
|
||||
const ingressesQuery = useIngresses(environmentId);
|
||||
const { data: ingresses } = ingressesQuery;
|
||||
const { data: ingressControllers, ...ingressControllersQuery } =
|
||||
useIngressControllers(environmentId, namespace);
|
||||
|
|
|
@ -19,7 +19,9 @@ export function ApplicationIngressesTable({
|
|||
namespace,
|
||||
appServices,
|
||||
}: Props) {
|
||||
const namespaceIngresses = useIngresses(environmentId, [namespace]);
|
||||
const namespaceIngresses = useIngresses(environmentId, {
|
||||
withServices: true,
|
||||
});
|
||||
// getIngressPathsForAppServices could be expensive, so memoize it
|
||||
const ingressPathsForAppServices = useMemo(
|
||||
() => getIngressPathsForAppServices(namespaceIngresses.data, appServices),
|
||||
|
|
|
@ -9,6 +9,7 @@ import { useNamespacesQuery } from '@/react/kubernetes/namespaces/queries/useNam
|
|||
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
||||
import { useCurrentEnvironment } from '@/react/hooks/useCurrentEnvironment';
|
||||
import { useAuthorizations } from '@/react/hooks/useUser';
|
||||
import { isSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace';
|
||||
|
||||
import { TableSettingsMenu } from '@@/datatables';
|
||||
import { useRepeater } from '@@/datatables/useRepeater';
|
||||
|
@ -18,6 +19,7 @@ import { ExpandableDatatable } from '@@/datatables/ExpandableDatatable';
|
|||
|
||||
import { NamespaceFilter } from '../ApplicationsStacksDatatable/NamespaceFilter';
|
||||
import { Namespace } from '../ApplicationsStacksDatatable/types';
|
||||
import { useApplications } from '../../application.queries';
|
||||
|
||||
import { Application, ConfigKind } from './types';
|
||||
import { useColumns } from './useColumns';
|
||||
|
@ -26,9 +28,7 @@ import { SubRow } from './SubRow';
|
|||
import { HelmInsightsBox } from './HelmInsightsBox';
|
||||
|
||||
export function ApplicationsDatatable({
|
||||
dataset,
|
||||
onRefresh,
|
||||
isLoading,
|
||||
onRemove,
|
||||
namespace = '',
|
||||
namespaces,
|
||||
|
@ -37,9 +37,7 @@ export function ApplicationsDatatable({
|
|||
onShowSystemChange,
|
||||
hideStacks,
|
||||
}: {
|
||||
dataset: Array<Application>;
|
||||
onRefresh: () => void;
|
||||
isLoading: boolean;
|
||||
onRemove: (selectedItems: Application[]) => void;
|
||||
namespace?: string;
|
||||
namespaces: Array<Namespace>;
|
||||
|
@ -50,7 +48,7 @@ export function ApplicationsDatatable({
|
|||
}) {
|
||||
const envId = useEnvironmentId();
|
||||
const envQuery = useCurrentEnvironment();
|
||||
const namespaceMetaListQuery = useNamespacesQuery(envId);
|
||||
const namespaceListQuery = useNamespacesQuery(envId);
|
||||
|
||||
const tableState = useKubeStore('kubernetes.applications', 'Name');
|
||||
useRepeater(tableState.autoRefreshRate, onRefresh);
|
||||
|
@ -58,7 +56,7 @@ export function ApplicationsDatatable({
|
|||
const hasWriteAuthQuery = useAuthorizations(
|
||||
'K8sApplicationsW',
|
||||
undefined,
|
||||
true
|
||||
false
|
||||
);
|
||||
|
||||
const { setShowSystemResources } = tableState;
|
||||
|
@ -67,27 +65,34 @@ export function ApplicationsDatatable({
|
|||
setShowSystemResources(showSystem || false);
|
||||
}, [showSystem, setShowSystemResources]);
|
||||
|
||||
const columns = useColumns(hideStacks);
|
||||
const applicationsQuery = useApplications(envId, {
|
||||
refetchInterval: tableState.autoRefreshRate * 1000,
|
||||
namespace,
|
||||
withDependencies: true,
|
||||
});
|
||||
const applications = applicationsQuery.data ?? [];
|
||||
const filteredApplications = showSystem
|
||||
? applications
|
||||
: applications.filter(
|
||||
(application) =>
|
||||
!isSystemNamespace(application.ResourcePool, namespaceListQuery.data)
|
||||
);
|
||||
|
||||
const filteredDataset = !showSystem
|
||||
? dataset.filter(
|
||||
(item) => !namespaceMetaListQuery.data?.[item.ResourcePool]?.IsSystem
|
||||
)
|
||||
: dataset;
|
||||
const columns = useColumns(hideStacks);
|
||||
|
||||
return (
|
||||
<ExpandableDatatable
|
||||
data-cy="k8sApp-appTable"
|
||||
noWidget
|
||||
dataset={filteredDataset}
|
||||
dataset={filteredApplications ?? []}
|
||||
settingsManager={tableState}
|
||||
columns={columns}
|
||||
title="Applications"
|
||||
titleIcon={BoxIcon}
|
||||
isLoading={isLoading}
|
||||
isLoading={applicationsQuery.isLoading}
|
||||
disableSelect={!hasWriteAuthQuery.authorized}
|
||||
isRowSelectable={(row) =>
|
||||
!namespaceMetaListQuery.data?.[row.original.ResourcePool]?.IsSystem
|
||||
!isSystemNamespace(row.original.ResourcePool, namespaceListQuery.data)
|
||||
}
|
||||
getRowCanExpand={(row) => isExpandable(row.original)}
|
||||
renderSubRow={(row) => (
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
import { isoDate, truncate } from '@/portainer/filters/filters';
|
||||
import { CellContext } from '@tanstack/react-table';
|
||||
|
||||
import { isoDate, truncate } from '@/portainer/filters/filters';
|
||||
import { useIsSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace';
|
||||
|
||||
import { Link } from '@@/Link';
|
||||
import { SystemBadge } from '@@/Badge/SystemBadge';
|
||||
|
||||
import { Application } from './types';
|
||||
import { helper } from './columns.helper';
|
||||
|
||||
export const stackName = helper.accessor('StackName', {
|
||||
|
@ -9,9 +16,26 @@ export const stackName = helper.accessor('StackName', {
|
|||
|
||||
export const namespace = helper.accessor('ResourcePool', {
|
||||
header: 'Namespace',
|
||||
cell: ({ getValue }) => getValue() || '-',
|
||||
cell: NamespaceCell,
|
||||
});
|
||||
|
||||
function NamespaceCell({ row, getValue }: CellContext<Application, string>) {
|
||||
const value = getValue();
|
||||
const isSystem = useIsSystemNamespace(value);
|
||||
return (
|
||||
<div className="flex gap-2">
|
||||
<Link
|
||||
to="kubernetes.resourcePools.resourcePool"
|
||||
params={{ id: value }}
|
||||
data-cy={`app-namespace-link-${row.original.Name}`}
|
||||
>
|
||||
{value}
|
||||
</Link>
|
||||
{isSystem && <SystemBadge />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export const image = helper.accessor('Image', {
|
||||
header: 'Image',
|
||||
cell: ({ row: { original: item } }) => (
|
||||
|
|
|
@ -39,6 +39,12 @@ export interface Application {
|
|||
}>;
|
||||
Port: number;
|
||||
}>;
|
||||
Resource?: {
|
||||
CpuLimit?: number;
|
||||
CpuRequest?: number;
|
||||
MemoryLimit?: number;
|
||||
MemoryRequest?: number;
|
||||
};
|
||||
}
|
||||
|
||||
export enum ConfigKind {
|
||||
|
|
|
@ -4,44 +4,41 @@ import { useEffect } from 'react';
|
|||
import { useAuthorizations } from '@/react/hooks/useUser';
|
||||
import { SystemResourceDescription } from '@/react/kubernetes/datatables/SystemResourceDescription';
|
||||
import { createStore } from '@/react/kubernetes/datatables/default-kube-datatable-store';
|
||||
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
||||
import { isSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace';
|
||||
import { useNamespacesQuery } from '@/react/kubernetes/namespaces/queries/useNamespacesQuery';
|
||||
|
||||
import { ExpandableDatatable } from '@@/datatables/ExpandableDatatable';
|
||||
import { useRepeater } from '@@/datatables/useRepeater';
|
||||
import { useTableState } from '@@/datatables/useTableState';
|
||||
|
||||
import { KubernetesStack } from '../../types';
|
||||
import { useApplications } from '../../application.queries';
|
||||
|
||||
import { columns } from './columns';
|
||||
import { SubRows } from './SubRows';
|
||||
import { Namespace } from './types';
|
||||
import { Namespace, Stack } from './types';
|
||||
import { StacksSettingsMenu } from './StacksSettingsMenu';
|
||||
import { NamespaceFilter } from './NamespaceFilter';
|
||||
import { TableActions } from './TableActions';
|
||||
import { getStacksFromApplications } from './getStacksFromApplications';
|
||||
|
||||
const storageKey = 'kubernetes.applications.stacks';
|
||||
|
||||
const settingsStore = createStore(storageKey);
|
||||
|
||||
interface Props {
|
||||
dataset: Array<KubernetesStack>;
|
||||
onRemove(selectedItems: Array<KubernetesStack>): void;
|
||||
onRefresh(): Promise<void>;
|
||||
onRemove(selectedItems: Array<Stack>): void;
|
||||
namespace?: string;
|
||||
namespaces: Array<Namespace>;
|
||||
onNamespaceChange(namespace: string): void;
|
||||
isLoading?: boolean;
|
||||
showSystem?: boolean;
|
||||
setSystemResources(showSystem: boolean): void;
|
||||
}
|
||||
|
||||
export function ApplicationsStacksDatatable({
|
||||
dataset,
|
||||
onRemove,
|
||||
onRefresh,
|
||||
namespace = '',
|
||||
namespaces,
|
||||
onNamespaceChange,
|
||||
isLoading,
|
||||
showSystem,
|
||||
setSystemResources,
|
||||
}: Props) {
|
||||
|
@ -53,16 +50,32 @@ export function ApplicationsStacksDatatable({
|
|||
setShowSystemResources(showSystem || false);
|
||||
}, [showSystem, setShowSystemResources]);
|
||||
|
||||
const envId = useEnvironmentId();
|
||||
const applicationsQuery = useApplications(envId, {
|
||||
refetchInterval: tableState.autoRefreshRate * 1000,
|
||||
namespace,
|
||||
withDependencies: true,
|
||||
});
|
||||
const namespaceListQuery = useNamespacesQuery(envId);
|
||||
const applications = applicationsQuery.data ?? [];
|
||||
const filteredApplications = showSystem
|
||||
? applications
|
||||
: applications.filter(
|
||||
(item) =>
|
||||
!isSystemNamespace(item.ResourcePool, namespaceListQuery.data ?? [])
|
||||
);
|
||||
|
||||
const { authorized } = useAuthorizations('K8sApplicationsW');
|
||||
useRepeater(tableState.autoRefreshRate, onRefresh);
|
||||
|
||||
const stacks = getStacksFromApplications(filteredApplications);
|
||||
|
||||
return (
|
||||
<ExpandableDatatable
|
||||
getRowCanExpand={(row) => row.original.Applications.length > 0}
|
||||
title="Stacks"
|
||||
titleIcon={List}
|
||||
dataset={dataset}
|
||||
isLoading={isLoading}
|
||||
dataset={stacks}
|
||||
isLoading={applicationsQuery.isLoading || namespaceListQuery.isLoading}
|
||||
columns={columns}
|
||||
settingsManager={tableState}
|
||||
disableSelect={!authorized}
|
||||
|
|
|
@ -6,15 +6,9 @@ import KubernetesNamespaceHelper from '@/kubernetes/helpers/namespaceHelper';
|
|||
import { Link } from '@@/Link';
|
||||
import { ExternalBadge } from '@@/Badge/ExternalBadge';
|
||||
|
||||
import { KubernetesStack } from '../../types';
|
||||
import { Stack } from './types';
|
||||
|
||||
export function SubRows({
|
||||
stack,
|
||||
span,
|
||||
}: {
|
||||
stack: KubernetesStack;
|
||||
span: number;
|
||||
}) {
|
||||
export function SubRows({ stack, span }: { stack: Stack; span: number }) {
|
||||
return (
|
||||
<>
|
||||
{stack.Applications.map((app) => (
|
||||
|
|
|
@ -2,14 +2,14 @@ import { Authorized } from '@/react/hooks/useUser';
|
|||
|
||||
import { DeleteButton } from '@@/buttons/DeleteButton';
|
||||
|
||||
import { KubernetesStack } from '../../types';
|
||||
import { Stack } from './types';
|
||||
|
||||
export function TableActions({
|
||||
selectedItems,
|
||||
onRemove,
|
||||
}: {
|
||||
selectedItems: Array<KubernetesStack>;
|
||||
onRemove: (selectedItems: Array<KubernetesStack>) => void;
|
||||
selectedItems: Array<Stack>;
|
||||
onRemove: (selectedItems: Array<Stack>) => void;
|
||||
}) {
|
||||
return (
|
||||
<Authorized authorizations="K8sApplicationsW">
|
||||
|
|
|
@ -1,63 +1,70 @@
|
|||
import { FileText } from 'lucide-react';
|
||||
import { createColumnHelper } from '@tanstack/react-table';
|
||||
import { CellContext, createColumnHelper } from '@tanstack/react-table';
|
||||
|
||||
import KubernetesNamespaceHelper from '@/kubernetes/helpers/namespaceHelper';
|
||||
import { useIsSystemNamespace } from '@/react/kubernetes/namespaces/queries/useIsSystemNamespace';
|
||||
|
||||
import { buildExpandColumn } from '@@/datatables/expand-column';
|
||||
import { Link } from '@@/Link';
|
||||
import { Icon } from '@@/Icon';
|
||||
import { SystemBadge } from '@@/Badge/SystemBadge';
|
||||
|
||||
import { KubernetesStack } from '../../types';
|
||||
import { Stack } from './types';
|
||||
|
||||
export const columnHelper = createColumnHelper<KubernetesStack>();
|
||||
export const columnHelper = createColumnHelper<Stack>();
|
||||
|
||||
const namespace = columnHelper.accessor('ResourcePool', {
|
||||
id: 'namespace',
|
||||
header: 'Namespace',
|
||||
cell: NamespaceCell,
|
||||
});
|
||||
|
||||
function NamespaceCell({ row, getValue }: CellContext<Stack, string>) {
|
||||
const value = getValue();
|
||||
const isSystem = useIsSystemNamespace(value);
|
||||
return (
|
||||
<div className="flex gap-2">
|
||||
<Link
|
||||
to="kubernetes.resourcePools.resourcePool"
|
||||
params={{ id: value }}
|
||||
data-cy={`app-stack-namespace-link-${row.original.Name}`}
|
||||
>
|
||||
{value}
|
||||
</Link>
|
||||
{isSystem && <SystemBadge />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const name = columnHelper.accessor('Name', {
|
||||
id: 'name',
|
||||
header: 'Stack',
|
||||
});
|
||||
|
||||
const applications = columnHelper.accessor((row) => row.Applications.length, {
|
||||
id: 'applications',
|
||||
header: 'Applications',
|
||||
});
|
||||
|
||||
const actions = columnHelper.display({
|
||||
id: 'actions',
|
||||
header: 'Actions',
|
||||
cell: ({ row: { original: item } }) => (
|
||||
<Link
|
||||
to="kubernetes.stacks.stack.logs"
|
||||
params={{ namespace: item.ResourcePool, name: item.Name }}
|
||||
className="flex items-center gap-1"
|
||||
data-cy={`app-stack-logs-link-${item.Name}`}
|
||||
>
|
||||
<Icon icon={FileText} />
|
||||
Logs
|
||||
</Link>
|
||||
),
|
||||
});
|
||||
|
||||
export const columns = [
|
||||
buildExpandColumn<KubernetesStack>(),
|
||||
columnHelper.accessor('Name', {
|
||||
id: 'name',
|
||||
header: 'Stack',
|
||||
}),
|
||||
columnHelper.accessor('ResourcePool', {
|
||||
id: 'namespace',
|
||||
header: 'Namespace',
|
||||
cell: ({ getValue, row }) => {
|
||||
const value = getValue();
|
||||
return (
|
||||
<div className="flex gap-2">
|
||||
<Link
|
||||
to="kubernetes.resourcePools.resourcePool"
|
||||
params={{ id: value }}
|
||||
data-cy={`app-stack-namespace-link-${row.original.Name}`}
|
||||
>
|
||||
{value}
|
||||
</Link>
|
||||
{KubernetesNamespaceHelper.isSystemNamespace(value) && (
|
||||
<SystemBadge />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
},
|
||||
}),
|
||||
|
||||
columnHelper.accessor((row) => row.Applications.length, {
|
||||
id: 'applications',
|
||||
header: 'Applications',
|
||||
}),
|
||||
|
||||
columnHelper.display({
|
||||
id: 'actions',
|
||||
header: 'Actions',
|
||||
cell: ({ row: { original: item } }) => (
|
||||
<Link
|
||||
to="kubernetes.stacks.stack.logs"
|
||||
params={{ namespace: item.ResourcePool, name: item.Name }}
|
||||
className="flex items-center gap-1"
|
||||
data-cy={`app-stack-logs-link-${item.Name}`}
|
||||
>
|
||||
<Icon icon={FileText} />
|
||||
Logs
|
||||
</Link>
|
||||
),
|
||||
}),
|
||||
buildExpandColumn<Stack>(),
|
||||
name,
|
||||
namespace,
|
||||
applications,
|
||||
actions,
|
||||
];
|
||||
|
|
|
@ -0,0 +1,159 @@
|
|||
import { Application } from '../ApplicationsDatatable/types';
|
||||
|
||||
import { getStacksFromApplications } from './getStacksFromApplications';
|
||||
import { Stack } from './types';
|
||||
|
||||
describe('getStacksFromApplications', () => {
|
||||
test('should return an empty array when passed an empty array', () => {
|
||||
expect(getStacksFromApplications([])).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('should return an empty array when passed a list of applications without stacks', () => {
|
||||
const appsWithoutStacks: Application[] = [
|
||||
{
|
||||
StackName: '',
|
||||
Id: '1',
|
||||
Name: 'app1',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
{
|
||||
StackName: '',
|
||||
Id: '1',
|
||||
Name: 'app2',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
{
|
||||
StackName: '',
|
||||
Id: '1',
|
||||
Name: 'app3',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
];
|
||||
expect(getStacksFromApplications(appsWithoutStacks)).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('should return a list of stacks when passed a list of applications with stacks', () => {
|
||||
const appsWithStacks: Application[] = [
|
||||
{
|
||||
StackName: 'stack1',
|
||||
Id: '1',
|
||||
Name: 'app1',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
{
|
||||
StackName: 'stack1',
|
||||
Id: '1',
|
||||
Name: 'app2',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
{
|
||||
StackName: 'stack2',
|
||||
Id: '1',
|
||||
Name: 'app3',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
];
|
||||
|
||||
const expectedStacksWithApps: Stack[] = [
|
||||
{
|
||||
Name: 'stack1',
|
||||
ResourcePool: 'namespace1',
|
||||
Applications: [
|
||||
{
|
||||
StackName: 'stack1',
|
||||
Id: '1',
|
||||
Name: 'app1',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
{
|
||||
StackName: 'stack1',
|
||||
Id: '1',
|
||||
Name: 'app2',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
],
|
||||
Highlighted: false,
|
||||
},
|
||||
{
|
||||
Name: 'stack2',
|
||||
ResourcePool: 'namespace1',
|
||||
Applications: [
|
||||
{
|
||||
StackName: 'stack2',
|
||||
Id: '1',
|
||||
Name: 'app3',
|
||||
CreationDate: '2021-10-01T00:00:00Z',
|
||||
ResourcePool: 'namespace1',
|
||||
Image: 'image1',
|
||||
ApplicationType: 'Pod',
|
||||
DeploymentType: 'Replicated',
|
||||
Status: 'status1',
|
||||
TotalPodsCount: 1,
|
||||
RunningPodsCount: 1,
|
||||
},
|
||||
],
|
||||
Highlighted: false,
|
||||
},
|
||||
];
|
||||
|
||||
expect(getStacksFromApplications(appsWithStacks)).toEqual(
|
||||
expectedStacksWithApps
|
||||
);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,36 @@
|
|||
import { Application } from '../ApplicationsDatatable/types';
|
||||
|
||||
import { Stack } from './types';
|
||||
|
||||
export function getStacksFromApplications(applications: Application[]) {
|
||||
const res = applications.reduce<Stack[]>((stacks, app) => {
|
||||
const updatedStacks = stacks.map((stack) => {
|
||||
if (
|
||||
stack.Name === app.StackName &&
|
||||
stack.ResourcePool === app.ResourcePool
|
||||
) {
|
||||
return {
|
||||
...stack,
|
||||
Applications: [...stack.Applications, app],
|
||||
};
|
||||
}
|
||||
return stack;
|
||||
});
|
||||
|
||||
const stackExists = updatedStacks.some(
|
||||
(stack) =>
|
||||
stack.Name === app.StackName && stack.ResourcePool === app.ResourcePool
|
||||
);
|
||||
|
||||
if (!stackExists && app.StackName) {
|
||||
updatedStacks.push({
|
||||
Name: app.StackName,
|
||||
ResourcePool: app.ResourcePool,
|
||||
Applications: [app],
|
||||
Highlighted: false,
|
||||
});
|
||||
}
|
||||
return updatedStacks;
|
||||
}, []);
|
||||
return res;
|
||||
}
|
|
@ -5,6 +5,8 @@ import {
|
|||
RefreshableTableSettings,
|
||||
} from '@@/datatables/types';
|
||||
|
||||
import { Application } from '../ApplicationsDatatable/types';
|
||||
|
||||
export interface TableSettings
|
||||
extends BasicTableSettings,
|
||||
RefreshableTableSettings,
|
||||
|
@ -16,3 +18,10 @@ export interface Namespace {
|
|||
Yaml: string;
|
||||
IsSystem?: boolean;
|
||||
}
|
||||
|
||||
export type Stack = {
|
||||
Name: string;
|
||||
ResourcePool: string;
|
||||
Applications: Application[];
|
||||
Highlighted: boolean;
|
||||
};
|
||||
|
|
|
@ -1,26 +1,37 @@
|
|||
import { UseQueryResult, useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { Pod } from 'kubernetes-types/core/v1';
|
||||
|
||||
import { queryClient, withError } from '@/react-tools/react-query';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
|
||||
import { getNamespaceServices } from '../services/service';
|
||||
import { Pod, PodList } from 'kubernetes-types/core/v1';
|
||||
|
||||
import {
|
||||
queryClient,
|
||||
withError,
|
||||
withGlobalError,
|
||||
} from '@/react-tools/react-query';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
|
||||
import { getNamespaceServices } from '../services/service';
|
||||
import { parseKubernetesAxiosError } from '../axiosError';
|
||||
|
||||
import {
|
||||
getApplicationsForCluster,
|
||||
getApplication,
|
||||
patchApplication,
|
||||
getApplicationRevisionList,
|
||||
} from './application.service';
|
||||
import type { AppKind, Application, ApplicationPatch } from './types';
|
||||
import { Application as K8sApplication } from './ListView/ApplicationsDatatable/types';
|
||||
import { deletePod } from './pod.service';
|
||||
import { getNamespaceHorizontalPodAutoscalers } from './autoscaling.service';
|
||||
import { applicationIsKind, matchLabelsToLabelSelectorValue } from './utils';
|
||||
import { getNamespacePods } from './usePods';
|
||||
|
||||
const queryKeys = {
|
||||
applicationsForCluster: (environmentId: EnvironmentId) =>
|
||||
['environments', environmentId, 'kubernetes', 'applications'] as const,
|
||||
applications: (environmentId: EnvironmentId, params?: GetAppsParams) =>
|
||||
[
|
||||
'environments',
|
||||
environmentId,
|
||||
'kubernetes',
|
||||
'applications',
|
||||
params,
|
||||
] as const,
|
||||
application: (
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
|
@ -110,21 +121,6 @@ const queryKeys = {
|
|||
] as const,
|
||||
};
|
||||
|
||||
// useQuery to get a list of all applications from an array of namespaces
|
||||
export function useApplicationsQuery(
|
||||
environmentId: EnvironmentId,
|
||||
namespaces?: string[]
|
||||
) {
|
||||
return useQuery(
|
||||
queryKeys.applicationsForCluster(environmentId),
|
||||
() => getApplicationsForCluster(environmentId, namespaces),
|
||||
{
|
||||
...withError('Unable to retrieve applications'),
|
||||
enabled: !!namespaces?.length,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// when yaml is set to true, the expected return type is a string
|
||||
export function useApplication<T extends Application | string = Application>(
|
||||
environmentId: EnvironmentId,
|
||||
|
@ -305,6 +301,37 @@ export function useApplicationPods(
|
|||
);
|
||||
}
|
||||
|
||||
async function getNamespacePods(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
labelSelector?: string
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<PodList>(
|
||||
`/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/pods`,
|
||||
{
|
||||
params: {
|
||||
labelSelector,
|
||||
},
|
||||
}
|
||||
);
|
||||
const items = (data.items || []).map(
|
||||
(pod) =>
|
||||
<Pod>{
|
||||
...pod,
|
||||
kind: 'Pod',
|
||||
apiVersion: data.apiVersion,
|
||||
}
|
||||
);
|
||||
return items;
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(
|
||||
e,
|
||||
`Unable to retrieve Pods in namespace '${namespace}'`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// useQuery to patch an application by environmentId, namespace, name and patch payload
|
||||
export function usePatchApplicationMutation(
|
||||
environmentId: EnvironmentId,
|
||||
|
@ -380,3 +407,45 @@ export function useRedeployApplicationMutation(
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
type GetAppsParams = {
|
||||
namespace?: string;
|
||||
nodeName?: string;
|
||||
withDependencies?: boolean;
|
||||
};
|
||||
|
||||
type GetAppsQueryOptions = {
|
||||
refetchInterval?: number;
|
||||
} & GetAppsParams;
|
||||
|
||||
// useQuery to get a list of all applications from an array of namespaces
|
||||
export function useApplications(
|
||||
environmentId: EnvironmentId,
|
||||
queryOptions?: GetAppsQueryOptions
|
||||
) {
|
||||
const { refetchInterval, ...params } = queryOptions ?? {};
|
||||
return useQuery(
|
||||
queryKeys.applications(environmentId, params),
|
||||
() => getApplications(environmentId, params),
|
||||
{
|
||||
refetchInterval,
|
||||
...withGlobalError('Unable to retrieve applications'),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// get all applications from a namespace
|
||||
export async function getApplications(
|
||||
environmentId: EnvironmentId,
|
||||
params?: GetAppsParams
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<K8sApplication[]>(
|
||||
`/kubernetes/${environmentId}/applications`,
|
||||
{ params }
|
||||
);
|
||||
return data;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e, 'Unable to retrieve applications');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
import {
|
||||
DaemonSetList,
|
||||
StatefulSetList,
|
||||
DeploymentList,
|
||||
Deployment,
|
||||
DaemonSet,
|
||||
StatefulSet,
|
||||
|
@ -16,56 +13,9 @@ import { isFulfilled } from '@/portainer/helpers/promise-utils';
|
|||
import { parseKubernetesAxiosError } from '../axiosError';
|
||||
|
||||
import { getPod, patchPod } from './pod.service';
|
||||
import { filterRevisionsByOwnerUid, getNakedPods } from './utils';
|
||||
import {
|
||||
AppKind,
|
||||
Application,
|
||||
ApplicationList,
|
||||
ApplicationPatch,
|
||||
} from './types';
|
||||
import { filterRevisionsByOwnerUid } from './utils';
|
||||
import { AppKind, Application, ApplicationPatch } from './types';
|
||||
import { appRevisionAnnotation } from './constants';
|
||||
import { getNamespacePods } from './usePods';
|
||||
|
||||
// This file contains services for Kubernetes apps/v1 resources (Deployments, DaemonSets, StatefulSets)
|
||||
|
||||
export async function getApplicationsForCluster(
|
||||
environmentId: EnvironmentId,
|
||||
namespaceNames?: string[]
|
||||
) {
|
||||
if (!namespaceNames) {
|
||||
return [];
|
||||
}
|
||||
const applications = await Promise.all(
|
||||
namespaceNames.map((namespace) =>
|
||||
getApplicationsForNamespace(environmentId, namespace)
|
||||
)
|
||||
);
|
||||
return applications.flat();
|
||||
}
|
||||
|
||||
// get a list of all Deployments, DaemonSets, StatefulSets and naked pods (https://portainer.atlassian.net/browse/CE-2) in one namespace
|
||||
async function getApplicationsForNamespace(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string
|
||||
) {
|
||||
const [deployments, daemonSets, statefulSets, pods] = await Promise.all([
|
||||
getApplicationsByKind<DeploymentList>(
|
||||
environmentId,
|
||||
namespace,
|
||||
'Deployment'
|
||||
),
|
||||
getApplicationsByKind<DaemonSetList>(environmentId, namespace, 'DaemonSet'),
|
||||
getApplicationsByKind<StatefulSetList>(
|
||||
environmentId,
|
||||
namespace,
|
||||
'StatefulSet'
|
||||
),
|
||||
getNamespacePods(environmentId, namespace),
|
||||
]);
|
||||
// find all pods which are 'naked' (not owned by a deployment, daemonset or statefulset)
|
||||
const nakedPods = getNakedPods(pods, deployments, daemonSets, statefulSets);
|
||||
return [...deployments, ...daemonSets, ...statefulSets, ...nakedPods];
|
||||
}
|
||||
|
||||
// if not known, get the type of an application (Deployment, DaemonSet, StatefulSet or naked pod) by name
|
||||
export async function getApplication<
|
||||
|
@ -235,29 +185,6 @@ async function getApplicationByKind<
|
|||
}
|
||||
}
|
||||
|
||||
async function getApplicationsByKind<T extends ApplicationList>(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
appKind: 'Deployment' | 'DaemonSet' | 'StatefulSet'
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<T>(
|
||||
buildUrl(environmentId, namespace, `${appKind}s`)
|
||||
);
|
||||
const items = (data.items || []).map((app) => ({
|
||||
...app,
|
||||
kind: appKind,
|
||||
apiVersion: data.apiVersion,
|
||||
}));
|
||||
return items as T['items'];
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(
|
||||
e,
|
||||
`Unable to retrieve ${appKind}s in namespace '${namespace}'`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getApplicationRevisionList(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
|
|
|
@ -24,9 +24,9 @@ export function NamespaceSelector({
|
|||
useNamespacesQuery(environmentId);
|
||||
const namespaceNames = Object.entries(namespaces ?? {})
|
||||
.filter(([, ns]) => !ns.IsSystem)
|
||||
.map(([nsName]) => ({
|
||||
label: nsName,
|
||||
value: nsName,
|
||||
.map(([, ns]) => ({
|
||||
label: ns.Name,
|
||||
value: ns.Name,
|
||||
}));
|
||||
|
||||
return (
|
||||
|
|
|
@ -8,8 +8,9 @@ import {
|
|||
ReplicaSet,
|
||||
ControllerRevision,
|
||||
} from 'kubernetes-types/apps/v1';
|
||||
import { Pod, PodList } from 'kubernetes-types/core/v1';
|
||||
import { Container, Pod, PodList, Volume } from 'kubernetes-types/core/v1';
|
||||
import { RawExtension } from 'kubernetes-types/runtime';
|
||||
import { OwnerReference } from 'kubernetes-types/meta/v1';
|
||||
|
||||
import { EnvVarValues } from '@@/form-components/EnvironmentVariablesFieldset';
|
||||
|
||||
|
@ -79,14 +80,29 @@ type Patch = {
|
|||
|
||||
export type ApplicationPatch = Patch | RawExtension;
|
||||
|
||||
export type KubernetesStack = {
|
||||
Name: string;
|
||||
ResourcePool: string;
|
||||
Applications: Array<
|
||||
Application & {
|
||||
Name: string;
|
||||
ResourcePool: string;
|
||||
}
|
||||
>;
|
||||
Highlighted: boolean;
|
||||
};
|
||||
export interface ConfigmapRef {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ValueFrom {
|
||||
configMapRef?: ConfigmapRef;
|
||||
secretRef?: ConfigmapRef;
|
||||
}
|
||||
|
||||
export interface Job {
|
||||
name?: string;
|
||||
namespace: string;
|
||||
creationDate?: string;
|
||||
uid?: string;
|
||||
containers: Container[];
|
||||
}
|
||||
|
||||
export interface K8sPod extends Job {
|
||||
ownerReferences: OwnerReference[];
|
||||
volumes?: Volume[];
|
||||
nodeName?: string;
|
||||
}
|
||||
|
||||
export interface CronJob extends Job {
|
||||
schedule: string;
|
||||
}
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
import { useQuery } from '@tanstack/react-query';
|
||||
import { Pod, PodList } from 'kubernetes-types/core/v1';
|
||||
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import { withError } from '@/react-tools/react-query';
|
||||
import axios from '@/portainer/services/axios';
|
||||
|
||||
import { parseKubernetesAxiosError } from '../axiosError';
|
||||
|
||||
const queryKeys = {
|
||||
podsForCluster: (environmentId: EnvironmentId) => [
|
||||
'environments',
|
||||
environmentId,
|
||||
'kubernetes',
|
||||
'pods',
|
||||
],
|
||||
};
|
||||
|
||||
export function usePods(environemtId: EnvironmentId, namespaces?: string[]) {
|
||||
return useQuery(
|
||||
queryKeys.podsForCluster(environemtId),
|
||||
() => getPodsForCluster(environemtId, namespaces),
|
||||
{
|
||||
...withError('Unable to retrieve Pods'),
|
||||
enabled: !!namespaces?.length,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export async function getPodsForCluster(
|
||||
environmentId: EnvironmentId,
|
||||
namespaceNames?: string[]
|
||||
) {
|
||||
if (!namespaceNames) {
|
||||
return [];
|
||||
}
|
||||
const pods = await Promise.all(
|
||||
namespaceNames.map((namespace) =>
|
||||
getNamespacePods(environmentId, namespace)
|
||||
)
|
||||
);
|
||||
return pods.flat();
|
||||
}
|
||||
|
||||
export async function getNamespacePods(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
labelSelector?: string
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<PodList>(
|
||||
`/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/pods`,
|
||||
{
|
||||
params: {
|
||||
labelSelector,
|
||||
},
|
||||
}
|
||||
);
|
||||
const items = (data.items || []).map(
|
||||
(pod) =>
|
||||
<Pod>{
|
||||
...pod,
|
||||
kind: 'Pod',
|
||||
apiVersion: data.apiVersion,
|
||||
}
|
||||
);
|
||||
return items;
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(
|
||||
e,
|
||||
`Unable to retrieve Pods in namespace '${namespace}'`
|
||||
);
|
||||
}
|
||||
}
|
|
@ -18,39 +18,6 @@ import {
|
|||
appRevisionAnnotation,
|
||||
} from './constants';
|
||||
|
||||
// naked pods are pods which are not owned by a deployment, daemonset, statefulset or replicaset
|
||||
// https://kubernetes.io/docs/concepts/configuration/overview/#naked-pods-vs-replicasets-deployments-and-jobs
|
||||
// getNakedPods returns an array of naked pods from an array of pods, deployments, daemonsets and statefulsets
|
||||
export function getNakedPods(
|
||||
pods: Pod[],
|
||||
deployments: Deployment[],
|
||||
daemonSets: DaemonSet[],
|
||||
statefulSets: StatefulSet[]
|
||||
) {
|
||||
const appLabels = [
|
||||
...deployments.map((deployment) => deployment.spec?.selector.matchLabels),
|
||||
...daemonSets.map((daemonSet) => daemonSet.spec?.selector.matchLabels),
|
||||
...statefulSets.map(
|
||||
(statefulSet) => statefulSet.spec?.selector.matchLabels
|
||||
),
|
||||
];
|
||||
|
||||
const nakedPods = pods.filter((pod) => {
|
||||
const podLabels = pod.metadata?.labels;
|
||||
// if the pod has no labels, it is naked
|
||||
if (!podLabels) return true;
|
||||
// if the pod has labels, but no app labels, it is naked
|
||||
return !appLabels.some((appLabel) => {
|
||||
if (!appLabel) return false;
|
||||
return Object.entries(appLabel).every(
|
||||
([key, value]) => podLabels[key] === value
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
return nakedPods;
|
||||
}
|
||||
|
||||
// type guard to check if an application is a deployment, daemonset, statefulset or pod
|
||||
export function applicationIsKind<T extends Application>(
|
||||
appKind: 'Deployment' | 'DaemonSet' | 'StatefulSet' | 'Pod',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue