Skip to content

Commit

Permalink
Add in proper handling of spaces and quotes in ISVC parsing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Xaenalt committed Dec 18, 2024
1 parent 2ee880d commit 42b88b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions frontend/src/api/k8s/inferenceServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -87,7 +89,7 @@ export const assembleInferenceService = (
path,
},
}),
args: nonEmptyArgs,
args: splitArgs,
env: nonEmptyEnvVars,
},
},
Expand Down Expand Up @@ -135,7 +137,7 @@ export const assembleInferenceService = (
path,
},
}),
args: nonEmptyArgs,
args: splitArgs,
env: nonEmptyEnvVars,
},
},
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/api/k8s/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 88 in frontend/src/api/k8s/utils.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/api/k8s/utils.ts#L88

Added line #L88 was not covered by tests
} else if (arg.startsWith("'") && arg.endsWith("'")) {
arg = arg.slice(1, -1); // Remove single quotes

Check warning on line 90 in frontend/src/api/k8s/utils.ts

View check run for this annotation

Codecov / codecov/patch

frontend/src/api/k8s/utils.ts#L90

Added line #L90 was not covered by tests
}

args.push(arg);
}

return args;
};

0 comments on commit 42b88b2

Please sign in to comment.