|
| 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 | +// READER NOTE: Currently CycleState is assumed to have appropriate request data rather that making a new object. |
| 26 | + |
| 27 | +// Plugin is the parent type for all the scheduling framework plugins. |
| 28 | +type Plugin interface { |
| 29 | + Name() string |
| 30 | +} |
| 31 | + |
| 32 | +type Endpoint struct { |
| 33 | + State EndpointState |
| 34 | + Score float64 |
| 35 | +} |
| 36 | + |
| 37 | +type EndpointState struct { |
| 38 | + // storage is per Scheduling Cycle, and so has no thread-safe concerns. |
| 39 | + storage map[string]any |
| 40 | +} |
| 41 | + |
| 42 | +type SchedulingResult struct { |
| 43 | + results map[string][]Endpoint |
| 44 | +} |
| 45 | + |
| 46 | +// Scheduler is the implementation of a... scheduler. |
| 47 | +// The scheduler object is created at startup using the provided configuration. |
| 48 | +type Scheduler interface { |
| 49 | + // PreSchedule selects scheduling profiles through the implemented |
| 50 | + // logic, and returns: |
| 51 | + // - profiles - A subset of the registered scheduling profiles to be ran |
| 52 | + PreSchedule(request map[string]any, data scheduling.CycleState, results map[string][]Endpoint) map[string]SchedulingProfile |
| 53 | + |
| 54 | + // PostSchedule recieves the output of the result(s) of the scheduling cycle(s) |
| 55 | + // and makes sense of the data to be consumed by the calling system. |
| 56 | + // For example: suppose you have 2 profiles ShadowBoxing Profile & Production Profile. |
| 57 | + // PostSchedule would know to simply log the result of ShadowBoxing |
| 58 | + // profile, and do nothing else with it. |
| 59 | + PostSchedule(profileResults map[string][]Endpoint) SchedulingResult |
| 60 | +} |
| 61 | + |
| 62 | +// SchedulingProfile is used to describe a profile that will |
| 63 | +// run for a given scheduling cycle. |
| 64 | +type SchedulingProfile struct { |
| 65 | + // Name of the profile. |
| 66 | + Name string |
| 67 | + // Filters lists all Filter plugins associated with this Profile. Filters |
| 68 | + // are optional. |
| 69 | + Filters []Filter |
| 70 | + // Scorers lists all Score plugins associated with this Profile. Scorers |
| 71 | + // are optional. |
| 72 | + Scorers map[Scorer]int |
| 73 | + // Picker returns the function that picks the endpoint(s). Picker is required. |
| 74 | + Picker Picker |
| 75 | +} |
| 76 | + |
| 77 | +// Filter runs before any scoring, and remove endpoints that are not fit for |
| 78 | +// selection. The framework will return an error to the client if the endpoints |
| 79 | +// are filtered to zero. |
| 80 | +type Filter interface { |
| 81 | + Plugin |
| 82 | + Filter(ctx context.Context, state scheduling.CycleState, endpoints []Endpoint) []Endpoint |
| 83 | +} |
| 84 | + |
| 85 | +// Scorer applies a score to each remaining endpoint provided. Scorers SHOULD |
| 86 | +// keep their score values in a normalized range: [0-1]. Any weighting should |
| 87 | +// be added at the SchedulingProfile configuration level. |
| 88 | +type Scorer interface { |
| 89 | + Plugin |
| 90 | + Score(ctx context.Context, state scheduling.CycleState, endpoints []Endpoint) []Endpoint |
| 91 | +} |
| 92 | + |
| 93 | +// Picker selects the endpoint(s) from the provided list of scored endpoints. |
| 94 | +// Picker MUST return, one endpoint at minimum. |
| 95 | +type Picker interface { |
| 96 | + Plugin |
| 97 | + Pick(ctx context.Context, state scheduling.CycleState, endpoints []Endpoint) []Endpoint |
| 98 | +} |
| 99 | + |
| 100 | +type PostResponse interface { |
| 101 | + Plugin |
| 102 | + PostResponse(ctx context.Context, request map[string]any, response map[string]any) |
| 103 | +} |
0 commit comments