Skip to content

fix: validate modelroute target entries#1061

Closed
pm-ju wants to merge 2 commits into
volcano-sh:mainfrom
pm-ju:fix-modelroute-target-validation
Closed

fix: validate modelroute target entries#1061
pm-ju wants to merge 2 commits into
volcano-sh:mainfrom
pm-ju:fix-modelroute-target-validation

Conversation

@pm-ju
Copy link
Copy Markdown
Contributor

@pm-ju pm-ju commented May 15, 2026

/kind bug

ModelRoute validation only checked that each rule had at least one targetModels entry. It did not reject nil target entries or empty modelServerName values.

That can admit invalid routes and also lets bad objects reach router runtime selection, where targetModels are dereferenced while building weighted routing choices.

Fixes #1060

This change:

  • rejects nil targetModels entries in the validating webhook
  • rejects empty targetModels[].modelServerName
  • adds datastore runtime guards for invalid target entries
  • adds webhook and datastore tests for these cases

Verification:
git diff --check
git diff --cached --check

Signed-off-by: pm-ju <pmdevops29@gmail.com>
Copilot AI review requested due to automatic review settings May 15, 2026 05:39
@volcano-sh-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign hzxuzhonghu for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces validation for TargetModels in both the datastore logic and the admission webhook, ensuring that target lists are not empty, entries are not nil, and ModelServerName is specified. Corresponding unit tests were added to verify these checks. Feedback suggests extending the webhook validation to enforce weight consistency across all targets in a rule, matching the existing runtime behavior to avoid debugging difficulties.

Comment on lines +183 to +192
for j, target := range rule.TargetModels {
targetField := ruleField.Child("targetModels").Index(j)
if target == nil {
allErrs = append(allErrs, field.Invalid(targetField, target, "target model must not be nil"))
continue
}
if target.ModelServerName == "" {
allErrs = append(allErrs, field.Required(targetField.Child("modelServerName"), "modelServerName must be specified"))
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The validating webhook should also enforce consistency in the weight field across all targetModels in a rule. Currently, the datastore runtime (in toWeightedSlice) rejects rules where some targets have weights and others don't, but the webhook allows them. This leads to valid-looking objects being ignored at runtime, which is difficult to debug. Please add a check to ensure that either all targetModels have a weight specified or none of them do.

Comment thread pkg/kthena-router/datastore/store.go Outdated
Comment on lines +1117 to +1122
if len(targets) == 0 {
return nil, fmt.Errorf("no target models specified in rule")
}
if targets[0] == nil {
return nil, fmt.Errorf("targetModel[0] must not be nil")
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// +kubebuilder:validation:MinItems=1
	// +kubebuilder:validation:MaxItems=16
	TargetModels []*TargetModel `json:"targetModels"`

we already get this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right, the empty targetModels case is already covered by MinItems=1, and the runtime guard was redundant.

I narrowed the PR to only validate empty targetModels[].modelServerName in the webhook, since required does not necessarily reject an explicitly empty string.

Signed-off-by: pm-ju <pmdevops29@gmail.com>
}
for j, target := range rule.TargetModels {
targetField := ruleField.Child("targetModels").Index(j)
if target != nil && target.ModelServerName == "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ptal // +kubebuilder:validation:required, so this is not needed

@pm-ju pm-ju closed this May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ModelRoute should reject invalid targetModels entries

5 participants