From bbaf93294373619b30089aa0b7d89b1fa7c7f731 Mon Sep 17 00:00:00 2001 From: Phillip Rhodes Date: Tue, 21 Jan 2025 10:33:41 -0500 Subject: [PATCH] Limit VM name length to max k8s label length --- locales/en/plugin__kubevirt-plugin.json | 1 + src/utils/resources/vm/utils/helpers.ts | 4 ++ .../CreateVMFooter/CreateVMFooter.tsx | 7 ++- .../components/DetailsLeftGrid.tsx | 53 ++++++++++++++----- .../TemplatesCatalogDrawerCreateForm.tsx | 14 +++++ .../hooks/useCreateDrawerForm.tsx | 7 ++- .../components/VMNameModal/VMNameModal.tsx | 13 ++--- .../components/VMNameModal/utils/utils.ts | 25 +++++++++ 8 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 src/utils/resources/vm/utils/helpers.ts create mode 100644 src/views/catalog/wizard/tabs/overview/components/VMNameModal/utils/utils.ts diff --git a/locales/en/plugin__kubevirt-plugin.json b/locales/en/plugin__kubevirt-plugin.json index 3ffd0266f..52fb65718 100644 --- a/locales/en/plugin__kubevirt-plugin.json +++ b/locales/en/plugin__kubevirt-plugin.json @@ -721,6 +721,7 @@ "Max. running migrations per cluster": "Max. running migrations per cluster", "Max. running migrations per node": "Max. running migrations per node", "Maximum latency": "Maximum latency", + "Maximum name length is {{ maxNameLength }} characters": "Maximum name length is {{ maxNameLength }} characters", "Measurement duration": "Measurement duration", "Mediated devices": "Mediated devices", "medium": "medium", diff --git a/src/utils/resources/vm/utils/helpers.ts b/src/utils/resources/vm/utils/helpers.ts new file mode 100644 index 000000000..bca65262a --- /dev/null +++ b/src/utils/resources/vm/utils/helpers.ts @@ -0,0 +1,4 @@ +import { MAX_K8S_NAME_LENGTH } from '@kubevirt-utils/utils/constants'; + +export const validateVMNameLength = (vmName: string): boolean => + vmName.length <= MAX_K8S_NAME_LENGTH; diff --git a/src/views/catalog/CreateFromInstanceTypes/components/CreateVMFooter/CreateVMFooter.tsx b/src/views/catalog/CreateFromInstanceTypes/components/CreateVMFooter/CreateVMFooter.tsx index 1b834d8c3..ba49e1750 100644 --- a/src/views/catalog/CreateFromInstanceTypes/components/CreateVMFooter/CreateVMFooter.tsx +++ b/src/views/catalog/CreateFromInstanceTypes/components/CreateVMFooter/CreateVMFooter.tsx @@ -36,6 +36,7 @@ import useKubevirtUserSettings from '@kubevirt-utils/hooks/useKubevirtUserSettin import useRHELAutomaticSubscription from '@kubevirt-utils/hooks/useRHELAutomaticSubscription/useRHELAutomaticSubscription'; import { getResourceUrl } from '@kubevirt-utils/resources/shared'; import useNamespaceUDN from '@kubevirt-utils/resources/udn/hooks/useNamespaceUDN'; +import { validateVMNameLength } from '@kubevirt-utils/resources/vm/utils/helpers'; import { vmSignal } from '@kubevirt-utils/store/customizeInstanceType'; import { createHeadlessService } from '@kubevirt-utils/utils/headless-service'; import { isEmpty } from '@kubevirt-utils/utils/utils'; @@ -204,7 +205,11 @@ const CreateVMFooter: FC = () => { }; const isDisabled = - isSubmitting || isEmpty(selectedBootableVolume) || !canCreateVM || !hasNameAndInstanceType; + isSubmitting || + isEmpty(selectedBootableVolume) || + !canCreateVM || + !hasNameAndInstanceType || + !validateVMNameLength(vmName); return (