Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template column for OpenShift VMs #742

Merged
merged 2 commits into from
Oct 2, 2023

Conversation

rszwajko
Copy link
Contributor

@rszwajko rszwajko commented Sep 27, 2023

Depends on #741

Display the name of the template used to create the VM.
The name is retrieved from label vm.kubevirt.io/template.

Reference-Url: https://kubevirt.io/user-guide/virtual_machines/templates/#relationship-between-templates-and-vms

image

@rszwajko rszwajko changed the title Add template column Add template column for OpenShift VMs Sep 27, 2023
@rszwajko rszwajko force-pushed the addTemplateColumn branch 3 times, most recently from 88fc76f to 4cbc3ef Compare September 28, 2023 14:09
@rszwajko rszwajko marked this pull request as ready for review September 28, 2023 15:22
@yaacov
Copy link
Member

yaacov commented Sep 28, 2023

@rszwajko can you clean the commits ?

Display the name of the template used to create the VM.
The name is retrieved from label 'vm.kubevirt.io/template'.

Reference-Url: https://kubevirt.io/user-guide/virtual_machines/templates/#relationship-between-templates-and-vms
Signed-off-by: Radoslaw Szwajkowski <[email protected]>
Comment on lines 15 to 17
<TableCell>
{(data?.vm as OpenshiftVM)?.object?.metadata?.labels?.['vm.kubevirt.io/template'] ?? ''}
</TableCell>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some thoughts about this renderer:
a. we should not need to cast, this is an Openshift specific method, it should already get the correct type.
b. this is a long renderer, it dos not look right implemented inline, i see two options, one is implementing a method that return the template string, another is implementing a cell rendered and using i

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not need to cast, this is an Openshift specific method, it should already get the correct type.

As already discussed, this is not happening because VMCellProps is not generic.

this is a long renderer, it dos not look right implemented inline, i see two options, one is implementing a method that return the template string, another is implementing a cell rendered and using i

The renderer is very simple, it's the the property path that is too long:) In the previous approach ("flat" objects) this was handled in the flattening method. In the current approach ("blob" objects) we need to dive deep to get the prop. Btw in the k8s world this seems a common pattern - adding a method for each nested prop would is not scalable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets consider a getVmTemplate(vm: ProviderVirtualMachine) if will check the type, and return the currect template for each type.

the code will look like:

<TableCell>{getVmTemplate(data.?vm)}</TableCell>

i would prefer using a method to get the template over the current way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets consider a getVmTemplate(vm: ProviderVirtualMachine) if will check the type, and return the currect

Done. Note that:

  1. right now it works only for OpenShift
  2. the same value will be retrieved in 2 different ways (jsonpath and accessor) - this impacts maintenance.
  3. following this pattern (accessor method per prop) will result in many accessor methods. In the long term we need a structured way to handle them i.e. folder structure and an utility file per type. As for now I've created a vmProps.ts utility.

{
resourceFieldId: 'template',
jsonPath: (data: VmData) =>
(data?.vm as OpenshiftVM)?.object?.metadata?.labels?.['vm.kubevirt.io/template'] ?? '',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a. we should not need to cast here
b. this is actually just a json path, why do you use a method ? what am i missing ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try:

jsonPath: ['object', 'metadata' ,'labels', 'vm.kubevirt.io/template'],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a. we should not need to cast here

The proposed solution with generics was dropped. If you have any other idea please share.

b. this is actually just a json path, why do you use a method ? what am i missing ?

To benefit from the type system and avoid i.e. typos.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea of json path is to make the code clear and clean, if you add a method here, you add casting, you add a long .? code snippet, I prefer using jsonPath if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea of json path is to make the code clear and clean

IMHO the opposite is true i.e. as stated above we lose type safety here.

if you add a method here, you add casting, you add a long .? code snippet

  1. Casting is needed because we dropped generics in VMCellProps that were implemented in the VM list screens. Compare the code above with that version:
    https://github.com/rszwajko/forklift-console-plugin/blob/fb778cf9e4b2ec24be6c754122233cda013de594/packages/forklift-console-plugin/src/modules/Providers/views/details/tabs/VirtualMachines/VSphereVirtualMachinesRow.tsx#L39
  2. Long path is needed because we dropped "flat" object approach. Using a string(jsonpath) is here similar to standard JS syntax.

I prefer using jsonPath if possible.

OK. Note that currently array of strings is not accepted - it's either a string or a method.

@rszwajko rszwajko marked this pull request as draft September 29, 2023 10:37
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
2.6% 2.6% Duplication

@rszwajko rszwajko marked this pull request as ready for review September 29, 2023 12:06
@yaacov yaacov merged commit e83ffe5 into kubev2v:main Oct 2, 2023
5 checks passed
@yaacov
Copy link
Member

yaacov commented Oct 2, 2023

#746 - add automation for kubevirt types

@@ -34,6 +34,18 @@ const openShiftVmFieldsMetadataFactory: ResourceFieldFactory = (t) => [
},
sortable: true,
},
{
resourceFieldId: 'template',
jsonPath: "$.vm.object.metadata.labels['vm.kubevirt.io/template']",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice !, didn't know you can do this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants