Skip to content

Commit a0b72db

Browse files
Move predicates and priorities configuration creation to Policy mapping
For separation of concerns. Also make the producers receive the existing configuration so they can take decisions based on them. Change-Id: Id5db5588aabb90e7b8f087bd3f3c967e86dc4acc
1 parent 4373d9a commit a0b72db

File tree

4 files changed

+172
-251
lines changed

4 files changed

+172
-251
lines changed

pkg/scheduler/factory.go

Lines changed: 24 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"sort"
2423
"time"
2524

2625
"github.com/google/go-cmp/cmp"
@@ -268,138 +267,56 @@ func (c *Configurator) createFromConfig(policy schedulerapi.Policy) (*Scheduler,
268267

269268
klog.V(2).Infof("Creating scheduler with fit predicates '%v' and priority functions '%v'", predicateKeys, priorityKeys)
270269

271-
pluginsForPredicates, pluginConfigForPredicates, err := getPredicateConfigs(predicateKeys, lr, args)
272-
if err != nil {
273-
return nil, err
274-
}
275-
276-
pluginsForPriorities, pluginConfigForPriorities, err := getPriorityConfigs(priorityKeys, lr, args)
277-
if err != nil {
278-
return nil, err
279-
}
280270
// Combine all framework configurations. If this results in any duplication, framework
281271
// instantiation should fail.
282-
var defPlugins schedulerapi.Plugins
272+
283273
// "PrioritySort" and "DefaultBinder" were neither predicates nor priorities
284274
// before. We add them by default.
285-
defPlugins.Append(&schedulerapi.Plugins{
275+
plugins := schedulerapi.Plugins{
286276
QueueSort: &schedulerapi.PluginSet{
287277
Enabled: []schedulerapi.Plugin{{Name: queuesort.Name}},
288278
},
289279
Bind: &schedulerapi.PluginSet{
290280
Enabled: []schedulerapi.Plugin{{Name: defaultbinder.Name}},
291281
},
292-
})
293-
defPlugins.Append(pluginsForPredicates)
294-
defPlugins.Append(pluginsForPriorities)
295-
defPluginConfig, err := mergePluginConfigsFromPolicy(pluginConfigForPredicates, pluginConfigForPriorities)
296-
if err != nil {
282+
}
283+
var pluginConfig []schedulerapi.PluginConfig
284+
var err error
285+
if plugins, pluginConfig, err = lr.AppendPredicateConfigs(predicateKeys, args, plugins, pluginConfig); err != nil {
286+
return nil, err
287+
}
288+
if plugins, pluginConfig, err = lr.AppendPriorityConfigs(priorityKeys, args, plugins, pluginConfig); err != nil {
289+
return nil, err
290+
}
291+
if pluginConfig, err = dedupPluginConfigs(pluginConfig); err != nil {
297292
return nil, err
298293
}
299294
for i := range c.profiles {
300295
prof := &c.profiles[i]
301-
// Plugins are empty when using Policy.
296+
// Plugins and PluginConfig are empty when using Policy; overriding.
302297
prof.Plugins = &schedulerapi.Plugins{}
303-
prof.Plugins.Append(&defPlugins)
304-
305-
// PluginConfig is ignored when using Policy.
306-
prof.PluginConfig = defPluginConfig
298+
prof.Plugins.Append(&plugins)
299+
prof.PluginConfig = pluginConfig
307300
}
308301

309302
return c.create()
310303
}
311304

312-
// mergePluginConfigsFromPolicy merges the giving plugin configs ensuring that,
305+
// dedupPluginConfigs removes duplicates from pluginConfig, ensuring that,
313306
// if a plugin name is repeated, the arguments are the same.
314-
func mergePluginConfigsFromPolicy(pc1, pc2 []schedulerapi.PluginConfig) ([]schedulerapi.PluginConfig, error) {
307+
func dedupPluginConfigs(pc []schedulerapi.PluginConfig) ([]schedulerapi.PluginConfig, error) {
315308
args := make(map[string]runtime.Object)
316-
for _, c := range pc1 {
317-
args[c.Name] = c.Args
318-
}
319-
for _, c := range pc2 {
320-
if v, ok := args[c.Name]; ok && !cmp.Equal(v, c.Args) {
309+
result := make([]schedulerapi.PluginConfig, 0, len(pc))
310+
for _, c := range pc {
311+
if v, found := args[c.Name]; !found {
312+
result = append(result, c)
313+
args[c.Name] = c.Args
314+
} else if !cmp.Equal(v, c.Args) {
321315
// This should be unreachable.
322316
return nil, fmt.Errorf("inconsistent configuration produced for plugin %s", c.Name)
323317
}
324-
args[c.Name] = c.Args
325318
}
326-
pc := make([]schedulerapi.PluginConfig, 0, len(args))
327-
for k, v := range args {
328-
pc = append(pc, schedulerapi.PluginConfig{
329-
Name: k,
330-
Args: v,
331-
})
332-
}
333-
return pc, nil
334-
}
335-
336-
// getPriorityConfigs returns priorities configuration: ones that will run as priorities and ones that will run
337-
// as framework plugins. Specifically, a priority will run as a framework plugin if a plugin config producer was
338-
// registered for that priority.
339-
func getPriorityConfigs(keys map[string]int64, lr *frameworkplugins.LegacyRegistry, args *frameworkplugins.ConfigProducerArgs) (*schedulerapi.Plugins, []schedulerapi.PluginConfig, error) {
340-
var plugins schedulerapi.Plugins
341-
var pluginConfig []schedulerapi.PluginConfig
342-
343-
// Sort the keys so that it is easier for unit tests to do compare.
344-
var sortedKeys []string
345-
for k := range keys {
346-
sortedKeys = append(sortedKeys, k)
347-
}
348-
sort.Strings(sortedKeys)
349-
350-
for _, priority := range sortedKeys {
351-
weight := keys[priority]
352-
producer, exist := lr.PriorityToConfigProducer[priority]
353-
if !exist {
354-
return nil, nil, fmt.Errorf("no config producer registered for %q", priority)
355-
}
356-
a := *args
357-
a.Weight = int32(weight)
358-
pl, plc := producer(a)
359-
plugins.Append(&pl)
360-
pluginConfig = append(pluginConfig, plc...)
361-
}
362-
return &plugins, pluginConfig, nil
363-
}
364-
365-
// getPredicateConfigs returns predicates configuration: ones that will run as fitPredicates and ones that will run
366-
// as framework plugins. Specifically, a predicate will run as a framework plugin if a plugin config producer was
367-
// registered for that predicate.
368-
// Note that the framework executes plugins according to their order in the Plugins list, and so predicates run as plugins
369-
// are added to the Plugins list according to the order specified in predicates.Ordering().
370-
func getPredicateConfigs(keys sets.String, lr *frameworkplugins.LegacyRegistry, args *frameworkplugins.ConfigProducerArgs) (*schedulerapi.Plugins, []schedulerapi.PluginConfig, error) {
371-
allPredicates := keys.Union(lr.MandatoryPredicates)
372-
373-
// Create the framework plugin configurations, and place them in the order
374-
// that the corresponding predicates were supposed to run.
375-
var plugins schedulerapi.Plugins
376-
var pluginConfig []schedulerapi.PluginConfig
377-
378-
for _, predicateKey := range frameworkplugins.PredicateOrdering() {
379-
if allPredicates.Has(predicateKey) {
380-
producer, exist := lr.PredicateToConfigProducer[predicateKey]
381-
if !exist {
382-
return nil, nil, fmt.Errorf("no framework config producer registered for %q", predicateKey)
383-
}
384-
pl, plc := producer(*args)
385-
plugins.Append(&pl)
386-
pluginConfig = append(pluginConfig, plc...)
387-
allPredicates.Delete(predicateKey)
388-
}
389-
}
390-
391-
// Third, add the rest in no specific order.
392-
for predicateKey := range allPredicates {
393-
producer, exist := lr.PredicateToConfigProducer[predicateKey]
394-
if !exist {
395-
return nil, nil, fmt.Errorf("no framework config producer registered for %q", predicateKey)
396-
}
397-
pl, plc := producer(*args)
398-
plugins.Append(&pl)
399-
pluginConfig = append(pluginConfig, plc...)
400-
}
401-
402-
return &plugins, pluginConfig, nil
319+
return result, nil
403320
}
404321

405322
// MakeDefaultErrorFunc construct a function to handle pod scheduler error

pkg/scheduler/framework/plugins/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ go_test(
8080
embed = [":go_default_library"],
8181
deps = [
8282
"//pkg/scheduler/apis/config:go_default_library",
83+
"//pkg/scheduler/framework/plugins/nodeunschedulable:go_default_library",
84+
"//pkg/scheduler/framework/plugins/tainttoleration:go_default_library",
85+
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
8386
"//vendor/github.com/google/go-cmp/cmp:go_default_library",
8487
],
8588
)

0 commit comments

Comments
 (0)