forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigloader.go
More file actions
165 lines (136 loc) · 5.46 KB
/
Copy pathconfigloader.go
File metadata and controls
165 lines (136 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
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 loader
import (
"errors"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/gateway-api-inference-extension/api/config/v1alpha1"
configapi "sigs.k8s.io/gateway-api-inference-extension/api/config/v1alpha1"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework"
)
var scheme = runtime.NewScheme()
func init() {
configapi.SchemeBuilder.Register(configapi.RegisterDefaults)
utilruntime.Must(configapi.Install(scheme))
}
// Load config from supplied text that was converted to []byte
func LoadConfig(configBytes []byte, handle plugins.Handle) (*configapi.EndpointPickerConfig, error) {
config := &configapi.EndpointPickerConfig{}
codecs := serializer.NewCodecFactory(scheme, serializer.EnableStrict)
err := runtime.DecodeInto(codecs.UniversalDecoder(), configBytes, config)
if err != nil {
return nil, fmt.Errorf("the configuration is invalid - %w", err)
}
// instantiate loaded plugins
if err = instantiatePlugins(config.Plugins, handle); err != nil {
return nil, fmt.Errorf("failed to instantiate plugins - %w", err)
}
if err = validateSchedulingProfiles(config); err != nil {
return nil, fmt.Errorf("failed to validate scheduling profiles - %w", err)
}
return config, nil
}
func LoadSchedulerConfig(configProfiles []v1alpha1.SchedulingProfile, handle plugins.Handle) (*scheduling.SchedulerConfig, error) {
profiles := map[string]*framework.SchedulerProfile{}
for _, namedProfile := range configProfiles {
profile := framework.NewSchedulerProfile()
for _, plugin := range namedProfile.Plugins {
referencedPlugin := handle.Plugin(plugin.PluginRef)
if scorer, ok := referencedPlugin.(framework.Scorer); ok {
if plugin.Weight == nil {
return nil, fmt.Errorf("scorer '%s' is missing a weight", plugin.PluginRef)
}
referencedPlugin = framework.NewWeightedScorer(scorer, *plugin.Weight)
}
if err := profile.AddPlugins(referencedPlugin); err != nil {
return nil, fmt.Errorf("failed to load scheduler config - %w", err)
}
}
profiles[namedProfile.Name] = profile
}
var profileHandler framework.ProfileHandler
for pluginName, plugin := range handle.GetAllPluginsWithNames() {
if theProfileHandler, ok := plugin.(framework.ProfileHandler); ok {
if profileHandler != nil {
return nil, fmt.Errorf("only one profile handler is allowed. Both %s and %s are profile handlers", profileHandler.TypedName().Name, pluginName)
}
profileHandler = theProfileHandler
}
}
if profileHandler == nil {
return nil, errors.New("no profile handler was specified")
}
return scheduling.NewSchedulerConfig(profileHandler, profiles), nil
}
func instantiatePlugins(configuredPlugins []configapi.PluginSpec, handle plugins.Handle) error {
pluginNames := sets.New[string]() // set of plugin names, a name must be unique
for _, pluginConfig := range configuredPlugins {
if pluginConfig.Type == "" {
return fmt.Errorf("plugin definition for '%s' is missing a type", pluginConfig.Name)
}
if pluginNames.Has(pluginConfig.Name) {
return fmt.Errorf("plugin name '%s' used more than once", pluginConfig.Name)
}
pluginNames.Insert(pluginConfig.Name)
factory, ok := plugins.Registry[pluginConfig.Type]
if !ok {
return fmt.Errorf("plugin type '%s' is not found in registry", pluginConfig.Type)
}
plugin, err := factory(pluginConfig.Name, pluginConfig.Parameters, handle)
if err != nil {
return fmt.Errorf("failed to instantiate the plugin type '%s' - %w", pluginConfig.Type, err)
}
handle.AddPlugin(pluginConfig.Name, plugin)
}
return nil
}
func validateSchedulingProfiles(config *configapi.EndpointPickerConfig) error {
if len(config.SchedulingProfiles) == 0 {
return errors.New("there must be at least one scheduling profile in the configuration")
}
profileNames := sets.New[string]()
for _, profile := range config.SchedulingProfiles {
if profile.Name == "" {
return errors.New("SchedulingProfile must have a name")
}
if profileNames.Has(profile.Name) {
return fmt.Errorf("the name '%s' has been specified for more than one SchedulingProfile", profile.Name)
}
profileNames.Insert(profile.Name)
if len(profile.Plugins) == 0 {
return fmt.Errorf("SchedulingProfile '%s' must have at least one plugin", profile.Name)
}
for _, plugin := range profile.Plugins {
if len(plugin.PluginRef) == 0 {
return fmt.Errorf("SchedulingProfile '%s' plugins must have a plugin reference", profile.Name)
}
notFound := true
for _, pluginConfig := range config.Plugins {
if plugin.PluginRef == pluginConfig.Name {
notFound = false
break
}
}
if notFound {
return errors.New(plugin.PluginRef + " is a reference to an undefined Plugin")
}
}
}
return nil
}