From 297b7dba1e915a6b74a3e9f8effe72188affaeca Mon Sep 17 00:00:00 2001 From: Amal Saidhanov Date: Wed, 18 Nov 2026 16:48:09 +0700 Subject: [PATCH] fix(webhook): count main models by role in playground validation The playground validation webhook counted main models by comparing the model name against the "main" role constant instead of inspecting the model role. As a result mainModelCount was effectively always zero and the "only one main model is allowed" check never triggered, letting a Playground with multiple main-role models pass admission. Compare against model.Role (defaulting a nil role to main), matching the logic already used by the Service webhook and the Playground defaulter. Add a unit test covering the single-main and multiple-main cases. Signed-off-by: Amal Saidhanov --- pkg/webhook/playground_webhook.go | 2 +- pkg/webhook/playground_webhook_test.go | 65 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 pkg/webhook/playground_webhook_test.go diff --git a/pkg/webhook/playground_webhook.go b/pkg/webhook/playground_webhook.go index 9e32a703..a61f1568 100644 --- a/pkg/webhook/playground_webhook.go +++ b/pkg/webhook/playground_webhook.go @@ -110,7 +110,7 @@ func (w *PlaygroundWebhook) generateValidate(obj runtime.Object) field.ErrorList mainModelCount := 0 for _, model := range playground.Spec.ModelClaims.Models { - if model.Name == coreapi.ModelName(coreapi.MainRole) { + if model.Role == nil || *model.Role == coreapi.MainRole { mainModelCount += 1 } } diff --git a/pkg/webhook/playground_webhook_test.go b/pkg/webhook/playground_webhook_test.go new file mode 100644 index 00000000..a8f3ae1c --- /dev/null +++ b/pkg/webhook/playground_webhook_test.go @@ -0,0 +1,65 @@ +/* +Copyright 2024 The InftyAI Team. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package webhook + +import ( + "strings" + "testing" + + inferenceapi "github.com/inftyai/llmaz/api/inference/v1alpha1" + "github.com/inftyai/llmaz/test/util/wrapper" +) + +func TestPlaygroundValidateMainModelCount(t *testing.T) { + testCases := []struct { + name string + playground *inferenceapi.Playground + wantErr bool + wantErrSubstr string + }{ + { + name: "single main model is valid", + playground: wrapper.MakePlayground("playground", "default"). + ModelClaims([]string{"llama3-8b"}, []string{"main"}).Obj(), + wantErr: false, + }, + { + name: "more than one main model is forbidden", + playground: wrapper.MakePlayground("playground", "default"). + ModelClaims([]string{"llama3-8b", "llama3-70b"}, []string{"main", "main"}).Obj(), + wantErr: true, + wantErrSubstr: "only one main model is allowed", + }, + } + + webhook := &PlaygroundWebhook{} + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := webhook.generateValidate(tc.playground).ToAggregate() + if tc.wantErr { + if err == nil { + t.Fatalf("expected a validation error, but got none") + } + if !strings.Contains(err.Error(), tc.wantErrSubstr) { + t.Fatalf("expected error to contain %q, but got %q", tc.wantErrSubstr, err.Error()) + } + } else if err != nil { + t.Fatalf("expected no validation error, but got %q", err.Error()) + } + }) + } +}