generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 200
feat: Implement Model Rewrite and Traffic Splitting Logic #1820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
261d89f
infModelRewrite reconciler logic.
zetxqx 520f274
implments model rewrite and traffic splitting.
zetxqx c3c32f6
more efficient rewrite fetching per request.
zetxqx 19d12d6
register controller.
zetxqx 8923424
fix e2e.
zetxqx a109e90
add integration test, rename datasoter interface, change split to tar…
zetxqx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,20 +57,25 @@ type InferenceModelRewriteSpec struct { | |
| // If multiple InferenceModelRewrite resources target the same | ||
| // InferencePool, the controller will merge them based on precedence. | ||
| // | ||
| // **Timestamp Wins:** If two rules from different rewrites all matches, | ||
| // the rule from the *oldest* | ||
| // InferenceModelRewrite resource (determined by | ||
| // metadata.creationTimestamp) will be used. | ||
| // Across all rules specified on applicable rewrites, precedence MUST be | ||
| // given to the match having an "Exact" model match over a generic match | ||
| // (a rule with an empty `matches` array). | ||
| // | ||
| // If ties still exist across multiple InferenceModelRewrite resources (e.g. | ||
| // two rewrites both have an exact match for the same model), matching | ||
| // precedence MUST be determined by the oldest resource based on | ||
| // creation timestamp. | ||
| // | ||
| // If ties still exist within a single InferenceModelRewrite resource, the | ||
| // FIRST matching rule (in list order) is used. | ||
| // +required | ||
| Rules []InferenceModelRewriteRule `json:"rules"` | ||
| } | ||
|
|
||
| // InferenceModelRewriteRule defines the match criteria and corresponding action. | ||
| // | ||
| // A specific model name can only be matched by one rule across all | ||
| // rules attached to the same InferencePool. If multiple rules attempt | ||
| // to match the same model name, the oldest rule (by creationTimestamp) | ||
| // will be the only one considered valid. | ||
| // For details on how precedence is determined across multiple rules and | ||
| // InferenceModelRewrite resources, see the "Precedence and Conflict Resolution" | ||
| // section in InferenceModelRewriteSpec. | ||
| type InferenceModelRewriteRule struct { | ||
| // Matches defines the criteria for matching a request. | ||
| // If multiple match criteria are specified, a request matches if | ||
|
|
@@ -87,7 +92,7 @@ type InferenceModelRewriteRule struct { | |
| // +optional | ||
| // +kubebuilder:validation:MinItems=1 | ||
| // | ||
| Targets []TargetModel `json:"split,omitempty"` | ||
| Targets []TargetModel `json:"targets,omitempty"` | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found this is a miss. updated the "split" to "targets" |
||
| } | ||
|
|
||
| // TargetModel defines a weighted model destination for traffic distribution. | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
client-go/applyconfiguration/apix/v1alpha2/inferencemodelrewriterule.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| Copyright 2025 The Kubernetes Authors. | ||
| 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 controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
|
||
| "sigs.k8s.io/gateway-api-inference-extension/apix/v1alpha2" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/common" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore" | ||
| logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" | ||
| ) | ||
|
|
||
| type InferenceModelRewriteReconciler struct { | ||
| client.Reader | ||
| Datastore datastore.Datastore | ||
| PoolGKNN common.GKNN | ||
| } | ||
|
|
||
| func (c *InferenceModelRewriteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| logger := log.FromContext(ctx).V(logutil.DEFAULT) | ||
| ctx = ctrl.LoggerInto(ctx, logger) | ||
|
|
||
| logger.Info("Reconciling InferenceModelRewrite") | ||
|
|
||
| infModelRewrite := &v1alpha2.InferenceModelRewrite{} | ||
| notFound := false | ||
| if err := c.Get(ctx, req.NamespacedName, infModelRewrite); err != nil { | ||
| if !errors.IsNotFound(err) { | ||
| return ctrl.Result{}, fmt.Errorf("unable to get InferenceModelRewrite - %w", err) | ||
| } | ||
| notFound = true | ||
| } | ||
|
|
||
| isDeleted := !infModelRewrite.DeletionTimestamp.IsZero() | ||
| isPooRefUnmatch := infModelRewrite.Spec.PoolRef == nil || | ||
| infModelRewrite.Spec.PoolRef.Name != v1alpha2.ObjectName(c.PoolGKNN.Name) || | ||
| infModelRewrite.Spec.PoolRef.Group != v1alpha2.Group(c.PoolGKNN.Group) | ||
|
|
||
| if notFound || isDeleted || isPooRefUnmatch { | ||
| // InferenceModelRewrite object got deleted or changed the referenced pool. | ||
| c.Datastore.ModelRewriteDelete(req.NamespacedName) | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| // Add or update if the InferenceModelRewrite instance has a creation timestamp older than the existing entry of the model. | ||
| logger = logger.WithValues("poolRef", infModelRewrite.Spec.PoolRef) | ||
| c.Datastore.ModelRewriteSet(infModelRewrite) | ||
| logger.Info("Added/Updated InferenceModelRewrite") | ||
|
|
||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| func (c *InferenceModelRewriteReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { | ||
| return ctrl.NewControllerManagedBy(mgr). | ||
| For(&v1alpha2.InferenceModelRewrite{}). | ||
| WithEventFilter(predicate.Funcs{ | ||
| CreateFunc: func(e event.CreateEvent) bool { return c.eventPredicate(e.Object.(*v1alpha2.InferenceModelRewrite)) }, | ||
| UpdateFunc: func(e event.UpdateEvent) bool { | ||
| return c.eventPredicate(e.ObjectOld.(*v1alpha2.InferenceModelRewrite)) || c.eventPredicate(e.ObjectNew.(*v1alpha2.InferenceModelRewrite)) | ||
| }, | ||
| DeleteFunc: func(e event.DeleteEvent) bool { return c.eventPredicate(e.Object.(*v1alpha2.InferenceModelRewrite)) }, | ||
| GenericFunc: func(e event.GenericEvent) bool { return c.eventPredicate(e.Object.(*v1alpha2.InferenceModelRewrite)) }, | ||
| }). | ||
| Complete(c) | ||
| } | ||
|
|
||
| func (c *InferenceModelRewriteReconciler) eventPredicate(infModelRewrite *v1alpha2.InferenceModelRewrite) bool { | ||
| return infModelRewrite.Spec.PoolRef != nil && string(infModelRewrite.Spec.PoolRef.Name) == c.PoolGKNN.Name && string(infModelRewrite.Spec.PoolRef.Group) == c.PoolGKNN.Group | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nirrozenbaum @ahg-g @kfswain
I've updated the precedence rules for conflicting matches to better align with the HTTPRoute specification in the Kubernetes Gateway API. https://github.com/kubernetes-sigs/gateway-api/blob/f24f3a61f398c65ab629da1843cb65fd5ec9419f/apis/v1/httproute_types.go#L148-L209
The new precedence order is:
This approach is more intuitive and simplifies the implementation of efficient RewriteRule fetching per request. Specifically, when we find an exact match, we no longer need to compare it against less specific, generic rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SG, this matches what we had with InferenceModel also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!