|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package framework |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + scheduling "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
| 23 | +) |
| 24 | + |
| 25 | +// Plugin is the parent type for all the scheduling framework plugins. |
| 26 | +type Plugin interface { |
| 27 | + Name() string |
| 28 | +} |
| 29 | + |
| 30 | +type Endpoint struct { |
| 31 | + State EndpointState |
| 32 | + Score float64 |
| 33 | +} |
| 34 | + |
| 35 | +type EndpointState struct { |
| 36 | + // storage is per Scheduling Cycle, and so has no thread-safe concerns. |
| 37 | + storage map[string]any |
| 38 | +} |
| 39 | + |
| 40 | +type SchedulingResult struct { |
| 41 | + results map[string][]Endpoint |
| 42 | +} |
| 43 | + |
| 44 | +type Scheduler interface { |
| 45 | + // ProfileSelection selects scheduling profiles through the implemented |
| 46 | + // logic, and returns: |
| 47 | + // - A subset of the registered scheduling profiles to be ran |
| 48 | + // - A bool flagging if this profileSet should return its result to ProfileSelection or complete with a result |
| 49 | + ProfileSelection(data scheduling.CycleState, results map[string][]Endpoint) (profiles map[string]SchedulingProfile, recurse bool) |
| 50 | + |
| 51 | + // SchedulingProfiles lists all of the scheduling profiles registered |
| 52 | + // with the scheduler. |
| 53 | + SchedulingProfiles() map[string]SchedulingProfile |
| 54 | + |
| 55 | + // SchedulingResult takes the output of the result(s) of the scheduling cycle(s) |
| 56 | + // and makes sense of the data to be consumed by request control. |
| 57 | + // For example: suppose you have 2 profiles ShadowBoxing Profile & Production Profile. |
| 58 | + // SchedulingResult would know to simply log the result of ShadowBoxing |
| 59 | + // profile, and do nothing else with it. |
| 60 | + SchedulingResult(profileResults map[string][]Endpoint) SchedulingResult |
| 61 | +} |
| 62 | + |
| 63 | +// SchedulingProfile is used to describe a profile that will |
| 64 | +// run for a given scheduling cycle. |
| 65 | +type SchedulingProfile struct { |
| 66 | + // Name of the profile |
| 67 | + Name string |
| 68 | + // Filters lists all Filter plugins associated with this Profile. Filters |
| 69 | + // are optional. |
| 70 | + Filters []Filter |
| 71 | + // Scorers lists all Score plugins associated with this Profile. At |
| 72 | + // least 1 scorer must be registered for a profile to be valid. |
| 73 | + Scorers map[Scorer]int |
| 74 | + // Selection returns the function that picks the endpoint(s). |
| 75 | + Selection Picker |
| 76 | +} |
| 77 | + |
| 78 | +// Preschedule will be ran at the start of a scheduling cycle. This should be |
| 79 | +// scoped to any foundational work needed that is custom to this scheduling |
| 80 | +// profile. |
| 81 | +type PreSchedule interface { |
| 82 | + Plugin |
| 83 | + PreSchedule(ctx context.Context, state scheduling.CycleState, endpoints []Endpoint) |
| 84 | +} |
| 85 | + |
| 86 | +// Filter runs before any scoring, and remove endpoints that are not fit for |
| 87 | +// selection. The framework will return an error to the client if the endpoints |
| 88 | +// are filtered to zero. |
| 89 | +type Filter interface { |
| 90 | + Plugin |
| 91 | + Filter(ctx context.Context, state scheduling.CycleState, endpoints []Endpoint) []Endpoint |
| 92 | +} |
| 93 | + |
| 94 | +// Scorer applies a score to each remaining endpoint provided. Scorers SHOULD |
| 95 | +// keep their score values in a normalized range: [0-1]. Any weighting should |
| 96 | +// be added at the SchedulingProfile configuration level. |
| 97 | +type Scorer interface { |
| 98 | + Plugin |
| 99 | + Score(ctx context.Context, state scheduling.CycleState, endpoints []Endpoint) []Endpoint |
| 100 | +} |
| 101 | + |
| 102 | +// Picker selects the endpoint(s) from the provided list of scored endpoints. |
| 103 | +// Picker MUST return, one endpoint at minimum. |
| 104 | +type Picker interface { |
| 105 | + Plugin |
| 106 | + Pick(ctx context.Context, state scheduling.CycleState, endpoints []Endpoint) []Endpoint |
| 107 | +} |
| 108 | + |
| 109 | +// PostSchedule runs per-scheduling cycle, and is part of a scheduling profile. |
| 110 | +// PostSchedule performs any remaining work needed for the scheduling cycle. |
| 111 | +// PostSchedule is not expected to change any values of the parameters. |
| 112 | +type PostSchedule interface { |
| 113 | + Plugin |
| 114 | + PostSchedule(ctx context.Context, state scheduling.CycleState, selectedEndpoints []Endpoint) |
| 115 | +} |
0 commit comments