Skip to content

Commit

Permalink
Merge pull request #1170 from yaacov/show-vm-conditions
Browse files Browse the repository at this point in the history
🐾 Plan -> vm list don't show plan status conditions
  • Loading branch information
yaacov authored May 23, 2024
2 parents ed0e2ac + 92ba467 commit 718651c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
import { useForkliftTranslation } from 'src/utils/i18n';

import { loadUserSettings, ResourceFieldFactory } from '@kubev2v/common';
import { V1beta1PlanSpecVms, V1beta1PlanStatusMigrationVms } from '@kubev2v/types';
import {
V1beta1PlanSpecVms,
V1beta1PlanStatusConditions,
V1beta1PlanStatusMigrationVms,
} from '@kubev2v/types';

import { PlanVMsDeleteButton } from '../components';
import { PlanData, VMData } from '../types';
Expand All @@ -27,6 +31,15 @@ const fieldsMetadataFactory: ResourceFieldFactory = (t) => [
},
sortable: true,
},
{
resourceFieldId: 'conditions',
jsonPath: (obj: VMData) => {
return obj?.conditions?.[0]?.category;
},
label: t('Conditions'),
isVisible: true,
sortable: true,
},
];

const PageWithSelection = StandardPageWithSelection<VMData>;
Expand All @@ -48,12 +61,22 @@ export const PlanVirtualMachinesList: FC<{ obj: PlanData }> = ({ obj }) => {
const vmDict: Record<string, V1beta1PlanStatusMigrationVms> = {};
migrationVirtualMachines.forEach((m) => (vmDict[m.id] = m));

const conditions = plan?.status?.conditions?.filter((c) => c?.items && c.items.length > 0);
const conditionsDict: Record<string, V1beta1PlanStatusConditions[]> = {};
conditions?.forEach((c) => {
c.items.forEach((i) => {
const { id: vmID } = extractIdAndNameFromConditionItem(i);
conditionsDict[vmID] ? conditionsDict[vmID].push(c) : (conditionsDict[vmID] = [c]);
});
});

const vmData: VMData[] = virtualMachines.map((m) => ({
specVM: m,
statusVM: vmDict[m.id],
pods: [],
jobs: [],
pvcs: [],
conditions: conditionsDict[m.id],
targetNamespace: plan?.spec?.targetNamespace,
}));

Expand Down Expand Up @@ -83,3 +106,26 @@ export const PlanVirtualMachinesList: FC<{ obj: PlanData }> = ({ obj }) => {

return <PageWithSelection {...extendedProps} />;
};

/**
* Extracts the ID and name from a condition item string.
*
* This function parses a string containing condition item details and extracts the ID and name.
* The string format expected is something like "id:<some_id> name:'<some_name>'".
*
* @param {string} input - The string containing the condition item details.
* @returns {{ id: string; name: string }} An object containing the extracted ID and name.
*/
function extractIdAndNameFromConditionItem(input: string): { id: string; name: string } {
const idMatch = /id:([^ ]+)/.exec(input);
const nameMatch = /name:'([^']+)'/.exec(input);

if (!idMatch || !nameMatch) {
return { id: '', name: '' };
}

return {
id: idMatch[1],
name: nameMatch[1],
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { ResourceField, RowProps } from '@kubev2v/common';
import { Td } from '@kubev2v/common';

import { PlanVMsCellProps } from '../components';
import { ConditionsCellRenderer, PlanVMsCellProps } from '../components';
import { NameCellRenderer } from '../components/NameCellRenderer';
import { VMData } from '../types';

Expand Down Expand Up @@ -33,6 +33,7 @@ const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdPro

const cellRenderers: Record<string, React.FC<PlanVMsCellProps>> = {
name: NameCellRenderer,
conditions: ConditionsCellRenderer,
};

interface RenderTdProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { TableCell } from 'src/modules/Providers/utils';

import { YellowExclamationTriangleIcon } from '@openshift-console/dynamic-plugin-sdk';
import { Label, Level, LevelItem } from '@patternfly/react-core';

import { PlanVMsCellProps } from './PlanVMsCellProps';

export const ConditionsCellRenderer: React.FC<PlanVMsCellProps> = ({ data }) => {
const condition = data?.conditions?.[0];
if (!condition) {
return <></>;
}

return (
<TableCell>
<Level hasGutter>
<LevelItem>
<Label isCompact color="orange" icon={<YellowExclamationTriangleIcon />}>
{condition.category}
</Label>
</LevelItem>
<LevelItem>{condition.message}</LevelItem>
</Level>
</TableCell>
);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @index(['./*', /style/g], f => `export * from '${f.path}';`)
export * from './ConditionsCellRenderer';
export * from './MigrationVMsCancelButton';
export * from './NameCellRenderer';
export * from './PlanVMsCellProps';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IoK8sApiCoreV1PersistentVolumeClaim,
IoK8sApiCoreV1Pod,
V1beta1PlanSpecVms,
V1beta1PlanStatusConditions,
V1beta1PlanStatusMigrationVms,
} from '@kubev2v/types';

Expand All @@ -12,5 +13,6 @@ export type VMData = {
pods: IoK8sApiCoreV1Pod[];
jobs: IoK8sApiBatchV1Job[];
pvcs: IoK8sApiCoreV1PersistentVolumeClaim[];
conditions?: V1beta1PlanStatusConditions[];
targetNamespace: string;
};

0 comments on commit 718651c

Please sign in to comment.