mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Introduce scorePluginWeightMap to replace pluginNameToWeightMap
This commit is contained in:
parent
0e13f93c26
commit
47785131e2
@ -75,21 +75,21 @@ var configDecoder = scheme.Codecs.UniversalDecoder()
|
|||||||
// frameworkImpl is the component responsible for initializing and running scheduler
|
// frameworkImpl is the component responsible for initializing and running scheduler
|
||||||
// plugins.
|
// plugins.
|
||||||
type frameworkImpl struct {
|
type frameworkImpl struct {
|
||||||
registry Registry
|
registry Registry
|
||||||
snapshotSharedLister framework.SharedLister
|
snapshotSharedLister framework.SharedLister
|
||||||
waitingPods *waitingPodsMap
|
waitingPods *waitingPodsMap
|
||||||
pluginNameToWeightMap map[string]int
|
scorePluginWeight map[string]int
|
||||||
queueSortPlugins []framework.QueueSortPlugin
|
queueSortPlugins []framework.QueueSortPlugin
|
||||||
preFilterPlugins []framework.PreFilterPlugin
|
preFilterPlugins []framework.PreFilterPlugin
|
||||||
filterPlugins []framework.FilterPlugin
|
filterPlugins []framework.FilterPlugin
|
||||||
postFilterPlugins []framework.PostFilterPlugin
|
postFilterPlugins []framework.PostFilterPlugin
|
||||||
preScorePlugins []framework.PreScorePlugin
|
preScorePlugins []framework.PreScorePlugin
|
||||||
scorePlugins []framework.ScorePlugin
|
scorePlugins []framework.ScorePlugin
|
||||||
reservePlugins []framework.ReservePlugin
|
reservePlugins []framework.ReservePlugin
|
||||||
preBindPlugins []framework.PreBindPlugin
|
preBindPlugins []framework.PreBindPlugin
|
||||||
bindPlugins []framework.BindPlugin
|
bindPlugins []framework.BindPlugin
|
||||||
postBindPlugins []framework.PostBindPlugin
|
postBindPlugins []framework.PostBindPlugin
|
||||||
permitPlugins []framework.PermitPlugin
|
permitPlugins []framework.PermitPlugin
|
||||||
|
|
||||||
clientSet clientset.Interface
|
clientSet clientset.Interface
|
||||||
kubeConfig *restclient.Config
|
kubeConfig *restclient.Config
|
||||||
@ -258,19 +258,19 @@ func NewFramework(r Registry, profile *config.KubeSchedulerProfile, opts ...Opti
|
|||||||
}
|
}
|
||||||
|
|
||||||
f := &frameworkImpl{
|
f := &frameworkImpl{
|
||||||
registry: r,
|
registry: r,
|
||||||
snapshotSharedLister: options.snapshotSharedLister,
|
snapshotSharedLister: options.snapshotSharedLister,
|
||||||
pluginNameToWeightMap: make(map[string]int),
|
scorePluginWeight: make(map[string]int),
|
||||||
waitingPods: newWaitingPodsMap(),
|
waitingPods: newWaitingPodsMap(),
|
||||||
clientSet: options.clientSet,
|
clientSet: options.clientSet,
|
||||||
kubeConfig: options.kubeConfig,
|
kubeConfig: options.kubeConfig,
|
||||||
eventRecorder: options.eventRecorder,
|
eventRecorder: options.eventRecorder,
|
||||||
informerFactory: options.informerFactory,
|
informerFactory: options.informerFactory,
|
||||||
metricsRecorder: options.metricsRecorder,
|
metricsRecorder: options.metricsRecorder,
|
||||||
runAllFilters: options.runAllFilters,
|
runAllFilters: options.runAllFilters,
|
||||||
extenders: options.extenders,
|
extenders: options.extenders,
|
||||||
PodNominator: options.podNominator,
|
PodNominator: options.podNominator,
|
||||||
parallelizer: options.parallelizer,
|
parallelizer: options.parallelizer,
|
||||||
}
|
}
|
||||||
|
|
||||||
if profile == nil {
|
if profile == nil {
|
||||||
@ -282,6 +282,22 @@ func NewFramework(r Registry, profile *config.KubeSchedulerProfile, opts ...Opti
|
|||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var totalPriority int64
|
||||||
|
for _, e := range profile.Plugins.Score.Enabled {
|
||||||
|
// a weight of zero is not permitted, plugins can be disabled explicitly
|
||||||
|
// when configured.
|
||||||
|
f.scorePluginWeight[e.Name] = int(e.Weight)
|
||||||
|
if f.scorePluginWeight[e.Name] == 0 {
|
||||||
|
f.scorePluginWeight[e.Name] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks totalPriority against MaxTotalScore to avoid overflow
|
||||||
|
if int64(f.scorePluginWeight[e.Name])*framework.MaxNodeScore > framework.MaxTotalScore-totalPriority {
|
||||||
|
return nil, fmt.Errorf("total score of Score plugins could overflow")
|
||||||
|
}
|
||||||
|
totalPriority += int64(f.scorePluginWeight[e.Name]) * framework.MaxNodeScore
|
||||||
|
}
|
||||||
|
|
||||||
// get needed plugins from config
|
// get needed plugins from config
|
||||||
pg := f.pluginsNeeded(profile.Plugins)
|
pg := f.pluginsNeeded(profile.Plugins)
|
||||||
|
|
||||||
@ -300,7 +316,6 @@ func NewFramework(r Registry, profile *config.KubeSchedulerProfile, opts ...Opti
|
|||||||
}
|
}
|
||||||
|
|
||||||
pluginsMap := make(map[string]framework.Plugin)
|
pluginsMap := make(map[string]framework.Plugin)
|
||||||
var totalPriority int64
|
|
||||||
for name, factory := range r {
|
for name, factory := range r {
|
||||||
// initialize only needed plugins.
|
// initialize only needed plugins.
|
||||||
if _, ok := pg[name]; !ok {
|
if _, ok := pg[name]; !ok {
|
||||||
@ -325,18 +340,6 @@ func NewFramework(r Registry, profile *config.KubeSchedulerProfile, opts ...Opti
|
|||||||
|
|
||||||
// Update ClusterEventMap in place.
|
// Update ClusterEventMap in place.
|
||||||
fillEventToPluginMap(p, options.clusterEventMap)
|
fillEventToPluginMap(p, options.clusterEventMap)
|
||||||
|
|
||||||
// a weight of zero is not permitted, plugins can be disabled explicitly
|
|
||||||
// when configured.
|
|
||||||
f.pluginNameToWeightMap[name] = int(pg[name].Weight)
|
|
||||||
if f.pluginNameToWeightMap[name] == 0 {
|
|
||||||
f.pluginNameToWeightMap[name] = 1
|
|
||||||
}
|
|
||||||
// Checks totalPriority against MaxTotalScore to avoid overflow
|
|
||||||
if int64(f.pluginNameToWeightMap[name])*framework.MaxNodeScore > framework.MaxTotalScore-totalPriority {
|
|
||||||
return nil, fmt.Errorf("total score of Score plugins could overflow")
|
|
||||||
}
|
|
||||||
totalPriority += int64(f.pluginNameToWeightMap[name]) * framework.MaxNodeScore
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, e := range f.getExtensionPoints(profile.Plugins) {
|
for _, e := range f.getExtensionPoints(profile.Plugins) {
|
||||||
@ -348,7 +351,7 @@ func NewFramework(r Registry, profile *config.KubeSchedulerProfile, opts ...Opti
|
|||||||
// Verifying the score weights again since Plugin.Name() could return a different
|
// Verifying the score weights again since Plugin.Name() could return a different
|
||||||
// value from the one used in the configuration.
|
// value from the one used in the configuration.
|
||||||
for _, scorePlugin := range f.scorePlugins {
|
for _, scorePlugin := range f.scorePlugins {
|
||||||
if f.pluginNameToWeightMap[scorePlugin.Name()] == 0 {
|
if f.scorePluginWeight[scorePlugin.Name()] == 0 {
|
||||||
return nil, fmt.Errorf("score plugin %q is not configured with weight", scorePlugin.Name())
|
return nil, fmt.Errorf("score plugin %q is not configured with weight", scorePlugin.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -820,7 +823,7 @@ func (f *frameworkImpl) RunScorePlugins(ctx context.Context, state *framework.Cy
|
|||||||
f.Parallelizer().Until(ctx, len(f.scorePlugins), func(index int) {
|
f.Parallelizer().Until(ctx, len(f.scorePlugins), func(index int) {
|
||||||
pl := f.scorePlugins[index]
|
pl := f.scorePlugins[index]
|
||||||
// Score plugins' weight has been checked when they are initialized.
|
// Score plugins' weight has been checked when they are initialized.
|
||||||
weight := f.pluginNameToWeightMap[pl.Name()]
|
weight := f.scorePluginWeight[pl.Name()]
|
||||||
nodeScoreList := pluginToNodeScores[pl.Name()]
|
nodeScoreList := pluginToNodeScores[pl.Name()]
|
||||||
|
|
||||||
for i, nodeScore := range nodeScoreList {
|
for i, nodeScore := range nodeScoreList {
|
||||||
@ -1140,7 +1143,7 @@ func (f *frameworkImpl) ListPlugins() map[string][]config.Plugin {
|
|||||||
p := config.Plugin{Name: name}
|
p := config.Plugin{Name: name}
|
||||||
if extName == "ScorePlugin" {
|
if extName == "ScorePlugin" {
|
||||||
// Weights apply only to score plugins.
|
// Weights apply only to score plugins.
|
||||||
p.Weight = int32(f.pluginNameToWeightMap[name])
|
p.Weight = int32(f.scorePluginWeight[name])
|
||||||
}
|
}
|
||||||
cfgs = append(cfgs, p)
|
cfgs = append(cfgs, p)
|
||||||
}
|
}
|
||||||
|
@ -134,6 +134,10 @@ func (pl *PluginNotImplementingScore) Name() string {
|
|||||||
return pluginNotImplementingScore
|
return pluginNotImplementingScore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newTestPlugin(injArgs runtime.Object, f framework.Handle) (framework.Plugin, error) {
|
||||||
|
return &TestPlugin{name: testPlugin}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// TestPlugin implements all Plugin interfaces.
|
// TestPlugin implements all Plugin interfaces.
|
||||||
type TestPlugin struct {
|
type TestPlugin struct {
|
||||||
name string
|
name string
|
||||||
@ -329,6 +333,7 @@ var registry = func() Registry {
|
|||||||
r.Register(scorePlugin1, newScorePlugin1)
|
r.Register(scorePlugin1, newScorePlugin1)
|
||||||
r.Register(pluginNotImplementingScore, newPluginNotImplementingScore)
|
r.Register(pluginNotImplementingScore, newPluginNotImplementingScore)
|
||||||
r.Register(duplicatePluginName, newDuplicatePlugin)
|
r.Register(duplicatePluginName, newDuplicatePlugin)
|
||||||
|
r.Register(testPlugin, newTestPlugin)
|
||||||
return r
|
return r
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -2362,6 +2367,46 @@ func TestListPlugins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewFrameworkPluginWeights(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
plugins *config.Plugins
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Extend multiple extension points by same plugin",
|
||||||
|
plugins: &config.Plugins{
|
||||||
|
Score: config.PluginSet{Enabled: []config.Plugin{{Name: testPlugin, Weight: 3}}},
|
||||||
|
PostBind: config.PluginSet{Enabled: []config.Plugin{{Name: testPlugin, Weight: 6}}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Add multiple score plugins",
|
||||||
|
plugins: &config.Plugins{
|
||||||
|
Score: config.PluginSet{Enabled: []config.Plugin{{Name: testPlugin, Weight: 3}, {Name: scorePlugin1, Weight: 6}}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
profile := config.KubeSchedulerProfile{Plugins: tt.plugins}
|
||||||
|
f, err := newFrameworkWithQueueSortAndBind(registry, profile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create framework for testing: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins := f.ListPlugins()
|
||||||
|
if len(plugins["ScorePlugin"]) != len(tt.plugins.Score.Enabled) {
|
||||||
|
t.Fatalf("Expect %d ScorePlugin, got %d from: %v", len(tt.plugins.Score.Enabled), len(plugins["ScorePlugin"]), plugins["ScorePlugin"])
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff := cmp.Diff(tt.plugins.Score.Enabled, plugins["ScorePlugin"]); diff != "" {
|
||||||
|
t.Errorf("unexpected plugin weights (-want,+got):\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func buildScoreConfigDefaultWeights(ps ...string) *config.Plugins {
|
func buildScoreConfigDefaultWeights(ps ...string) *config.Plugins {
|
||||||
return buildScoreConfigWithWeights(defaultWeights, ps...)
|
return buildScoreConfigWithWeights(defaultWeights, ps...)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user