feat: use PreFilter instead of Prefilter in the scheduling framework

This commit is contained in:
draveness 2019-08-23 01:56:28 +08:00
parent 2af52db689
commit f3816fb757
5 changed files with 40 additions and 40 deletions

View File

@ -195,9 +195,9 @@ func (g *genericScheduler) Schedule(pod *v1.Pod, pluginContext *framework.Plugin
} }
// Run "prefilter" plugins. // Run "prefilter" plugins.
prefilterStatus := g.framework.RunPrefilterPlugins(pluginContext, pod) preFilterStatus := g.framework.RunPreFilterPlugins(pluginContext, pod)
if !prefilterStatus.IsSuccess() { if !preFilterStatus.IsSuccess() {
return result, prefilterStatus.AsError() return result, preFilterStatus.AsError()
} }
numNodes := g.cache.NodeTree().NumNodes() numNodes := g.cache.NodeTree().NumNodes()

View File

@ -39,7 +39,7 @@ type framework struct {
waitingPods *waitingPodsMap waitingPods *waitingPodsMap
pluginNameToWeightMap map[string]int pluginNameToWeightMap map[string]int
queueSortPlugins []QueueSortPlugin queueSortPlugins []QueueSortPlugin
prefilterPlugins []PrefilterPlugin preFilterPlugins []PreFilterPlugin
filterPlugins []FilterPlugin filterPlugins []FilterPlugin
postFilterPlugins []PostFilterPlugin postFilterPlugins []PostFilterPlugin
scorePlugins []ScorePlugin scorePlugins []ScorePlugin
@ -105,11 +105,11 @@ func NewFramework(r Registry, plugins *config.Plugins, args []config.PluginConfi
if plugins.PreFilter != nil { if plugins.PreFilter != nil {
for _, pf := range plugins.PreFilter.Enabled { for _, pf := range plugins.PreFilter.Enabled {
if pg, ok := pluginsMap[pf.Name]; ok { if pg, ok := pluginsMap[pf.Name]; ok {
p, ok := pg.(PrefilterPlugin) p, ok := pg.(PreFilterPlugin)
if !ok { if !ok {
return nil, fmt.Errorf("plugin %q does not extend prefilter plugin", pf.Name) return nil, fmt.Errorf("plugin %q does not extend prefilter plugin", pf.Name)
} }
f.prefilterPlugins = append(f.prefilterPlugins, p) f.preFilterPlugins = append(f.preFilterPlugins, p)
} else { } else {
return nil, fmt.Errorf("prefilter plugin %q does not exist", pf.Name) return nil, fmt.Errorf("prefilter plugin %q does not exist", pf.Name)
} }
@ -283,14 +283,14 @@ func (f *framework) QueueSortFunc() LessFunc {
return f.queueSortPlugins[0].Less return f.queueSortPlugins[0].Less
} }
// RunPrefilterPlugins runs the set of configured prefilter plugins. It returns // RunPreFilterPlugins runs the set of configured PreFilter plugins. It returns
// *Status and its code is set to non-success if any of the plugins returns // *Status and its code is set to non-success if any of the plugins returns
// anything but Success. If a non-success status is returned, then the scheduling // anything but Success. If a non-success status is returned, then the scheduling
// cycle is aborted. // cycle is aborted.
func (f *framework) RunPrefilterPlugins( func (f *framework) RunPreFilterPlugins(
pc *PluginContext, pod *v1.Pod) *Status { pc *PluginContext, pod *v1.Pod) *Status {
for _, pl := range f.prefilterPlugins { for _, pl := range f.preFilterPlugins {
status := pl.Prefilter(pc, pod) status := pl.PreFilter(pc, pod)
if !status.IsSuccess() { if !status.IsSuccess() {
if status.Code() == Unschedulable { if status.Code() == Unschedulable {
msg := fmt.Sprintf("rejected by %q at prefilter: %v", pl.Name(), status.Message()) msg := fmt.Sprintf("rejected by %q at prefilter: %v", pl.Name(), status.Message())

View File

@ -152,13 +152,13 @@ type QueueSortPlugin interface {
Less(*PodInfo, *PodInfo) bool Less(*PodInfo, *PodInfo) bool
} }
// PrefilterPlugin is an interface that must be implemented by "prefilter" plugins. // PreFilterPlugin is an interface that must be implemented by "prefilter" plugins.
// These plugins are called at the beginning of the scheduling cycle. // These plugins are called at the beginning of the scheduling cycle.
type PrefilterPlugin interface { type PreFilterPlugin interface {
Plugin Plugin
// Prefilter is called at the beginning of the scheduling cycle. All prefilter // PreFilter is called at the beginning of the scheduling cycle. All PreFilter
// plugins must return success or the pod will be rejected. // plugins must return success or the pod will be rejected.
Prefilter(pc *PluginContext, p *v1.Pod) *Status PreFilter(pc *PluginContext, p *v1.Pod) *Status
} }
// FilterPlugin is an interface for Filter plugins. These plugins are called at the // FilterPlugin is an interface for Filter plugins. These plugins are called at the
@ -289,11 +289,11 @@ type Framework interface {
// QueueSortFunc returns the function to sort pods in scheduling queue // QueueSortFunc returns the function to sort pods in scheduling queue
QueueSortFunc() LessFunc QueueSortFunc() LessFunc
// RunPrefilterPlugins runs the set of configured prefilter plugins. It returns // RunPreFilterPlugins runs the set of configured prefilter plugins. It returns
// *Status and its code is set to non-success if any of the plugins returns // *Status and its code is set to non-success if any of the plugins returns
// anything but Success. If a non-success status is returned, then the scheduling // anything but Success. If a non-success status is returned, then the scheduling
// cycle is aborted. // cycle is aborted.
RunPrefilterPlugins(pc *PluginContext, pod *v1.Pod) *Status RunPreFilterPlugins(pc *PluginContext, pod *v1.Pod) *Status
// RunFilterPlugins runs the set of configured filter plugins for pod on the // RunFilterPlugins runs the set of configured filter plugins for pod on the
// given host. If any of these plugins returns any status other than "Success", // given host. If any of these plugins returns any status other than "Success",

View File

@ -167,7 +167,7 @@ func (*fakeFramework) NodeInfoSnapshot() *internalcache.NodeInfoSnapshot {
return nil return nil
} }
func (*fakeFramework) RunPrefilterPlugins(pc *framework.PluginContext, pod *v1.Pod) *framework.Status { func (*fakeFramework) RunPreFilterPlugins(pc *framework.PluginContext, pod *v1.Pod) *framework.Status {
return nil return nil
} }

View File

@ -31,10 +31,10 @@ import (
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1" framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
) )
type PrefilterPlugin struct { type PreFilterPlugin struct {
numPrefilterCalled int numPreFilterCalled int
failPrefilter bool failPreFilter bool
rejectPrefilter bool rejectPreFilter bool
} }
type ScorePlugin struct { type ScorePlugin struct {
@ -113,7 +113,7 @@ const (
permitPluginName = "permit-plugin" permitPluginName = "permit-plugin"
) )
var _ = framework.PrefilterPlugin(&PrefilterPlugin{}) var _ = framework.PreFilterPlugin(&PreFilterPlugin{})
var _ = framework.ScorePlugin(&ScorePlugin{}) var _ = framework.ScorePlugin(&ScorePlugin{})
var _ = framework.FilterPlugin(&FilterPlugin{}) var _ = framework.FilterPlugin(&FilterPlugin{})
var _ = framework.ScorePlugin(&ScorePlugin{}) var _ = framework.ScorePlugin(&ScorePlugin{})
@ -321,27 +321,27 @@ func (pp *PostbindPlugin) reset() {
} }
// Name returns name of the plugin. // Name returns name of the plugin.
func (pp *PrefilterPlugin) Name() string { func (pp *PreFilterPlugin) Name() string {
return prefilterPluginName return prefilterPluginName
} }
// Prefilter is a test function that returns (true, nil) or errors for testing. // PreFilter is a test function that returns (true, nil) or errors for testing.
func (pp *PrefilterPlugin) Prefilter(pc *framework.PluginContext, pod *v1.Pod) *framework.Status { func (pp *PreFilterPlugin) PreFilter(pc *framework.PluginContext, pod *v1.Pod) *framework.Status {
pp.numPrefilterCalled++ pp.numPreFilterCalled++
if pp.failPrefilter { if pp.failPreFilter {
return framework.NewStatus(framework.Error, fmt.Sprintf("injecting failure for pod %v", pod.Name)) return framework.NewStatus(framework.Error, fmt.Sprintf("injecting failure for pod %v", pod.Name))
} }
if pp.rejectPrefilter { if pp.rejectPreFilter {
return framework.NewStatus(framework.Unschedulable, fmt.Sprintf("reject pod %v", pod.Name)) return framework.NewStatus(framework.Unschedulable, fmt.Sprintf("reject pod %v", pod.Name))
} }
return nil return nil
} }
// reset used to reset prefilter plugin. // reset used to reset prefilter plugin.
func (pp *PrefilterPlugin) reset() { func (pp *PreFilterPlugin) reset() {
pp.numPrefilterCalled = 0 pp.numPreFilterCalled = 0
pp.failPrefilter = false pp.failPreFilter = false
pp.rejectPrefilter = false pp.rejectPreFilter = false
} }
// Name returns name of the plugin. // Name returns name of the plugin.
@ -426,11 +426,11 @@ func newPermitPlugin(permitPlugin *PermitPlugin) framework.PluginFactory {
} }
} }
// TestPrefilterPlugin tests invocation of prefilter plugins. // TestPreFilterPlugin tests invocation of prefilter plugins.
func TestPrefilterPlugin(t *testing.T) { func TestPreFilterPlugin(t *testing.T) {
// Create a plugin registry for testing. Register only a pre-filter plugin. // Create a plugin registry for testing. Register only a pre-filter plugin.
prefilterPlugin := &PrefilterPlugin{} preFilterPlugin := &PreFilterPlugin{}
registry := framework.Registry{prefilterPluginName: newPlugin(prefilterPlugin)} registry := framework.Registry{prefilterPluginName: newPlugin(preFilterPlugin)}
// Setup initial prefilter plugin for testing. // Setup initial prefilter plugin for testing.
plugins := &schedulerconfig.Plugins{ plugins := &schedulerconfig.Plugins{
@ -478,8 +478,8 @@ func TestPrefilterPlugin(t *testing.T) {
} }
for i, test := range tests { for i, test := range tests {
prefilterPlugin.failPrefilter = test.fail preFilterPlugin.failPreFilter = test.fail
prefilterPlugin.rejectPrefilter = test.reject preFilterPlugin.rejectPreFilter = test.reject
// Create a best effort pod. // Create a best effort pod.
pod, err := createPausePod(cs, pod, err := createPausePod(cs,
initPausePod(cs, &pausePodConfig{Name: "test-pod", Namespace: context.ns.Name})) initPausePod(cs, &pausePodConfig{Name: "test-pod", Namespace: context.ns.Name}))
@ -497,11 +497,11 @@ func TestPrefilterPlugin(t *testing.T) {
} }
} }
if prefilterPlugin.numPrefilterCalled == 0 { if preFilterPlugin.numPreFilterCalled == 0 {
t.Errorf("Expected the prefilter plugin to be called.") t.Errorf("Expected the prefilter plugin to be called.")
} }
prefilterPlugin.reset() preFilterPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod}) cleanupPods(cs, t, []*v1.Pod{pod})
} }
} }