From 42b88b2887510b218c0e930d4f3e30b471778eac Mon Sep 17 00:00:00 2001 From: Sean Pryor Date: Wed, 18 Dec 2024 11:55:32 -0500 Subject: [PATCH] Add in proper handling of spaces and quotes in ISVC parsing This adds a rudimentary parser that should handle most cases for inferenceservice argument parsing. Note, I'm not a JS expert, so it's possible there's some degerenate edge case with this, but in testing it worked fine --- frontend/src/api/k8s/inferenceServices.ts | 8 +++++--- frontend/src/api/k8s/utils.ts | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/frontend/src/api/k8s/inferenceServices.ts b/frontend/src/api/k8s/inferenceServices.ts index fded27ccbc..0a5c4a8ca4 100644 --- a/frontend/src/api/k8s/inferenceServices.ts +++ b/frontend/src/api/k8s/inferenceServices.ts @@ -15,7 +15,7 @@ import { ContainerResources } from '~/types'; import { AcceleratorProfileFormData } from '~/utilities/useAcceleratorProfileFormState'; import { AcceleratorProfileState } from '~/utilities/useReadAcceleratorState'; import { getModelServingProjects } from './projects'; -import { assemblePodSpecOptions } from './utils'; +import { assemblePodSpecOptions, parseCommandLine } from './utils'; export const assembleInferenceService = ( data: CreatingInferenceServiceObject, @@ -45,6 +45,8 @@ export const assembleInferenceService = ( const dataConnectionKey = secretKey || dataConnection; const nonEmptyArgs = servingRuntimeArgs?.filter(Boolean) || []; + // Ensure that we properly handle separating args + const splitArgs: string[] = nonEmptyArgs.flatMap(parseCommandLine); const nonEmptyEnvVars = servingRuntimeEnvVars?.filter((ev) => ev.name) || []; const updateInferenceService: InferenceServiceKind = inferenceService @@ -87,7 +89,7 @@ export const assembleInferenceService = ( path, }, }), - args: nonEmptyArgs, + args: splitArgs, env: nonEmptyEnvVars, }, }, @@ -135,7 +137,7 @@ export const assembleInferenceService = ( path, }, }), - args: nonEmptyArgs, + args: splitArgs, env: nonEmptyEnvVars, }, }, diff --git a/frontend/src/api/k8s/utils.ts b/frontend/src/api/k8s/utils.ts index 2399f3eca8..caec06914d 100644 --- a/frontend/src/api/k8s/utils.ts +++ b/frontend/src/api/k8s/utils.ts @@ -74,3 +74,24 @@ export const getshmVolume = (sizeLimit?: string): Volume => ({ name: 'shm', emptyDir: { medium: 'Memory', ...(sizeLimit && { sizeLimit }) }, }); + +export const parseCommandLine = (input: string): string[] => { + const args: string[] = []; + const regex = /(?:[^\s"']+|"[^"]*"|'[^']*')+/g; + let match: RegExpExecArray | null; + + while ((match = regex.exec(input)) !== null) { + let arg: string = match[0]; + + // Remove surrounding quotes if any + if (arg.startsWith('"') && arg.endsWith('"')) { + arg = arg.slice(1, -1).replace(/\\"/g, '"'); // Unescape double quotes + } else if (arg.startsWith("'") && arg.endsWith("'")) { + arg = arg.slice(1, -1); // Remove single quotes + } + + args.push(arg); + } + + return args; +};