-
Notifications
You must be signed in to change notification settings - Fork 670
kubevirt: enhance memory validation and template selection #3909
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { UpdateOptions } from '../../types'; | ||
| import { | ||
| iGetVmSettingAttribute, | ||
| iGetVmSettingValue, | ||
| } from '../../../selectors/immutable/vm-settings'; | ||
| import { iGetLoadedCommonData } from '../../../selectors/immutable/selectors'; | ||
| import { VMSettingsField, VMWizardProps } from '../../../types'; | ||
| import { iGetTemplateValidations } from '../../../../../selectors/immutable/template/selectors'; | ||
| import { | ||
| iGetRelevantTemplate, | ||
| iGetRelevantTemplates, | ||
| } from '../../../../../selectors/immutable/template/combined'; | ||
| import { TemplateValidations } from '../../../../../utils/validations/template/template-validations'; | ||
|
|
||
| const getValidationsFromTemplates = (templates): TemplateValidations[] => | ||
| templates.map( | ||
| (relevantTemplate) => new TemplateValidations(iGetTemplateValidations(relevantTemplate)), | ||
| ); | ||
|
|
||
| export const getTemplateValidations = (options: UpdateOptions): TemplateValidations[] => { | ||
| const { getState, id } = options; | ||
| const state = getState(); | ||
|
|
||
| const userTemplateName = iGetVmSettingValue(state, id, VMSettingsField.USER_TEMPLATE); | ||
| const os = iGetVmSettingAttribute(state, id, VMSettingsField.OPERATING_SYSTEM); | ||
| const flavor = iGetVmSettingAttribute(state, id, VMSettingsField.FLAVOR); | ||
| const workload = iGetVmSettingAttribute(state, id, VMSettingsField.WORKLOAD_PROFILE); | ||
|
|
||
| const relevantOptions = { | ||
| userTemplateName, | ||
| os, | ||
| workload, | ||
| flavor, | ||
| }; | ||
|
|
||
| const iUserTemplates = iGetLoadedCommonData(state, id, VMWizardProps.userTemplates); | ||
| const iCommonTemplates = iGetLoadedCommonData(state, id, VMWizardProps.commonTemplates); | ||
|
|
||
| if (userTemplateName || (flavor && os && workload)) { | ||
| // all information is filled to select a final template | ||
| const relevantTemplate = iGetRelevantTemplate( | ||
| iUserTemplates, | ||
| iCommonTemplates, | ||
| relevantOptions, | ||
| ); | ||
| return getValidationsFromTemplates(relevantTemplate ? [relevantTemplate] : []); | ||
| } | ||
|
|
||
| const relevantTemplates = iGetRelevantTemplates( | ||
| iUserTemplates, | ||
| iCommonTemplates, | ||
| relevantOptions, | ||
| ); | ||
|
|
||
| return getValidationsFromTemplates(relevantTemplates.toArray()); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,11 +41,10 @@ import { | |
| import { validatePositiveInteger } from '../../../../utils/validations/common'; | ||
| import { pluralize } from '../../../../utils/strings'; | ||
| import { vmSettingsOrder } from '../initial-state/vm-settings-tab-initial-state'; | ||
| import { TemplateValidations } from '../../../../utils/validations/template/template-validations'; | ||
| import { combineIntegerValidationResults } from '../../../../utils/validations/template/utils'; | ||
| import { getValidationUpdate } from './utils'; | ||
| import { | ||
| getTemplateValidations, | ||
| runValidation, | ||
| } from './common-template-validations/common-templates-validations'; | ||
| import { getTemplateValidations } from './utils/templates-validations'; | ||
|
|
||
| const validateVm: VmSettingsValidator = (field, options) => { | ||
| const { getState, id } = options; | ||
|
|
@@ -101,32 +100,25 @@ export const validateOperatingSystem: VmSettingsValidator = (field) => { | |
|
|
||
| const memoryValidation: VmSettingsValidator = (field, options): ValidationObject => { | ||
| const memValueGB = iGetFieldValue(field); | ||
| if (!memValueGB) { | ||
| if (memValueGB == null || memValueGB === '') { | ||
| return null; | ||
| } | ||
| const memValueBytes = memValueGB * 1024 ** 3; | ||
| const validations = getTemplateValidations(options, VMSettingsField.MEMORY); | ||
| const validations = getTemplateValidations(options); | ||
| if (validations.length === 0) { | ||
| return null; | ||
| validations.push(new TemplateValidations()); // add empty validation for positive integer if it is missing one | ||
|
||
| } | ||
|
|
||
| const validationResult = runValidation(validations, memValueBytes); | ||
|
|
||
| if (!validationResult.isValid) { | ||
| // Must have failed all validations, including first one: | ||
| const validation = validations[0]; | ||
| let customMessage = validationResult.errorMsg; | ||
|
|
||
| if ('min' in validation && 'max' in validation) { | ||
| customMessage = `Memory must be between ${validation.min / 1024 ** 3}GB and ${validation.max / | ||
| 1024 ** 3} GB`; | ||
| } else if ('min' in validation) { | ||
| customMessage = `Memory must be above ${validation.min / 1024 ** 3}GB`; | ||
| } else if ('max' in validation) { | ||
| customMessage = `Memory must be below ${validation.max / 1024 ** 3}GB`; | ||
| } | ||
| const validationResults = validations | ||
| .map((v) => v.validateMemory(memValueBytes)) | ||
| .filter(({ isValid }) => !isValid); | ||
|
|
||
| return asValidationObject(customMessage, ValidationErrorType.Error); | ||
| if (validationResults.length === validations.length) { | ||
| // every template failed its validations - we cannot choose one | ||
| return combineIntegerValidationResults(validationResults, { | ||
| defaultMin: 0, | ||
| isDefaultMinInclusive: false, | ||
| }); | ||
| } | ||
|
|
||
| return null; | ||
|
|
@@ -177,6 +169,7 @@ const validationConfig: VMSettingsValidationConfig = { | |
| VMSettingsField.OPERATING_SYSTEM, | ||
| VMSettingsField.WORKLOAD_PROFILE, | ||
| ], | ||
| detectCommonDataChanges: [VMWizardProps.userTemplates, VMWizardProps.commonTemplates], | ||
| validator: memoryValidation, | ||
| }, | ||
| }; | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1