generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 184
refactor scheduler filters package #797
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
k8s-ci-robot
merged 2 commits into
kubernetes-sigs:main
from
nirrozenbaum:filter-refactor
May 8, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,81 @@ | ||
| /* | ||
| 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 filter | ||
|
|
||
| import ( | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" | ||
| logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" | ||
| ) | ||
|
|
||
| // DecisionTreeFilter applies current fitler, and then recursively applies next filters | ||
| // depending success or failure of the current filter. | ||
| // It can be used to construct a flow chart algorithm. | ||
| type DecisionTreeFilter struct { | ||
| Current plugins.Filter | ||
| // NextOnSuccess filter will be applied after successfully applying the current filter. | ||
| // The filtered results will be passed to the next filter. | ||
| NextOnSuccess plugins.Filter | ||
| // NextOnFailure filter will be applied if current filter results in no pods. | ||
| // The original input will be passed to the next filter. | ||
| NextOnFailure plugins.Filter | ||
| // NextOnSuccessOrFailure is a convenience field to configure the next filter regardless of the | ||
| // success or failure of the current filter. | ||
| // NOTE: When using NextOnSuccessOrFailure, both nextOnSuccess and nextOnFailure SHOULD be nil. | ||
| // However if that's not the case, nextOnSuccess and nextOnFailure will be used, instead of | ||
| // NextOnSuccessOrFailure, in the success and failure scenarios, respectively. | ||
| NextOnSuccessOrFailure plugins.Filter | ||
| } | ||
|
|
||
| // Name returns the name of the filter. | ||
| func (f *DecisionTreeFilter) Name() string { | ||
| if f == nil { | ||
| return "nil" | ||
| } | ||
| return f.Current.Name() | ||
| } | ||
|
|
||
| // Filter filters out pods that doesn't meet the filter criteria. | ||
| func (f *DecisionTreeFilter) Filter(ctx *types.SchedulingContext, pods []types.Pod) []types.Pod { | ||
| loggerTrace := ctx.Logger.V(logutil.TRACE) | ||
| filteredPod := f.Current.Filter(ctx, pods) | ||
|
|
||
| next := f.NextOnSuccessOrFailure | ||
| if len(filteredPod) > 0 { | ||
| if f.NextOnSuccess == nil && f.NextOnSuccessOrFailure == nil { | ||
| // No succeeding filters to run, return. | ||
| return filteredPod | ||
| } | ||
| if f.NextOnSuccess != nil { | ||
| next = f.NextOnSuccess | ||
| } | ||
| loggerTrace.Info("Filter succeeded", "filter", f.Name(), "next", next.Name(), "filteredPodCount", len(filteredPod)) | ||
| // On success, pass the filtered result to the next filter. | ||
| return next.Filter(ctx, filteredPod) | ||
| } else { | ||
| if f.NextOnFailure == nil && f.NextOnSuccessOrFailure == nil { | ||
| // No succeeding filters to run, return. | ||
| return filteredPod | ||
| } | ||
| if f.NextOnFailure != nil { | ||
| next = f.NextOnFailure | ||
| } | ||
| loggerTrace.Info("Filter failed", "filter", f.Name(), "next", next.Name()) | ||
| // On failure, pass the initial set of pods to the next filter. | ||
| return next.Filter(ctx, pods) | ||
| } | ||
| } |
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.
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.
after having some experience with setting a custom plugins, the recommended way would be to set in main.go the scheduler using NewSchedulerWithConfig.
no need for global var. default configuration is defined inside NewScheduler instead.