From e45b4f2fa776087783ae69bf124ec31a2c7c86f5 Mon Sep 17 00:00:00 2001 From: googs1025 Date: Fri, 20 Sep 2024 20:41:18 +0800 Subject: [PATCH] feature(webhook): add BackendRuntimeConfig resources validation --- pkg/webhook/playground_webhook.go | 17 +++++++++++++++++ test/integration/webhook/playground_test.go | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pkg/webhook/playground_webhook.go b/pkg/webhook/playground_webhook.go index f8af4474..094e4bb1 100644 --- a/pkg/webhook/playground_webhook.go +++ b/pkg/webhook/playground_webhook.go @@ -18,6 +18,7 @@ package webhook import ( "context" + "fmt" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/validation" @@ -122,5 +123,21 @@ func (w *PlaygroundWebhook) generateValidate(obj runtime.Object) field.ErrorList allErrs = append(allErrs, field.Forbidden(specPath.Child("modelClaims", "models"), "only one main model is allowed")) } } + + if playground.Spec.BackendRuntimeConfig != nil && playground.Spec.BackendRuntimeConfig.Resources != nil { + requirements := playground.Spec.BackendRuntimeConfig.Resources + for resourceName, limit := range requirements.Limits { + request, ok := requirements.Requests[resourceName] + if !ok { + continue + } + + if limit.Cmp(request) == -1 { + allErrs = append(allErrs, field.Forbidden(specPath.Child("backendRuntimeConfig", "resources"), + fmt.Sprintf("limit (%v) for %s must be greater than or equal to request (%v)", limit, resourceName, request))) + } + } + } + return allErrs } diff --git a/test/integration/webhook/playground_test.go b/test/integration/webhook/playground_test.go index aa2f8aa4..f9fcdafb 100644 --- a/test/integration/webhook/playground_test.go +++ b/test/integration/webhook/playground_test.go @@ -69,6 +69,18 @@ var _ = ginkgo.Describe("Playground default and validation", func() { }, failed: true, }), + ginkgo.Entry("invalid BackendRuntime resources", &testValidatingCase{ + playground: func() *inferenceapi.Playground { + return wrapper.MakePlayground("playground", ns.Name).Replicas(1).ModelClaim("llama3-8b").BackendRuntimeRequest("cpu", "10").BackendRuntimeLimit("cpu", "5").Obj() + }, + failed: true, + }), + ginkgo.Entry("valid BackendRuntime resources with limit but without request", &testValidatingCase{ + playground: func() *inferenceapi.Playground { + return wrapper.MakePlayground("playground", ns.Name).Replicas(1).ModelClaim("llama3-8b").BackendRuntimeLimit("cpu", "5").Obj() + }, + failed: false, + }), ginkgo.Entry("no model claim declared", &testValidatingCase{ playground: func() *inferenceapi.Playground { return wrapper.MakePlayground("playground", ns.Name).Replicas(1).Obj()