Skip to content

Commit

Permalink
Replace concerns with V1VirtualMachine object in OpenshiftVM
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rszwajko committed Sep 15, 2023
1 parent 98d1719 commit 71604e6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<ProviderVirtualMachinesProps> = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, React.FC<VMCellProps>> = {
name: VMNameCellRenderer,
concerns: VMConcernsCellRenderer,
};

const renderTd = ({ resourceData, resourceFieldId, resourceFields }: RenderTdProps) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -23,10 +25,11 @@ const categoryWeights = {
};

export const VMConcernsCellRenderer: React.FC<VMCellProps> = ({ data }) => {
const concerns = hasConcerns(data?.vm) ? data.vm.concerns : [];
return (
<TableCell>
<Stack>
{data?.vm?.concerns
{concerns
?.sort((a, b) => categoryWeights[a.category] - categoryWeights[b.category])
?.map((c) => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
76 changes: 76 additions & 0 deletions packages/types/src/types/k8s/V1VirtualMachine.ts
Original file line number Diff line number Diff line change
@@ -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.

Check warning on line 44 in packages/types/src/types/k8s/V1VirtualMachine.ts

View workflow job for this annotation

GitHub Actions / Run linter and tests

Unknown word: "hotplug"
// +listType=atomic
// VolumeRequests []VirtualMachineVolumeRequest `json:"volumeRequests,omitempty" optional:"true"`

// VolumeSnapshotStatuses indicates a list of statuses whether snapshotting is

Check warning on line 48 in packages/types/src/types/k8s/V1VirtualMachine.ts

View workflow job for this annotation

GitHub Actions / Run linter and tests

Unknown word: "snapshotting"
// 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';
5 changes: 3 additions & 2 deletions packages/types/src/types/provider/openshift/VM.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 71604e6

Please sign in to comment.