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

Bug 1826234: Filter out duplicate OSes in the new VM wizard #4897

Merged

Conversation

irosenzw
Copy link
Contributor

@irosenzw irosenzw commented Apr 2, 2020

templates in common-templates have some labels pointing to the same name annotation.
Thus, filter the duplicate OSes names presented in the VM wizard.

Signed-off-by: Ido Rosenzwig irosenzw@redhat.com

Screenshot from 2020-04-16 12-58-33

@openshift-ci-robot openshift-ci-robot added the component/kubevirt Related to kubevirt-plugin label Apr 2, 2020
Copy link
Member

@atiratree atiratree left a comment

Choose a reason for hiding this comment

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

What does it mean, in the future? Shouldn't we commit this change after it is ready?


export const removeOSDups = (osArr: OperatingSystemArray): OperatingSystemArray => {
const osNames: { [key: string]: string } = {};
osArr.forEach((os) => {
Copy link
Member

Choose a reason for hiding this comment

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

what happens when these OS are in different templates?

Copy link
Contributor Author

@irosenzw irosenzw Apr 5, 2020

Choose a reason for hiding this comment

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

@suomiy what do you mean?
we expect each record to be in the form: { name: "Fedora31", id: "someID" }.
Am I missing something ?

Copy link
Member

@atiratree atiratree Apr 6, 2020

Choose a reason for hiding this comment

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

if you have

template1: 
 { name: "Fedora29+", id: "fedora29" }.
template2 
 { name: "Fedora29+", id: "fedora31" }.

you will always choose template2 even though you should have chosen template1

* edited

Copy link
Member

@atiratree atiratree Apr 6, 2020

Choose a reason for hiding this comment

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

edited original comment

ah I see, I made a mistake - now it makes sense even with mutiple templates - we just choose the newer version one
and if the user wants his to be used he should change their own template to

template1: 
 { name: "Fedora29", id: "fedora29" }.

const osNames: { [key: string]: string } = {};
osArr.forEach((os) => {
if (osNames[os.name]) {
osNames[os.name] = os.id > osNames[os.name] ? os.id : osNames[os.name];
Copy link
Member

Choose a reason for hiding this comment

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

what happens when the comparison misbehaves? - shouldn't we test at least for numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked the comparison with several examples - they all went OK.
@suomiy can you think of a scenario that is would misbehave ?

Copy link
Member

Choose a reason for hiding this comment

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

for example "ubuntu9.10" > "ubuntu10.04"

@@ -110,3 +110,8 @@ export type VMSettingsValidationConfig = {
validator: VmSettingsValidator;
};
};

export type OperatingSystemArray = {
Copy link
Member

Choose a reason for hiding this comment

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

please do not create array types, also it should not be under wizard directory

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

: operatingSystemsNative;
const operatingSystems = removeOSDups(
openshiftFlag
? ignoreCaseSort(getOperatingSystems(vanillaTemplates, params), ['name'])
Copy link
Member

Choose a reason for hiding this comment

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

is this the only place we should worry about?

Copy link
Contributor Author

@irosenzw irosenzw Apr 5, 2020

Choose a reason for hiding this comment

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

@suomiy The template validations are working.
except the wizard, do you think on other places ?

Copy link
Member

Choose a reason for hiding this comment

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

IMO this functionality should be implemented in the following function to be applied everywhere in the wizard (for example this is used in template prefil)

getTemplateOperatingSystems = (templates: TemplateKind[]) in selectors/vm-template/advanced.ts

@irosenzw irosenzw changed the title CNV-4469: Filter out duplicate OSes in the new VM wizard [WIP] CNV-4469: Filter out duplicate OSes in the new VM wizard Apr 5, 2020
@openshift-ci-robot openshift-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 5, 2020
@irosenzw
Copy link
Contributor Author

irosenzw commented Apr 5, 2020

@suomiy "in the future" means that the templates in common-templates project are being modified to minimize the Operating-Systems dropdown list by changing the os.template annotations as follows:

instead of

name.os.template.kubevirt.io/fedora29: Fedora 29
name.os.template.kubevirt.io/fedora30: Fedora 30
name.os.template.kubevirt.io/fedora31: Fedora 31

have:

name.os.template.kubevirt.io/fedora29: Fedora 29+
name.os.template.kubevirt.io/fedora30: Fedora 29+
name.os.template.kubevirt.io/fedora31: Fedora 29+

osArr.forEach((os) => {
if (osNames[os.name]) {
osNames[os.name] =
os.id.toLocaleLowerCase().localeCompare(osNames[os.name].toLocaleLowerCase()) >= 0
Copy link
Member

@atiratree atiratree Apr 6, 2020

Choose a reason for hiding this comment

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

better, but still not working

@atiratree
Copy link
Member

@suomiy "in the future" means that the templates in common-templates project are being modified to minimize the Operating-Systems dropdown list by changing the os.template annotations as follows:

I see, that looks useful

@openshift-ci-robot openshift-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 16, 2020
@openshift-ci-robot openshift-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 19, 2020
@irosenzw irosenzw changed the title [WIP] CNV-4469: Filter out duplicate OSes in the new VM wizard CNV-4469: Filter out duplicate OSes in the new VM wizard Apr 19, 2020
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 19, 2020
* return 0 when equal.
*
*/
const compareVersions = (osVersion1: number[], osVersion2: number[]): number => {
Copy link
Member

Choose a reason for hiding this comment

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

nice function! just few nitpicks

Copy link
Member

Choose a reason for hiding this comment

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

a short test for this function would be also nice

*
*/
const compareVersions = (osVersion1: number[], osVersion2: number[]): number => {
if (!osVersion1 || !osVersion2) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!osVersion1 || !osVersion2) {
if (!osVersion1 && !osVersion2) {

should be 0 only in this case no?

otherwise the present one should be larger

export const operatingSystemsNative = [
import { OperatingSystemRecord } from '../../../types';

export const operatingSystemsNative: OperatingSystemRecord[] = [
Copy link
Member

Choose a reason for hiding this comment

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

could you move this file to src/constants/vm-templates/os.ts please?

osNames[os.name] = os.id;
}
});
return Object.keys(osNames).map((name) => {
Copy link
Member

Choose a reason for hiding this comment

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

this whole function would be more simpler and readable if we just used sort and then _.sortedUniqBy. It is much easier to add other conditions in the future

@irosenzw irosenzw changed the title CNV-4469: Filter out duplicate OSes in the new VM wizard Bug 1826234: Filter out duplicate OSes in the new VM wizard Apr 21, 2020
@openshift-ci-robot openshift-ci-robot added bugzilla/unspecified bugzilla/valid-bug Indicates that a referenced Bugzilla bug is valid for the branch this PR is targeting. labels Apr 21, 2020
@openshift-ci-robot
Copy link
Contributor

@irosenzw: This pull request references Bugzilla bug 1826234, which is valid. The bug has been moved to the POST state. The bug has been updated to refer to the pull request using the external bug tracker.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target release (4.5.0) matches configured target release for branch (4.5.0)
  • bug is in the state NEW, which is one of the valid states (NEW, ASSIGNED, ON_DEV, POST, POST)

In response to this:

Bug 1826234: Filter out duplicate OSes in the new VM wizard

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@irosenzw
Copy link
Contributor Author

/retest

return 0;
}

if (!osVersion1) {
Copy link
Member

Choose a reason for hiding this comment

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

these two ifs should just fill osVersion with empty array - you don't know what will come in the other version.

It could be [0] or [null]

can you please add a test for that?

};

const descSortOSes = (os1: OperatingSystemRecord, os2: OperatingSystemRecord): number => {
if (os1.name > os2.name) {
Copy link
Member

Choose a reason for hiding this comment

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

can we use localCompare for these two to be more accurate?

};

export const removeOSDups = (osArr: OperatingSystemRecord[]): OperatingSystemRecord[] =>
_.orderBy(_.uniqBy(osArr.sort(descSortOSes), 'name'), ['name']);
Copy link
Member

Choose a reason for hiding this comment

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

please can you remove orderBy? This is not the purpose of this function and is sorted by ignoreCaseSort down the road

return compareVersions(getOSVersion(os1.id), getOSVersion(os2.id)) * -1;
};

export const removeOSDups = (osArr: OperatingSystemRecord[]): OperatingSystemRecord[] =>
Copy link
Member

Choose a reason for hiding this comment

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

could you add a small test for this function as well?

const getOSVersion = (osName: string): number[] =>
osName
.split(/\D/)
.filter((x) => x)
Copy link
Member

Choose a reason for hiding this comment

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

this should test if it is a number

this would mean that 2.1.0 == 2.0.1

Copy link
Member

Choose a reason for hiding this comment

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

probably makes sense to do the map first

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@suomiy I think you are mistaken
2.1.0 will yield [2,1,0]
2.0.1 will yield [2,0,1]
The comparison afterwards will examine the two arrays by the value at the same index,
thus determine that 2.1.0 > 2.0.1

Copy link
Member

Choose a reason for hiding this comment

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

yes, you are right
I forgot that these are strings and not numbers.

"0" == 0
// > true
!"0" == !0
// > false

};

export const removeOSDups = (osArr: OperatingSystemRecord[]): OperatingSystemRecord[] =>
_.orderBy(_.uniqBy(osArr.sort(descSortOSes), 'name'), ['name']);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_.orderBy(_.uniqBy(osArr.sort(descSortOSes), 'name'), ['name']);
_.orderBy(_.uniqBy([...osArr].sort(descSortOSes), 'name'), ['name']);

better not to change the underlying array we don't own in this function

};

const descSortOSes = (os1: OperatingSystemRecord, os2: OperatingSystemRecord): number => {
if (os1.name.localeCompare(os2.name) > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

can we remember result of this call and just check if it is not equal to 0 and return it?

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 need to return it multiplied by (-1), but yeah... totally make sense

return 0;
}

const osVer1 = osVersion1 || [0];
Copy link
Member

Choose a reason for hiding this comment

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

these can be just [] no?

@@ -46,3 +47,74 @@ export const ignoreCaseSort = <T>(
};
return array.sort((a, b) => resolve(a).localeCompare(resolve(b)));
};

const getOSVersion = (osName: string): number[] =>
osName
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
osName
(osID || "")

};

const descSortOSes = (os1: OperatingSystemRecord, os2: OperatingSystemRecord): number => {
if (os1.name.localeCompare(os2.name) > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (os1.name.localeCompare(os2.name) > 0) {
if ((os1.name || "").localeCompare(os2.name) > 0) {

};

export const removeOSDups = (osArr: OperatingSystemRecord[]): OperatingSystemRecord[] =>
_.uniqBy([...osArr].sort(descSortOSes), 'name');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_.uniqBy([...osArr].sort(descSortOSes), 'name');
_.uniqBy(osArr.filter(a => a).sort(descSortOSes), 'name');

I guess this makes it more useful and we get a new array as well

In the future, the common-templates will have more labels pointing
to the same name annotation. Thus, filter the duplicate OSes names
presented in the VM wizard

Signed-off-by: Ido Rosenzwig <irosenzw@redhat.com>
@atiratree
Copy link
Member

/bugzilla refresh

@openshift-ci-robot
Copy link
Contributor

@suomiy: This pull request references Bugzilla bug 1826234, which is valid.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target release (4.5.0) matches configured target release for branch (4.5.0)
  • bug is in the state POST, which is one of the valid states (NEW, ASSIGNED, ON_DEV, POST, POST)

In response to this:

/bugzilla refresh

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@atiratree
Copy link
Member

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Apr 22, 2020
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: irosenzw, suomiy

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 22, 2020
@openshift-merge-robot openshift-merge-robot merged commit 05d49a6 into openshift:master Apr 22, 2020
@openshift-ci-robot
Copy link
Contributor

@irosenzw: All pull requests linked via external trackers have merged: openshift/console#4897. Bugzilla bug 1826234 has been moved to the MODIFIED state.

In response to this:

Bug 1826234: Filter out duplicate OSes in the new VM wizard

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@spadgett spadgett added this to the v4.5 milestone Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. bugzilla/valid-bug Indicates that a referenced Bugzilla bug is valid for the branch this PR is targeting. component/kubevirt Related to kubevirt-plugin lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants