mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 21:39:40 +02:00
feat(helm): auto refresh helm resources [r8s-298] (#672)
This commit is contained in:
parent
9a9373dd0f
commit
61d6ac035d
10 changed files with 120 additions and 50 deletions
|
@ -9,10 +9,9 @@ import {
|
||||||
|
|
||||||
import { Datatable, defaultGlobalFilterFn, Props } from './Datatable';
|
import { Datatable, defaultGlobalFilterFn, Props } from './Datatable';
|
||||||
import {
|
import {
|
||||||
BasicTableSettings,
|
|
||||||
createPersistedStore,
|
createPersistedStore,
|
||||||
refreshableSettings,
|
refreshableSettings,
|
||||||
RefreshableTableSettings,
|
TableSettingsWithRefreshable,
|
||||||
} from './types';
|
} from './types';
|
||||||
import { useTableState } from './useTableState';
|
import { useTableState } from './useTableState';
|
||||||
|
|
||||||
|
@ -30,13 +29,14 @@ const mockColumns = [
|
||||||
];
|
];
|
||||||
|
|
||||||
// mock table settings / state
|
// mock table settings / state
|
||||||
export interface TableSettings
|
|
||||||
extends BasicTableSettings,
|
|
||||||
RefreshableTableSettings {}
|
|
||||||
function createStore(storageKey: string) {
|
function createStore(storageKey: string) {
|
||||||
return createPersistedStore<TableSettings>(storageKey, 'name', (set) => ({
|
return createPersistedStore<TableSettingsWithRefreshable>(
|
||||||
|
storageKey,
|
||||||
|
'name',
|
||||||
|
(set) => ({
|
||||||
...refreshableSettings(set),
|
...refreshableSettings(set),
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const storageKey = 'test-table';
|
const storageKey = 'test-table';
|
||||||
const settingsStore = createStore(storageKey);
|
const settingsStore = createStore(storageKey);
|
||||||
|
|
|
@ -99,6 +99,10 @@ export interface BasicTableSettings
|
||||||
extends SortableTableSettings,
|
extends SortableTableSettings,
|
||||||
PaginationTableSettings {}
|
PaginationTableSettings {}
|
||||||
|
|
||||||
|
export interface TableSettingsWithRefreshable
|
||||||
|
extends BasicTableSettings,
|
||||||
|
RefreshableTableSettings {}
|
||||||
|
|
||||||
export function createPersistedStore<T extends BasicTableSettings>(
|
export function createPersistedStore<T extends BasicTableSettings>(
|
||||||
storageKey: string,
|
storageKey: string,
|
||||||
initialSortBy?: string | { id: string; desc: boolean },
|
initialSortBy?: string | { id: string; desc: boolean },
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
import {
|
import {
|
||||||
BasicTableSettings,
|
|
||||||
createPersistedStore,
|
createPersistedStore,
|
||||||
refreshableSettings,
|
refreshableSettings,
|
||||||
RefreshableTableSettings,
|
TableSettingsWithRefreshable,
|
||||||
} from '@@/datatables/types';
|
} from '@@/datatables/types';
|
||||||
|
|
||||||
export interface TableSettings
|
|
||||||
extends BasicTableSettings,
|
|
||||||
RefreshableTableSettings {}
|
|
||||||
|
|
||||||
export function createStore(storageKey: string) {
|
export function createStore(storageKey: string) {
|
||||||
return createPersistedStore<TableSettings>(storageKey, 'name', (set) => ({
|
return createPersistedStore<TableSettingsWithRefreshable>(
|
||||||
|
storageKey,
|
||||||
|
'name',
|
||||||
|
(set) => ({
|
||||||
...refreshableSettings(set),
|
...refreshableSettings(set),
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,5 @@
|
||||||
import { SystemResourcesTableSettings } from '@/react/kubernetes/datatables/SystemResourcesSettings';
|
|
||||||
|
|
||||||
import {
|
|
||||||
BasicTableSettings,
|
|
||||||
RefreshableTableSettings,
|
|
||||||
} from '@@/datatables/types';
|
|
||||||
|
|
||||||
import { Application } from '../ApplicationsDatatable/types';
|
import { Application } from '../ApplicationsDatatable/types';
|
||||||
|
|
||||||
export interface TableSettings
|
|
||||||
extends BasicTableSettings,
|
|
||||||
RefreshableTableSettings,
|
|
||||||
SystemResourcesTableSettings {}
|
|
||||||
|
|
||||||
export type Stack = {
|
export type Stack = {
|
||||||
Name: string;
|
Name: string;
|
||||||
ResourcePool: string;
|
ResourcePool: string;
|
||||||
|
|
|
@ -25,7 +25,7 @@ function helmTabs(
|
||||||
{
|
{
|
||||||
label: 'Resources',
|
label: 'Resources',
|
||||||
id: 'resources',
|
id: 'resources',
|
||||||
children: <ResourcesTable resources={release.info?.resources ?? []} />,
|
children: <ResourcesTable />,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Values',
|
label: 'Values',
|
||||||
|
|
|
@ -8,6 +8,24 @@ import { GenericResource } from '../../../types';
|
||||||
|
|
||||||
import { ResourcesTable } from './ResourcesTable';
|
import { ResourcesTable } from './ResourcesTable';
|
||||||
|
|
||||||
|
// Mock the necessary hooks
|
||||||
|
const mockUseCurrentStateAndParams = vi.fn();
|
||||||
|
const mockUseEnvironmentId = vi.fn();
|
||||||
|
const mockUseHelmRelease = vi.fn();
|
||||||
|
|
||||||
|
vi.mock('@uirouter/react', async (importOriginal: () => Promise<object>) => ({
|
||||||
|
...(await importOriginal()),
|
||||||
|
useCurrentStateAndParams: () => mockUseCurrentStateAndParams(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('@/react/hooks/useEnvironmentId', () => ({
|
||||||
|
useEnvironmentId: () => mockUseEnvironmentId(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../../queries/useHelmRelease', () => ({
|
||||||
|
useHelmRelease: () => mockUseHelmRelease(),
|
||||||
|
}));
|
||||||
|
|
||||||
const successResources = [
|
const successResources = [
|
||||||
{
|
{
|
||||||
kind: 'ValidatingWebhookConfiguration',
|
kind: 'ValidatingWebhookConfiguration',
|
||||||
|
@ -108,8 +126,26 @@ const failedResources = [
|
||||||
];
|
];
|
||||||
|
|
||||||
function renderResourcesTable(resources: GenericResource[]) {
|
function renderResourcesTable(resources: GenericResource[]) {
|
||||||
|
// Setup mock return values
|
||||||
|
mockUseEnvironmentId.mockReturnValue(3);
|
||||||
|
mockUseCurrentStateAndParams.mockReturnValue({
|
||||||
|
params: {
|
||||||
|
name: 'test-release',
|
||||||
|
namespace: 'default',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
mockUseHelmRelease.mockReturnValue({
|
||||||
|
data: {
|
||||||
|
info: {
|
||||||
|
resources,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isLoading: false,
|
||||||
|
error: null,
|
||||||
|
});
|
||||||
|
|
||||||
const Wrapped = withTestQueryProvider(withTestRouter(ResourcesTable));
|
const Wrapped = withTestQueryProvider(withTestRouter(ResourcesTable));
|
||||||
return render(<Wrapped resources={resources} />);
|
return render(<Wrapped />);
|
||||||
}
|
}
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
|
|
@ -1,23 +1,47 @@
|
||||||
import { Datatable } from '@@/datatables';
|
import { useCurrentStateAndParams } from '@uirouter/react';
|
||||||
import { createPersistedStore } from '@@/datatables/types';
|
|
||||||
|
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
||||||
|
|
||||||
|
import { Datatable, TableSettingsMenu } from '@@/datatables';
|
||||||
|
import {
|
||||||
|
createPersistedStore,
|
||||||
|
refreshableSettings,
|
||||||
|
TableSettingsWithRefreshable,
|
||||||
|
} from '@@/datatables/types';
|
||||||
import { useTableState } from '@@/datatables/useTableState';
|
import { useTableState } from '@@/datatables/useTableState';
|
||||||
import { Widget } from '@@/Widget';
|
import { Widget } from '@@/Widget';
|
||||||
|
import { TableSettingsMenuAutoRefresh } from '@@/datatables/TableSettingsMenuAutoRefresh';
|
||||||
|
|
||||||
import { GenericResource } from '../../../types';
|
import { useHelmRelease } from '../../queries/useHelmRelease';
|
||||||
|
|
||||||
import { columns } from './columns';
|
import { columns } from './columns';
|
||||||
import { useResourceRows } from './useResourceRows';
|
import { useResourceRows } from './useResourceRows';
|
||||||
|
|
||||||
type Props = {
|
|
||||||
resources: GenericResource[];
|
|
||||||
};
|
|
||||||
|
|
||||||
const storageKey = 'helm-resources';
|
const storageKey = 'helm-resources';
|
||||||
const settingsStore = createPersistedStore(storageKey, 'resourceType');
|
|
||||||
|
|
||||||
export function ResourcesTable({ resources }: Props) {
|
export function createStore(storageKey: string) {
|
||||||
|
return createPersistedStore<TableSettingsWithRefreshable>(
|
||||||
|
storageKey,
|
||||||
|
'name',
|
||||||
|
(set) => ({
|
||||||
|
...refreshableSettings(set),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const settingsStore = createStore('helm-resources');
|
||||||
|
|
||||||
|
export function ResourcesTable() {
|
||||||
|
const environmentId = useEnvironmentId();
|
||||||
|
const { params } = useCurrentStateAndParams();
|
||||||
|
const { name, namespace } = params;
|
||||||
|
|
||||||
const tableState = useTableState(settingsStore, storageKey);
|
const tableState = useTableState(settingsStore, storageKey);
|
||||||
const rows = useResourceRows(resources);
|
const helmReleaseQuery = useHelmRelease(environmentId, name, namespace, {
|
||||||
|
showResources: true,
|
||||||
|
refetchInterval: tableState.autoRefreshRate * 1000,
|
||||||
|
});
|
||||||
|
const rows = useResourceRows(helmReleaseQuery.data?.info?.resources);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Widget>
|
<Widget>
|
||||||
|
@ -32,6 +56,14 @@ export function ResourcesTable({ resources }: Props) {
|
||||||
disableSelect
|
disableSelect
|
||||||
getRowId={(row) => row.id}
|
getRowId={(row) => row.id}
|
||||||
data-cy="helm-resources-datatable"
|
data-cy="helm-resources-datatable"
|
||||||
|
renderTableSettings={() => (
|
||||||
|
<TableSettingsMenu>
|
||||||
|
<TableSettingsMenuAutoRefresh
|
||||||
|
value={tableState.autoRefreshRate}
|
||||||
|
onChange={(value) => tableState.setAutoRefreshRate(value)}
|
||||||
|
/>
|
||||||
|
</TableSettingsMenu>
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
</Widget>
|
</Widget>
|
||||||
);
|
);
|
||||||
|
|
|
@ -6,11 +6,15 @@ import { ResourceRow } from '../types';
|
||||||
|
|
||||||
import { columnHelper } from './helper';
|
import { columnHelper } from './helper';
|
||||||
|
|
||||||
export const name = columnHelper.accessor((row) => row.name.label, {
|
// `${row.name.label}/${row.resourceType}` reduces shuffling when the name is the same but the resourceType is different
|
||||||
|
export const name = columnHelper.accessor(
|
||||||
|
(row) => `${row.name.label}/${row.resourceType}`,
|
||||||
|
{
|
||||||
header: 'Name',
|
header: 'Name',
|
||||||
cell: Cell,
|
cell: Cell,
|
||||||
id: 'name',
|
id: 'name',
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
function Cell({ row }: CellContext<ResourceRow, string>) {
|
function Cell({ row }: CellContext<ResourceRow, string>) {
|
||||||
const { name } = row.original;
|
const { name } = row.original;
|
||||||
|
|
|
@ -27,11 +27,15 @@ const statusToColorMap: Record<string, StatusBadgeType> = {
|
||||||
Unknown: 'mutedLite',
|
Unknown: 'mutedLite',
|
||||||
};
|
};
|
||||||
|
|
||||||
export function useResourceRows(resources: GenericResource[]): ResourceRow[] {
|
export function useResourceRows(resources?: GenericResource[]): ResourceRow[] {
|
||||||
return useMemo(() => getResourceRows(resources), [resources]);
|
return useMemo(() => getResourceRows(resources), [resources]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getResourceRows(resources: GenericResource[]): ResourceRow[] {
|
function getResourceRows(resources?: GenericResource[]): ResourceRow[] {
|
||||||
|
if (!resources) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
return resources.map(getResourceRow);
|
return resources.map(getResourceRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,19 +16,22 @@ export function useHelmRelease<T = HelmRelease>(
|
||||||
options: {
|
options: {
|
||||||
select?: (data: HelmRelease) => T;
|
select?: (data: HelmRelease) => T;
|
||||||
showResources?: boolean;
|
showResources?: boolean;
|
||||||
|
refetchInterval?: number;
|
||||||
} = {}
|
} = {}
|
||||||
) {
|
) {
|
||||||
|
const { select, showResources, refetchInterval } = options;
|
||||||
return useQuery(
|
return useQuery(
|
||||||
[environmentId, 'helm', 'releases', namespace, name, options.showResources],
|
[environmentId, 'helm', 'releases', namespace, name, options.showResources],
|
||||||
() =>
|
() =>
|
||||||
getHelmRelease(environmentId, name, {
|
getHelmRelease(environmentId, name, {
|
||||||
namespace,
|
namespace,
|
||||||
showResources: options.showResources,
|
showResources,
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
enabled: !!environmentId && !!name && !!namespace,
|
enabled: !!environmentId && !!name && !!namespace,
|
||||||
...withGlobalError('Unable to retrieve helm application details'),
|
...withGlobalError('Unable to retrieve helm application details'),
|
||||||
select: options.select,
|
select,
|
||||||
|
refetchInterval,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue