From 85867ae29ce29f2b7d74bcf13b03ed543a8fe500 Mon Sep 17 00:00:00 2001 From: Radoslaw Szwajkowski Date: Thu, 14 Sep 2023 13:20:27 +0200 Subject: [PATCH] Replace concerns with V1VirtualMachine object in OpenshiftVM Other changes: 1. remove Concerns column from OpenShift VM list 2. add type guard utility function to test for concerns 3. add partial type definitions for KubeVirt VirtualMachine Signed-off-by: Radoslaw Szwajkowski --- .../OpenShiftVirtualMachinesList.tsx | 15 +--- .../OpenShiftVirtualMachinesRow.tsx | 3 +- .../components/VMConcernsCellRenderer.tsx | 5 +- .../helpers/getHighestPriorityConcern.ts | 11 ++- .../utils/helpers/hasConcerns.ts | 7 ++ .../types/src/types/k8s/V1VirtualMachine.ts | 76 +++++++++++++++++++ .../types/src/types/provider/openshift/VM.ts | 5 +- 7 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/hasConcerns.ts create mode 100644 packages/types/src/types/k8s/V1VirtualMachine.ts diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesList.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesList.tsx index bf902801c..3e908e78d 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesList.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesList.tsx @@ -1,6 +1,6 @@ import React from 'react'; -import { EnumToTuple, ResourceFieldFactory } from '@kubev2v/common'; +import { ResourceFieldFactory } from '@kubev2v/common'; import { ProviderVirtualMachinesList } from './components/ProviderVirtualMachinesList'; import { OpenShiftVirtualMachinesRow } from './OpenShiftVirtualMachinesRow'; @@ -19,19 +19,6 @@ const openShiftVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [ }, sortable: true, }, - { - resourceFieldId: 'concerns', - jsonPath: '$.concerns', - label: t('Concerns'), - isVisible: true, - sortable: true, - filter: { - type: 'enum', - primary: true, - placeholderLabel: t('Concerns'), - values: EnumToTuple({ Critical: 'Critical', Warning: 'Warning', Information: 'Information' }), - }, - }, ]; export const OpenShiftVirtualMachinesList: React.FC = ({ diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesRow.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesRow.tsx index 8b11970fb..42608a1fc 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesRow.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/OpenShiftVirtualMachinesRow.tsx @@ -3,11 +3,10 @@ import React from 'react'; import { ResourceField, RowProps } from '@kubev2v/common'; import { Td, Tr } from '@patternfly/react-table'; -import { VMCellProps, VMConcernsCellRenderer, VmData, VMNameCellRenderer } from './components'; +import { VMCellProps, VmData, VMNameCellRenderer } from './components'; const cellRenderers: Record> = { name: VMNameCellRenderer, - concerns: VMConcernsCellRenderer, }; const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdProps) => { diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/VMConcernsCellRenderer.tsx b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/VMConcernsCellRenderer.tsx index 457892503..c9bc47b24 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/VMConcernsCellRenderer.tsx +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/components/VMConcernsCellRenderer.tsx @@ -8,6 +8,8 @@ import { } from '@openshift-console/dynamic-plugin-sdk'; import { Stack, StackItem } from '@patternfly/react-core'; +import { hasConcerns } from '../utils/helpers/hasConcerns'; + import { VMCellProps } from './VMCellProps'; const statusIcons = { @@ -23,10 +25,11 @@ const categoryWeights = { }; export const VMConcernsCellRenderer: React.FC = ({ data }) => { + const concerns = hasConcerns(data?.vm) ? data.vm.concerns : []; return ( - {data?.vm?.concerns + {concerns ?.sort((a, b) => categoryWeights[a.category] - categoryWeights[b.category]) ?.map((c) => { return ( diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getHighestPriorityConcern.ts b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getHighestPriorityConcern.ts index 53aaa2f3a..1ca0ec29d 100644 --- a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getHighestPriorityConcern.ts +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/getHighestPriorityConcern.ts @@ -1,21 +1,24 @@ import { ProviderVirtualMachine } from '@kubev2v/types'; +import { hasConcerns } from './hasConcerns'; + type ConcernCategory = 'Critical' | 'Warning' | 'Information'; export const getHighestPriorityConcern = (vm: ProviderVirtualMachine): ConcernCategory => { - if (!vm?.concerns) { + const concerns = hasConcerns(vm) ? vm.concerns : []; + if (!concerns.length) { return undefined; } - if (vm.concerns.some((c) => c.category === 'Critical')) { + if (concerns.some((c) => c.category === 'Critical')) { return 'Critical'; } - if (vm.concerns.some((c) => c.category === 'Warning')) { + if (concerns.some((c) => c.category === 'Warning')) { return 'Warning'; } - if (vm.concerns.some((c) => c.category === 'Information')) { + if (concerns.some((c) => c.category === 'Information')) { return 'Information'; } diff --git a/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/hasConcerns.ts b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/hasConcerns.ts new file mode 100644 index 000000000..c684de3ab --- /dev/null +++ b/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/utils/helpers/hasConcerns.ts @@ -0,0 +1,7 @@ +import { Concern, OVirtVM, ProviderVirtualMachine } from '@kubev2v/types'; + +export function hasConcerns( + vm: ProviderVirtualMachine, +): vm is ProviderVirtualMachine & { concerns: Concern[] } { + return Array.isArray((vm as OVirtVM).concerns); +} diff --git a/packages/types/src/types/k8s/V1VirtualMachine.ts b/packages/types/src/types/k8s/V1VirtualMachine.ts new file mode 100644 index 000000000..712fb20c8 --- /dev/null +++ b/packages/types/src/types/k8s/V1VirtualMachine.ts @@ -0,0 +1,76 @@ +// https://github.com/kubev2v/forklift/blob/main/vendor/kubevirt.io/api/export/v1alpha1/types.go + +import { IoK8sApimachineryPkgApisMetaV1ObjectMeta } from '../../models'; + +// https://kubevirt.io/api-reference/master/definitions.html#_v1_virtualmachine +export interface V1VirtualMachine { + kind: 'VirtualMachine'; + apiVersion: 'kubevirt.io/v1'; + metadata?: IoK8sApimachineryPkgApisMetaV1ObjectMeta; + spec?: { + // Mutually exclusive with RunStrategy + // Running *bool `json:"running,omitempty" optional:"true"` + running?: boolean; + + // Running state indicates the requested running state of the VirtualMachineInstance + // mutually exclusive with Running + // RunStrategy *VirtualMachineRunStrategy `json:"runStrategy,omitempty" optional:"true"` + + // Template is the direct specification of VirtualMachineInstance + // Template *VirtualMachineInstanceTemplateSpec `json:"template"` + + // dataVolumeTemplates is a list of dataVolumes that the VirtualMachineInstance template can reference. + // DataVolumes in this list are dynamically created for the VirtualMachine and are tied to the VirtualMachine's life-cycle. + // DataVolumeTemplates []DataVolumeTemplateSpec `json:"dataVolumeTemplates,omitempty"` + }; + status?: { + // SnapshotInProgress is the name of the VirtualMachineSnapshot currently executing + // SnapshotInProgress *string `json:"snapshotInProgress,omitempty"` + // Created indicates if the virtual machine is created in the cluster + // Created bool `json:"created,omitempty"` + created?: boolean; + // Ready indicates if the virtual machine is running and ready + // Ready bool `json:"ready,omitempty"` + ready?: boolean; + // PrintableStatus is a human readable, high-level representation of the status of the virtual machine + // PrintableStatus VirtualMachinePrintableStatus `json:"printableStatus,omitempty"` + printableStatus: VirtualMachinePrintableStatus; + // Hold the state information of the VirtualMachine and its VirtualMachineInstance + // Conditions []VirtualMachineCondition `json:"conditions,omitempty" optional:"true"` + // StateChangeRequests indicates a list of actions that should be taken on a VMI + // e.g. stop a specific VMI then start a new one. + // StateChangeRequests []VirtualMachineStateChangeRequest `json:"stateChangeRequests,omitempty" optional:"true"` + // VolumeRequests indicates a list of volumes add or remove from the VMI template and + // hotplug on an active running VMI. + // +listType=atomic + // VolumeRequests []VirtualMachineVolumeRequest `json:"volumeRequests,omitempty" optional:"true"` + + // VolumeSnapshotStatuses indicates a list of statuses whether snapshotting is + // supported by each volume. + // VolumeSnapshotStatuses []VolumeSnapshotStatus `json:"volumeSnapshotStatuses,omitempty" optional:"true"` + }; +} + +type VirtualMachinePrintableStatus = + // VirtualMachineStatusStopped indicates that the virtual machine is currently stopped and isn't expected to start. + | 'Stopped' + // VirtualMachineStatusProvisioning indicates that cluster resources associated with the virtual machine + // (e.g., DataVolumes) are being provisioned and prepared. + | 'Provisioning' + // VirtualMachineStatusStarting indicates that the virtual machine is being prepared for running. + | 'Starting' + // VirtualMachineStatusRunning indicates that the virtual machine is running. + | 'Running' + // VirtualMachineStatusPaused indicates that the virtual machine is paused. + | 'Paused' + // VirtualMachineStatusStopping indicates that the virtual machine is in the process of being stopped. + | 'Stopping' + // VirtualMachineStatusTerminating indicates that the virtual machine is in the process of deletion, + // as well as its associated resources (VirtualMachineInstance, DataVolumes, …). + | 'Terminating' + // VirtualMachineStatusMigrating indicates that the virtual machine is in the process of being migrated + // to another host. + | 'Migrating' + // VirtualMachineStatusUnknown indicates that the state of the virtual machine could not be obtained, + // typically due to an error in communicating with the host on which it's running. + | 'Unknown'; diff --git a/packages/types/src/types/provider/openshift/VM.ts b/packages/types/src/types/provider/openshift/VM.ts index a98df82fe..dbdcb3a4e 100644 --- a/packages/types/src/types/provider/openshift/VM.ts +++ b/packages/types/src/types/provider/openshift/VM.ts @@ -1,7 +1,8 @@ -import { Concern } from '../base'; +import { V1VirtualMachine } from '../../k8s/V1VirtualMachine'; import { OpenshiftResource } from './Resource'; +// https://github.com/kubev2v/forklift/blob/main/pkg/controller/provider/web/ocp/vm.go export interface OpenshiftVM extends OpenshiftResource { - concerns: Concern[]; + object: V1VirtualMachine; }