Skip to content

Commit 6057e36

Browse files
committed
Address reviewer feedback.
1 parent 2ad2746 commit 6057e36

File tree

3 files changed

+18
-42
lines changed

3 files changed

+18
-42
lines changed

pkg/epp/config/loader/configloader.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,14 @@ func RegisterFeatureGate(gate string) {
5252
// LoadRawConfig parses the raw configuration bytes, applies initial defaults, and extracts feature gates.
5353
// It does not instantiate plugins.
5454
func LoadRawConfig(configBytes []byte, logger logr.Logger) (*configapi.EndpointPickerConfig, map[string]bool, error) {
55-
// Decode JSON/YAML.
5655
rawConfig, err := decodeRawConfig(configBytes)
5756
if err != nil {
5857
return nil, nil, err
5958
}
60-
logger.V(1).Info("Loaded raw configuration", "config", rawConfig)
59+
logger.Info("Loaded raw configuration", "config", rawConfig)
6160

62-
// Sanitize data.
6361
applyStaticDefaults(rawConfig)
6462

65-
// Early validation of Feature Gates.
6663
// We validate gates early because they might dictate downstream loading logic.
6764
if err := validateFeatureGates(rawConfig.FeatureGates); err != nil {
6865
return nil, nil, fmt.Errorf("feature gate validation failed: %w", err)
@@ -80,23 +77,19 @@ func InstantiateAndConfigure(
8077
logger logr.Logger,
8178
) (*config.Config, error) {
8279

83-
// Instantiate user-configured plugins.
8480
if err := instantiatePlugins(rawConfig.Plugins, handle); err != nil {
8581
return nil, fmt.Errorf("plugin instantiation failed: %w", err)
8682
}
8783

88-
// Fill in the architectural gaps (inject required system plugins).
8984
if err := applySystemDefaults(rawConfig, handle); err != nil {
9085
return nil, fmt.Errorf("system default application failed: %w", err)
9186
}
9287
logger.Info("Effective configuration loaded", "config", rawConfig)
9388

94-
// Deep validation checks relationships between now-finalized profiles and plugins.
9589
if err := validateConfig(rawConfig); err != nil {
9690
return nil, fmt.Errorf("configuration validation failed: %w", err)
9791
}
9892

99-
// Build scheduler config.
10093
schedulerConfig, err := buildSchedulerConfig(rawConfig.SchedulingProfiles, handle)
10194
if err != nil {
10295
return nil, fmt.Errorf("scheduler config build failed: %w", err)
@@ -150,7 +143,6 @@ func buildSchedulerConfig(
150143

151144
profiles := make(map[string]*framework.SchedulerProfile)
152145

153-
// Build profiles.
154146
for _, cfgProfile := range configProfiles {
155147
fwProfile := framework.NewSchedulerProfile()
156148

@@ -178,7 +170,6 @@ func buildSchedulerConfig(
178170
profiles[cfgProfile.Name] = fwProfile
179171
}
180172

181-
// Find Profile Handler (singleton check).
182173
var profileHandler framework.ProfileHandler
183174
for name, plugin := range handle.GetAllPluginsWithNames() {
184175
if ph, ok := plugin.(framework.ProfileHandler); ok {
@@ -194,7 +185,6 @@ func buildSchedulerConfig(
194185
return nil, errors.New("no profile handler configured")
195186
}
196187

197-
// Validate SingleProfileHandler usage.
198188
if profileHandler.TypedName().Type == profile.SingleProfileHandlerType && len(profiles) > 1 {
199189
return nil, errors.New("SingleProfileHandler cannot support multiple scheduling profiles")
200190
}
@@ -203,27 +193,23 @@ func buildSchedulerConfig(
203193
}
204194

205195
func loadFeatureConfig(gates configapi.FeatureGates) map[string]bool {
206-
// Initialize with all false.
207196
config := make(map[string]bool, len(registeredFeatureGates))
208197
for gate := range registeredFeatureGates {
209198
config[gate] = false
210199
}
211-
// Apply overrides.
212200
for _, gate := range gates {
213201
config[gate] = true
214202
}
215203
return config
216204
}
217205

218206
func buildSaturationConfig(apiConfig *configapi.SaturationDetector) *saturationdetector.Config {
219-
// 1. Initialize with Defaults
220207
cfg := &saturationdetector.Config{
221208
QueueDepthThreshold: saturationdetector.DefaultQueueDepthThreshold,
222209
KVCacheUtilThreshold: saturationdetector.DefaultKVCacheUtilThreshold,
223210
MetricsStalenessThreshold: saturationdetector.DefaultMetricsStalenessThreshold,
224211
}
225212

226-
// 2. Apply Overrides (if valid)
227213
if apiConfig != nil {
228214
if apiConfig.QueueDepthThreshold > 0 {
229215
cfg.QueueDepthThreshold = apiConfig.QueueDepthThreshold

pkg/epp/config/loader/configloader_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestLoadRawConfiguration(t *testing.T) {
141141
}
142142
}
143143

144-
// --- Test: Phase 2 (Instantiation, Architecture Injection, Deep Validation) ---
144+
// --- Test: Phase 2 (Instantiation, System Defaulting, Deep Validation) ---
145145

146146
func TestInstantiateAndConfigure(t *testing.T) {
147147
// Not parallel because it modifies global plugin registry.
@@ -300,7 +300,7 @@ func TestInstantiateAndConfigure(t *testing.T) {
300300
}
301301
}
302302

303-
// Add this new test function to verify the builder logic specifically
303+
// Verify the SaturationConfig builder specifically.
304304
func TestBuildSaturationConfig(t *testing.T) {
305305
t.Parallel()
306306

pkg/epp/config/loader/defaults.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,40 @@ const DefaultScorerWeight = 1
3131

3232
var defaultScorerWeight = DefaultScorerWeight
3333

34-
// applyStaticDefaults standardizes the configuration object before plugin instantiation.
35-
// It handles simple structural defaults that do not require knowledge of the plugin registry.
34+
// applyStaticDefaults sanitizes the configuration object before plugin instantiation.
35+
// It handles "Static" defaults: simple structural changes to the API object that do not require access to the plugin
36+
// registry.
3637
func applyStaticDefaults(cfg *configapi.EndpointPickerConfig) {
37-
// Infer plugin names. If a plugin has a Type but no Name, the Type becomes the Name.
3838
for idx, pluginConfig := range cfg.Plugins {
3939
if pluginConfig.Name == "" {
4040
cfg.Plugins[idx].Name = pluginConfig.Type
4141
}
4242
}
4343

44-
// Initialize feature gates.
4544
if cfg.FeatureGates == nil {
4645
cfg.FeatureGates = configapi.FeatureGates{}
4746
}
4847
}
4948

50-
// applySystemDefaults injects required architectural components that were omitted from the config.
51-
// It inspects the instantiated plugins (via the handle) and ensures the system graph is complete.
49+
// applySystemDefaults injects required components that were omitted from the config.
50+
// It handles "System" defaults: logic that requires inspecting instantiated plugins (via the handle) to ensure the
51+
// system graph is complete.
5252
func applySystemDefaults(cfg *configapi.EndpointPickerConfig, handle plugins.Handle) error {
5353
allPlugins := handle.GetAllPluginsWithNames()
54-
55-
// Ensure the scheduling layer has profiles, pickers, and handlers.
56-
if err := ensureSchedulingArchitecture(cfg, handle, allPlugins); err != nil {
54+
if err := ensureSchedulingLayer(cfg, handle, allPlugins); err != nil {
5755
return fmt.Errorf("failed to apply scheduling system defaults: %w", err)
5856
}
59-
6057
return nil
6158
}
6259

63-
// ensureSchedulingArchitecture guarantees that a valid scheduling profile exists and that all profiles have valid
64-
// Pickers and Handlers.
65-
func ensureSchedulingArchitecture(
60+
// ensureSchedulingLayer guarantees that the scheduling subsystem is structurally complete.
61+
// It ensures a valid profile exists and injects missing architectural components (like Pickers and ProfileHandlers) if
62+
// they are not explicitly configured.
63+
func ensureSchedulingLayer(
6664
cfg *configapi.EndpointPickerConfig,
6765
handle plugins.Handle,
6866
allPlugins map[string]plugins.Plugin,
6967
) error {
70-
// Ensure at least one Scheduling Profile exists.
7168
if len(cfg.SchedulingProfiles) == 0 {
7269
defaultProfile := configapi.SchedulingProfile{Name: "default"}
7370
// Auto-populate the default profile with all Filter, Scorer, and Picker plugins found.
@@ -80,7 +77,6 @@ func ensureSchedulingArchitecture(
8077
cfg.SchedulingProfiles = []configapi.SchedulingProfile{defaultProfile}
8178
}
8279

83-
// Ensure profile handler.
8480
// If there is only 1 profile and no handler is explicitly configured, use the SingleProfileHandler.
8581
if len(cfg.SchedulingProfiles) == 1 {
8682
hasHandler := false
@@ -91,13 +87,12 @@ func ensureSchedulingArchitecture(
9187
}
9288
}
9389
if !hasHandler {
94-
if err := registerDefaultPlugin(cfg, handle, profile.SingleProfileHandlerType, profile.SingleProfileHandlerType); err != nil {
90+
if err := registerDefaultPlugin(cfg, handle, profile.SingleProfileHandlerType); err != nil {
9591
return err
9692
}
9793
}
9894
}
9995

100-
// Ensure Picker(s) and Scorer weights.
10196
// Find or Create a default MaxScorePicker to reuse across profiles.
10297
var maxScorePickerName string
10398
for name, p := range allPlugins {
@@ -106,32 +101,28 @@ func ensureSchedulingArchitecture(
106101
break
107102
}
108103
}
109-
// If no Picker exists anywhere, create one.
104+
110105
if maxScorePickerName == "" {
111-
if err := registerDefaultPlugin(cfg, handle, picker.MaxScorePickerType, picker.MaxScorePickerType); err != nil {
106+
if err := registerDefaultPlugin(cfg, handle, picker.MaxScorePickerType); err != nil {
112107
return err
113108
}
114109
maxScorePickerName = picker.MaxScorePickerType
115110
}
116111

117-
// Update profiles.
118112
for i, prof := range cfg.SchedulingProfiles {
119113
hasPicker := false
120114
for j, pluginRef := range prof.Plugins {
121115
p := handle.Plugin(pluginRef.PluginRef)
122116

123-
// Default Scorer weight.
124117
if _, ok := p.(framework.Scorer); ok && pluginRef.Weight == nil {
125118
cfg.SchedulingProfiles[i].Plugins[j].Weight = &defaultScorerWeight
126119
}
127120

128-
// Check for Picker.
129121
if _, ok := p.(framework.Picker); ok {
130122
hasPicker = true
131123
}
132124
}
133125

134-
// Inject default Picker if missing.
135126
if !hasPicker {
136127
cfg.SchedulingProfiles[i].Plugins = append(
137128
cfg.SchedulingProfiles[i].Plugins,
@@ -148,15 +139,14 @@ func ensureSchedulingArchitecture(
148139
func registerDefaultPlugin(
149140
cfg *configapi.EndpointPickerConfig,
150141
handle plugins.Handle,
151-
name string,
152142
pluginType string,
153143
) error {
144+
name := pluginType
154145
factory, ok := plugins.Registry[pluginType]
155146
if !ok {
156147
return fmt.Errorf("plugin type '%s' not found in registry", pluginType)
157148
}
158149

159-
// Instantiate with nil config (factory must handle defaults).
160150
plugin, err := factory(name, nil, handle)
161151
if err != nil {
162152
return fmt.Errorf("failed to instantiate default plugin '%s': %w", name, err)

0 commit comments

Comments
 (0)