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 1839041: prefer common templates with newer version and timestamp #5544

Merged
merged 1 commit into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const VirtualMachinesPage: React.FC<VirtualMachinesPageProps> = (props) => {

const uniqueVMImportsByTargetName = _.sortedUniqBy(
[...(loadedVMImports || [])].sort((a, b) =>
getCreationTimestamp(a) > getCreationTimestamp(b) ? -1 : 1,
new Date(getCreationTimestamp(a)) > new Date(getCreationTimestamp(b)) ? -1 : 1,
),
(vmImport) => new VMImportWrappper(vmImport).getResolvedVMTargetName(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const TEMPLATE_OS_LABEL = 'os.template.kubevirt.io';
export const TEMPLATE_PARAM_VM_NAME = 'NAME';
export const TEMPLATE_PARAM_VM_NAME_DESC = 'Name for the new VM';
export const TEMPLATE_TYPE_LABEL = 'template.kubevirt.io/type';
export const TEMPLATE_VERSION_LABEL = 'template.kubevirt.io/version';
export const TEMPLATE_TYPE_VM = 'vm';
export const TEMPLATE_TYPE_BASE = 'base';
export const TEMPLATE_WORKLOAD_LABEL = 'workload.template.kubevirt.io';
Expand All @@ -26,8 +27,6 @@ export const TEMPLATE_VM_SIZE_LABEL = 'kubevirt.io/size';

export const LABEL_USED_TEMPLATE_NAME = 'vm.kubevirt.io/template';
export const LABEL_USED_TEMPLATE_NAMESPACE = 'vm.kubevirt.io/template.namespace';
export const LABEL_TEMPLATE_REVISION = 'vm.kubevirt.io/template.revision';
export const LABEL_TEMPLATE_VERSION = 'vm.kubevirt.io/template.version';

export const DEFAULT_RDP_PORT = 3389;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import { iGetIn } from '../../utils/immutable';
import { ILabels } from '../../types/template';

export const iGetLabels = (obj): ILabels => iGetIn(obj, ['metadata', 'labels']);
export const iGetCreationTimestamp = (obj): string =>
iGetIn(obj, ['metadata', 'creationTimestamp']);
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { List as ImmutableList, Map as ImmutableMap } from 'immutable';
import { getFlavorLabel, getOsLabel, getWorkloadLabel } from '../../vm-template/combined-dependent';
import { TEMPLATE_TYPE_BASE, TEMPLATE_TYPE_LABEL } from '../../../constants/vm';
import {
TEMPLATE_VERSION_LABEL,
TEMPLATE_TYPE_BASE,
TEMPLATE_TYPE_LABEL,
} from '../../../constants/vm';
import { iGetName } from '../../../components/create-vm-wizard/selectors/immutable/selectors';
import { ITemplate } from '../../../types/template';
import { iGetLabels } from '../common';
import { iGetCreationTimestamp, iGetLabels } from '../common';
import { compareVersions, splitVersion } from '../../../utils/sort';

type FindTemplateOptions = {
userTemplateName?: string;
Expand All @@ -17,6 +22,7 @@ const flavorOrder = {
medium: 1,
small: 2,
tiny: 3,
unknown: 4,
};

export const iGetRelevantTemplates = (
Expand Down Expand Up @@ -53,22 +59,37 @@ export const iGetRelevantTemplates = (
const aLabels = iGetLabels(a);
const bLabels = iGetLabels(b);

const aFlavor =
let aFlavor =
aLabels &&
flavorOrder[Object.keys(flavorOrder).find((f) => aLabels.has(getFlavorLabel(f)))];
const bFlavor =
let bFlavor =
bLabels &&
flavorOrder[Object.keys(flavorOrder).find((f) => bLabels.has(getFlavorLabel(f)))];

if (aFlavor == null) {
return -1;
aFlavor = flavorOrder.unknown;
}

if (bFlavor == null) {
return 1;
bFlavor = flavorOrder.unknown;
}

return aFlavor - bFlavor;
const flavorCMP = aFlavor - bFlavor;

if (flavorCMP !== 0) {
return flavorCMP;
}

const aVersion = aLabels?.get(TEMPLATE_VERSION_LABEL);
const bVersion = bLabels?.get(TEMPLATE_VERSION_LABEL);

const versionCMP = compareVersions(splitVersion(aVersion), splitVersion(bVersion)) * -1; // descending

if (versionCMP !== 0) {
return versionCMP;
}

return new Date(iGetCreationTimestamp(a)) > new Date(iGetCreationTimestamp(b)) ? -1 : 1;
}),
);
};
Expand Down
24 changes: 12 additions & 12 deletions frontend/packages/kubevirt-plugin/src/utils/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const ignoreCaseSort = <T>(
return array.sort((a, b) => resolve(a).localeCompare(resolve(b)));
};

const getOSVersion = (osID: string): number[] =>
export const splitVersion = (osID: string): number[] =>
(osID || '')
.split(/\D/)
.filter((x) => x)
Expand All @@ -58,29 +58,29 @@ const getOSVersion = (osID: string): number[] =>
*
*
* Compare the numbers between the two versions by the order of their appearance
* in the OS name.
* eg in the OS name.
*
* For example:
* osVersion1: [10,2] for OS: 'Windows 10 R2',
* osVersion2: [10] for OS: 'Windows 10',
* version1: [10,2] for OS: 'Windows 10 R2',
* version2: [10] for OS: 'Windows 10',
* (return 1)
*
* osVersion1: [9,10] for OS: 'ubuntu9.10',
* osVersion2: [10,4] for OS: 'ubuntu10.04',
* version1: [9,10] for OS: 'ubuntu9.10',
* version2: [10,4] for OS: 'ubuntu10.04',
* (return -1)
*
* return 0 when equal.
*
*/
export const compareVersions = (osVersion1: number[], osVersion2: number[]): number => {
if (!osVersion1 && !osVersion2) {
export const compareVersions = (version1: number[], version2: number[]): number => {
if (!version1 && !version2) {
return 0;
}

const osVer1 = osVersion1 || [];
const osVer2 = osVersion2 || [];
const finalVersion1 = version1 || [];
const finalVersion2 = version2 || [];

const zipped = _.zip(osVer1, osVer2);
const zipped = _.zip(finalVersion1, finalVersion2);
let idx = 0;
while (idx < zipped.length) {
/*
Expand Down Expand Up @@ -111,7 +111,7 @@ const descSortOSes = (os1: OperatingSystemRecord, os2: OperatingSystemRecord): n
return nameCMP * -1;
}

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

export const removeOSDups = (osArr: OperatingSystemRecord[]): OperatingSystemRecord[] =>
Expand Down