@@ -19,11 +19,11 @@ package loader
19
19
import (
20
20
"errors"
21
21
"fmt"
22
- "os"
23
22
24
23
"k8s.io/apimachinery/pkg/runtime"
25
24
"k8s.io/apimachinery/pkg/runtime/serializer"
26
25
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
26
+ "k8s.io/apimachinery/pkg/util/sets"
27
27
28
28
"sigs.k8s.io/gateway-api-inference-extension/api/config/v1alpha1"
29
29
configapi "sigs.k8s.io/gateway-api-inference-extension/api/config/v1alpha1"
@@ -39,77 +39,54 @@ func init() {
39
39
utilruntime .Must (configapi .Install (scheme ))
40
40
}
41
41
42
- // Load config either from supplied text or from a file
43
- func LoadConfig (configText []byte , fileName string ) (* configapi.EndpointPickerConfig , error ) {
44
- var err error
45
- if len (configText ) == 0 {
46
- configText , err = os .ReadFile (fileName )
47
- if err != nil {
48
- return nil , fmt .Errorf ("failed to load config file. Error: %s" , err )
49
- }
50
- }
51
-
52
- theConfig := & configapi.EndpointPickerConfig {}
42
+ // Load config from supplied text that was converted to []byte
43
+ func LoadConfig (configBytes []byte , handle plugins.Handle ) (* configapi.EndpointPickerConfig , error ) {
44
+ config := & configapi.EndpointPickerConfig {}
53
45
54
46
codecs := serializer .NewCodecFactory (scheme , serializer .EnableStrict )
55
- err = runtime .DecodeInto (codecs .UniversalDecoder (), configText , theConfig )
47
+ err : = runtime .DecodeInto (codecs .UniversalDecoder (), configBytes , config )
56
48
if err != nil {
57
- return nil , fmt .Errorf ("the configuration is invalid. Error: %s " , err )
49
+ return nil , fmt .Errorf ("the configuration is invalid - %w " , err )
58
50
}
59
51
60
- // Validate loaded configuration
61
- err = validateConfiguration (theConfig )
62
- if err != nil {
63
- return nil , fmt .Errorf ("the configuration is invalid. error: %s" , err )
52
+ // instantiate loaded plugins
53
+ if err = instantiatePlugins (config .Plugins , handle ); err != nil {
54
+ return nil , fmt .Errorf ("failed to instantiate plugins - %w" , err )
64
55
}
65
- return theConfig , nil
66
- }
67
56
68
- func LoadPluginReferences (thePlugins []configapi.PluginSpec , handle plugins.Handle ) error {
69
- for _ , pluginConfig := range thePlugins {
70
- thePlugin , err := instantiatePlugin (pluginConfig , handle )
71
- if err != nil {
72
- return err
73
- }
74
- handle .Plugins ().AddPlugin (pluginConfig .Name , thePlugin )
57
+ if err = validateSchedulingProfiles (config ); err != nil {
58
+ return nil , fmt .Errorf ("failed to validate scheduling profiles - %w" , err )
75
59
}
76
- return nil
60
+
61
+ return config , nil
77
62
}
78
63
79
64
func LoadSchedulerConfig (configProfiles []v1alpha1.SchedulingProfile , handle plugins.Handle ) (* scheduling.SchedulerConfig , error ) {
80
-
81
- var profiles = map [string ]* framework.SchedulerProfile {}
82
-
83
- for _ , configProfile := range configProfiles {
84
- profile := framework.SchedulerProfile {}
85
-
86
- for _ , plugin := range configProfile .Plugins {
87
- var err error
88
- thePlugin := handle .Plugins ().Plugin (plugin .PluginRef )
89
- if theScorer , ok := thePlugin .(framework.Scorer ); ok {
65
+ profiles := map [string ]* framework.SchedulerProfile {}
66
+ for _ , namedProfile := range configProfiles {
67
+ profile := framework .NewSchedulerProfile ()
68
+ for _ , plugin := range namedProfile .Plugins {
69
+ referencedPlugin := handle .Plugins ().Plugin (plugin .PluginRef )
70
+ if scorer , ok := referencedPlugin .(framework.Scorer ); ok {
90
71
if plugin .Weight == nil {
91
72
return nil , fmt .Errorf ("scorer '%s' is missing a weight" , plugin .PluginRef )
92
73
}
93
- thePlugin = framework .NewWeightedScorer (theScorer , * plugin .Weight )
74
+ referencedPlugin = framework .NewWeightedScorer (scorer , * plugin .Weight )
94
75
}
95
- err = profile .AddPlugins (thePlugin )
96
- if err != nil {
97
- return nil , err
76
+ if err := profile .AddPlugins (referencedPlugin ); err != nil {
77
+ return nil , fmt .Errorf ("failed to load scheduler config - %w" , err )
98
78
}
99
79
}
100
- profiles [configProfile .Name ] = & profile
80
+ profiles [namedProfile .Name ] = profile
101
81
}
102
82
103
83
var profileHandler framework.ProfileHandler
104
- var profileHandlerName string
105
-
106
- for pluginName , thePlugin := range handle .Plugins ().GetAllPluginsWithNames () {
107
- if theProfileHandler , ok := thePlugin .(framework.ProfileHandler ); ok {
84
+ for pluginName , plugin := range handle .Plugins ().GetAllPluginsWithNames () {
85
+ if theProfileHandler , ok := plugin .(framework.ProfileHandler ); ok {
108
86
if profileHandler != nil {
109
- return nil , fmt .Errorf ("only one profile handler is allowed. Both %s and %s are profile handlers" , profileHandlerName , pluginName )
87
+ return nil , fmt .Errorf ("only one profile handler is allowed. Both %s and %s are profile handlers" , profileHandler . TypedName (). Name , pluginName )
110
88
}
111
89
profileHandler = theProfileHandler
112
- profileHandlerName = pluginName
113
90
}
114
91
}
115
92
if profileHandler == nil {
@@ -119,62 +96,61 @@ func LoadSchedulerConfig(configProfiles []v1alpha1.SchedulingProfile, handle plu
119
96
return scheduling .NewSchedulerConfig (profileHandler , profiles ), nil
120
97
}
121
98
122
- func instantiatePlugin (pluginSpec configapi.PluginSpec , handle plugins.Handle ) (plugins.Plugin , error ) {
123
- factory , ok := plugins .Registry [pluginSpec .Type ]
124
- if ! ok {
125
- return nil , fmt .Errorf ("failed to instantiate the plugin. plugin type %s not found" , pluginSpec .Type )
126
- }
127
- thePlugin , err := factory (pluginSpec .Name , pluginSpec .Parameters , handle )
128
- if err != nil {
129
- return nil , fmt .Errorf ("failed to instantiate the plugin type %s. Error: %s" , pluginSpec .Type , err )
130
- }
131
- return thePlugin , err
132
- }
133
-
134
- func validateConfiguration (theConfig * configapi.EndpointPickerConfig ) error {
135
- names := make (map [string ]struct {})
99
+ func instantiatePlugins (configuredPlugins []configapi.PluginSpec , handle plugins.Handle ) error {
100
+ pluginNames := sets .New [string ]() // set of plugin names, a name must be unique
136
101
137
- for _ , pluginConfig := range theConfig . Plugins {
102
+ for _ , pluginConfig := range configuredPlugins {
138
103
if pluginConfig .Type == "" {
139
- return fmt .Errorf ("plugin definition for %s is missing a type" , pluginConfig .Name )
104
+ return fmt .Errorf ("plugin definition for '%s' is missing a type" , pluginConfig .Name )
140
105
}
141
106
142
- if _ , ok := names [ pluginConfig .Name ]; ok {
143
- return fmt .Errorf ("plugin name %s used more than once" , pluginConfig .Name )
107
+ if pluginNames . Has ( pluginConfig .Name ) {
108
+ return fmt .Errorf ("plugin name '%s' used more than once" , pluginConfig .Name )
144
109
}
145
- names [ pluginConfig .Name ] = struct {}{}
110
+ pluginNames . Insert ( pluginConfig .Name )
146
111
147
- _ , ok := plugins .Registry [pluginConfig .Type ]
112
+ factory , ok := plugins .Registry [pluginConfig .Type ]
148
113
if ! ok {
149
- return fmt .Errorf ("plugin type %s is not found" , pluginConfig .Type )
114
+ return fmt .Errorf ("plugin type '%s' is not found in registry " , pluginConfig .Type )
150
115
}
116
+
117
+ plugin , err := factory (pluginConfig .Name , pluginConfig .Parameters , handle )
118
+ if err != nil {
119
+ return fmt .Errorf ("failed to instantiate the plugin type '%s' - %w" , pluginConfig .Type , err )
120
+ }
121
+
122
+ handle .Plugins ().AddPlugin (pluginConfig .Name , plugin )
151
123
}
152
124
153
- if len (theConfig .SchedulingProfiles ) == 0 {
125
+ return nil
126
+ }
127
+
128
+ func validateSchedulingProfiles (config * configapi.EndpointPickerConfig ) error {
129
+ if len (config .SchedulingProfiles ) == 0 {
154
130
return errors .New ("there must be at least one scheduling profile in the configuration" )
155
131
}
156
132
157
- names = map [string ]struct {}{}
158
- for _ , profile := range theConfig .SchedulingProfiles {
133
+ profileNames := sets . New [string ]()
134
+ for _ , profile := range config .SchedulingProfiles {
159
135
if profile .Name == "" {
160
- return errors .New ("SchedulingProfiles need a name" )
136
+ return errors .New ("SchedulingProfile must have a name" )
161
137
}
162
138
163
- if _ , ok := names [ profile .Name ]; ok {
164
- return fmt .Errorf ("the name %s has been specified for more than one SchedulingProfile" , profile .Name )
139
+ if profileNames . Has ( profile .Name ) {
140
+ return fmt .Errorf ("the name '%s' has been specified for more than one SchedulingProfile" , profile .Name )
165
141
}
166
- names [ profile .Name ] = struct {}{}
142
+ profileNames . Insert ( profile .Name )
167
143
168
144
if len (profile .Plugins ) == 0 {
169
- return errors . New ( "SchedulingProfiles need at least one plugin" )
145
+ return fmt . Errorf ( "SchedulingProfile '%s' must have at least one plugin", profile . Name )
170
146
}
171
147
for _ , plugin := range profile .Plugins {
172
148
if len (plugin .PluginRef ) == 0 {
173
- return errors . New ("SchedulingProfile's plugins need a plugin reference" )
149
+ return fmt . Errorf ("SchedulingProfile '%s' plugins must have a plugin reference" , profile . Name )
174
150
}
175
151
176
152
notFound := true
177
- for _ , pluginConfig := range theConfig .Plugins {
153
+ for _ , pluginConfig := range config .Plugins {
178
154
if plugin .PluginRef == pluginConfig .Name {
179
155
notFound = false
180
156
break
0 commit comments