Skip to content

Refactor role validation logic in validator.go#879

Open
nalan2012 wants to merge 4 commits intovolcano-sh:mainfrom
nalan2012:nalan2012-patch-3
Open

Refactor role validation logic in validator.go#879
nalan2012 wants to merge 4 commits intovolcano-sh:mainfrom
nalan2012:nalan2012-patch-3

Conversation

@nalan2012
Copy link
Copy Markdown
Contributor

What type of PR is this?

What this PR does / why we need it:
optimize the logic in the validation file

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:
"None"

Copilot AI review requested due to automatic review settings April 12, 2026 08:42
@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 lizhencheng9527 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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Refactors validateGangPolicy role lookup to avoid repeatedly scanning roles when validating minRoleReplicas, aiming to improve efficiency and simplify logic.

Changes:

  • Build a role lookup map from ms.Spec.Template.Roles.
  • Replace nested role-search loop with direct map lookup.
  • Keep existing validations for unknown role names, minReplicas > replicas, and minReplicas < 0.
Comments suppressed due to low confidence (1)

pkg/model-serving-controller/webhook/validator.go:1

  • role is declared in the if initializer, so its scope is limited to the if statement; it is not accessible on lines 233–235. This will not compile. Fix by retrieving role in a separate statement before the if !ok check (e.g., role, ok := roleMap[roleName] then if !ok { ... continue }).
/*

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 212 to 215
roleMap := make(map[string]*workloadv1alpha1.Role)
for _, role := range ms.Spec.Template.Roles {
roleNames[role.Name] = true
roleMap[role.Name] = role
}
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

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

This assumes ms.Spec.Template.Roles iterates values that are *workloadv1alpha1.Role. If Roles is a slice of structs (common in Go APIs), this assignment won’t compile and/or may require taking addresses correctly (e.g., ranging by index and storing &ms.Spec.Template.Roles[i]). Align roleMap’s value type and population with the actual Roles element type to avoid type/scope pitfalls.

Copilot uses AI. Check for mistakes.
replicas = *role.Replicas
}
// Find the role in the roleMap then check its actual replicas
/// Calculate total replicas for this role
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

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

Go convention uses // for comments; /// is non-idiomatic and can be confusing in Go codebases. Replace with a single // (and consider folding it into the existing comment on the next line to avoid redundancy).

Suggested change
/// Calculate total replicas for this role
// Calculate total replicas for this role

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@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 optimizes the validateGangPolicy function by replacing a nested loop with a map-based lookup for roles. While this improves efficiency, the current implementation introduces two critical issues: a potential loop variable address pitfall when populating the map and a scoping error where the role variable is declared within an if statement initialization but accessed later in the loop, which will result in compilation errors.

for roleName, minReplicas := range minRoleReplicas {
// Check if the role exists
if !roleNames[roleName] {
if role, ok := roleMap[roleName]; !ok {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The variable role is declared within the scope of the if statement initialization. This means it is not accessible outside of the if block (e.g., at line 233), which will result in a compilation error. You should declare it before the if statement to ensure it remains in scope for the rest of the loop iteration.

		role, ok := roleMap[roleName]
		if !ok {

Copilot AI review requested due to automatic review settings April 12, 2026 13:24
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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

for roleName, minReplicas := range minRoleReplicas {
// Check if the role exists
if !roleNames[roleName] {
roleElement, ok := roleMap[roleName];
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

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

There’s a trailing semicolon after the map lookup (;) which is non-idiomatic in Go and will be removed by gofmt. Please remove the semicolon (and trailing whitespace) to match standard Go formatting.

Suggested change
roleElement, ok := roleMap[roleName];
roleElement, ok := roleMap[roleName]

Copilot uses AI. Check for mistakes.
replicas = *role.Replicas
}
// Find the role in the roleMap then check its actual replicas
/// Calculate total replicas for this role
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

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

The triple-slash comment marker (///) is inconsistent with typical Go commenting style and reads like an accidental duplication. Please replace with a normal // comment (or remove if redundant) to keep comments consistent and avoid confusion.

Suggested change
/// Calculate total replicas for this role
// Calculate total replicas for this role

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@LiZhenCheng9527 LiZhenCheng9527 left a comment

Choose a reason for hiding this comment

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

General LGTM
Please correct the errors in your comments.

@FAUST-BENCHOU
Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants