Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions pkg/model-serving-controller/webhook/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,16 @@
minRoleReplicasPath := field.NewPath("spec").Child("template").Child("gangPolicy").Child("minRoleReplicas")

// Create a map of role names for quick lookup
roleNames := make(map[string]bool)
for _, role := range ms.Spec.Template.Roles {
roleNames[role.Name] = true
roleNames := make(map[string]workloadv1alpha1.Role)
for _, roleElement := range ms.Spec.Template.Roles {
roleNames[roleElement.Name] = roleElement
}
Comment on lines +212 to 215

// Validate each minRoleReplicas entry
for roleName, minReplicas := range minRoleReplicas {
// Check if the role exists
if !roleNames[roleName] {
role, ok := roleMap[roleName]

Check failure on line 220 in pkg/model-serving-controller/webhook/validator.go

View workflow job for this annotation

GitHub Actions / Go tests

undefined: roleMap

Check failure on line 220 in pkg/model-serving-controller/webhook/validator.go

View workflow job for this annotation

GitHub Actions / build

undefined: roleMap (typecheck)

Check failure on line 220 in pkg/model-serving-controller/webhook/validator.go

View workflow job for this annotation

GitHub Actions / build

undefined: roleMap) (typecheck)
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 roleMap is undefined in this scope. It should be roleNames to match the map initialized on line 212. This will cause a compilation error.

Suggested change
role, ok := roleMap[roleName]
role, ok := roleNames[roleName]

if !ok {
allErrs = append(allErrs, field.Invalid(
minRoleReplicasPath.Key(roleName),
roleName,
Expand All @@ -227,34 +228,29 @@
}

// Find the role to check its actual replicas
for _, role := range ms.Spec.Template.Roles {
if role.Name == roleName {
/// Calculate total replicas for this role
// minRoleReplicas is compared against the number of Role replicas
replicas := int32(1)
if role.Replicas != nil {
replicas = *role.Replicas
}
// Calculate total replicas for this role
// minRoleReplicas is compared against the number of Role replicas
replicas := int32(1)
if role.Replicas != nil {
replicas = *role.Replicas
}

// Validate minReplicas doesn't exceed total replicas
if minReplicas > replicas {
allErrs = append(allErrs, field.Invalid(
minRoleReplicasPath.Key(roleName),
minReplicas,
fmt.Sprintf("minRoleReplicas (%d) for role %s cannot exceed replicas (%d)", minReplicas, roleName, replicas),
))
}
// Validate minReplicas doesn't exceed total replicas
if minReplicas > replicas {
allErrs = append(allErrs, field.Invalid(
minRoleReplicasPath.Key(roleName),
minReplicas,
fmt.Sprintf("minRoleReplicas (%d) for role %s cannot exceed replicas (%d)", minReplicas, roleName, replicas),
))
}

// Validate minReplicas is non-negative
if minReplicas < 0 {
allErrs = append(allErrs, field.Invalid(
minRoleReplicasPath.Key(roleName),
minReplicas,
fmt.Sprintf("minRoleReplicas for role %s must be non-negative", roleName),
))
}
break
}
// Validate minReplicas is non-negative
if minReplicas < 0 {
allErrs = append(allErrs, field.Invalid(
minRoleReplicasPath.Key(roleName),
minReplicas,
fmt.Sprintf("minRoleReplicas for role %s must be non-negative", roleName),
))
}
}

Expand Down
Loading