feat: use PreBind instead of Prebind in the scheduling framework

This commit is contained in:
draveness 2019-08-23 02:07:28 +08:00
parent 03f0934c80
commit af2e0428f6
8 changed files with 101 additions and 101 deletions

View File

@ -17,7 +17,7 @@ limitations under the License.
package multipoint
import (
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)
@ -27,7 +27,7 @@ import (
type CommunicatingPlugin struct{}
var _ = framework.ReservePlugin(CommunicatingPlugin{})
var _ = framework.PrebindPlugin(CommunicatingPlugin{})
var _ = framework.PreBindPlugin(CommunicatingPlugin{})
// Name is the name of the plug used in Registry and configurations.
const Name = "multipoint-communicating-plugin"
@ -50,8 +50,8 @@ func (mc CommunicatingPlugin) Reserve(pc *framework.PluginContext, pod *v1.Pod,
return nil
}
// Prebind is the functions invoked by the framework at "prebind" extension point.
func (mc CommunicatingPlugin) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
// PreBind is the functions invoked by the framework at "prebind" extension point.
func (mc CommunicatingPlugin) PreBind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
if pod == nil {
return framework.NewStatus(framework.Error, "pod cannot be nil")
}

View File

@ -19,27 +19,27 @@ package prebind
import (
"fmt"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)
// StatelessPrebindExample is an example of a simple plugin that has no state
// StatelessPreBindExample is an example of a simple plugin that has no state
// and implements only one hook for prebind.
type StatelessPrebindExample struct{}
type StatelessPreBindExample struct{}
var _ = framework.PrebindPlugin(StatelessPrebindExample{})
var _ = framework.PreBindPlugin(StatelessPreBindExample{})
// Name is the name of the plugin used in Registry and configurations.
const Name = "stateless-prebind-plugin-example"
// Name returns name of the plugin. It is used in logs, etc.
func (sr StatelessPrebindExample) Name() string {
func (sr StatelessPreBindExample) Name() string {
return Name
}
// Prebind is the functions invoked by the framework at "prebind" extension point.
func (sr StatelessPrebindExample) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
// PreBind is the functions invoked by the framework at "prebind" extension point.
func (sr StatelessPreBindExample) PreBind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
if pod == nil {
return framework.NewStatus(framework.Error, fmt.Sprintf("pod cannot be nil"))
}
@ -51,5 +51,5 @@ func (sr StatelessPrebindExample) Prebind(pc *framework.PluginContext, pod *v1.P
// New initializes a new plugin and returns it.
func New(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
return &StatelessPrebindExample{}, nil
return &StatelessPreBindExample{}, nil
}

View File

@ -20,7 +20,7 @@ import (
"fmt"
"sync"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
@ -36,7 +36,7 @@ type MultipointExample struct {
}
var _ = framework.ReservePlugin(&MultipointExample{})
var _ = framework.PrebindPlugin(&MultipointExample{})
var _ = framework.PreBindPlugin(&MultipointExample{})
// Name is the name of the plug used in Registry and configurations.
const Name = "multipoint-plugin-example"
@ -53,9 +53,9 @@ func (mp *MultipointExample) Reserve(pc *framework.PluginContext, pod *v1.Pod, n
return nil
}
// Prebind is the functions invoked by the framework at "prebind" extension point.
func (mp *MultipointExample) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
// Prebind could be called concurrently for different pods.
// PreBind is the functions invoked by the framework at "prebind" extension point.
func (mp *MultipointExample) PreBind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
// PreBind could be called concurrently for different pods.
mp.mu.Lock()
defer mp.mu.Unlock()
mp.numRuns++

View File

@ -45,7 +45,7 @@ type framework struct {
scorePlugins []ScorePlugin
scoreWithNormalizePlugins []ScoreWithNormalizePlugin
reservePlugins []ReservePlugin
prebindPlugins []PrebindPlugin
preBindPlugins []PreBindPlugin
bindPlugins []BindPlugin
postBindPlugins []PostBindPlugin
unreservePlugins []UnreservePlugin
@ -186,11 +186,11 @@ func NewFramework(r Registry, plugins *config.Plugins, args []config.PluginConfi
if plugins.PreBind != nil {
for _, pb := range plugins.PreBind.Enabled {
if pg, ok := pluginsMap[pb.Name]; ok {
p, ok := pg.(PrebindPlugin)
p, ok := pg.(PreBindPlugin)
if !ok {
return nil, fmt.Errorf("plugin %q does not extend prebind plugin", pb.Name)
}
f.prebindPlugins = append(f.prebindPlugins, p)
f.preBindPlugins = append(f.preBindPlugins, p)
} else {
return nil, fmt.Errorf("prebind plugin %q does not exist", pb.Name)
}
@ -425,13 +425,13 @@ func (f *framework) RunScorePlugins(pc *PluginContext, pod *v1.Pod, nodes []*v1.
return pluginToNodeScores, nil
}
// RunPrebindPlugins runs the set of configured prebind plugins. It returns a
// RunPreBindPlugins runs the set of configured prebind plugins. It returns a
// failure (bool) if any of the plugins returns an error. It also returns an
// error containing the rejection message or the error occurred in the plugin.
func (f *framework) RunPrebindPlugins(
func (f *framework) RunPreBindPlugins(
pc *PluginContext, pod *v1.Pod, nodeName string) *Status {
for _, pl := range f.prebindPlugins {
status := pl.Prebind(pc, pod, nodeName)
for _, pl := range f.preBindPlugins {
status := pl.PreBind(pc, pod, nodeName)
if !status.IsSuccess() {
if status.Code() == Unschedulable {
msg := fmt.Sprintf("rejected by %q at prebind: %v", pl.Name(), status.Message())

View File

@ -225,13 +225,13 @@ type ReservePlugin interface {
Reserve(pc *PluginContext, p *v1.Pod, nodeName string) *Status
}
// PrebindPlugin is an interface that must be implemented by "prebind" plugins.
// PreBindPlugin is an interface that must be implemented by "prebind" plugins.
// These plugins are called before a pod being scheduled.
type PrebindPlugin interface {
type PreBindPlugin interface {
Plugin
// Prebind is called before binding a pod. All prebind plugins must return
// PreBind is called before binding a pod. All prebind plugins must return
// success or the pod will be rejected and won't be sent for binding.
Prebind(pc *PluginContext, p *v1.Pod, nodeName string) *Status
PreBind(pc *PluginContext, p *v1.Pod, nodeName string) *Status
}
// PostBindPlugin is an interface that must be implemented by "postbind" plugins.
@ -311,12 +311,12 @@ type Framework interface {
// a non-success status.
RunScorePlugins(pc *PluginContext, pod *v1.Pod, nodes []*v1.Node) (PluginToNodeScores, *Status)
// RunPrebindPlugins runs the set of configured prebind plugins. It returns
// RunPreBindPlugins runs the set of configured prebind plugins. It returns
// *Status and its code is set to non-success if any of the plugins returns
// anything but Success. If the Status code is "Unschedulable", it is
// considered as a scheduling check failure, otherwise, it is considered as an
// internal error. In either case the pod is not going to be bound.
RunPrebindPlugins(pc *PluginContext, pod *v1.Pod, nodeName string) *Status
RunPreBindPlugins(pc *PluginContext, pod *v1.Pod, nodeName string) *Status
// RunPostBindPlugins runs the set of configured postbind plugins.
RunPostBindPlugins(pc *PluginContext, pod *v1.Pod, nodeName string)

View File

@ -179,7 +179,7 @@ func (*fakeFramework) RunScorePlugins(pc *framework.PluginContext, pod *v1.Pod,
return nil, nil
}
func (*fakeFramework) RunPrebindPlugins(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
func (*fakeFramework) RunPreBindPlugins(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
return nil
}

View File

@ -632,10 +632,10 @@ func (sched *Scheduler) scheduleOne() {
}
// Run "prebind" plugins.
prebindStatus := fwk.RunPrebindPlugins(pluginContext, assumedPod, scheduleResult.SuggestedHost)
if !prebindStatus.IsSuccess() {
preBindStatus := fwk.RunPreBindPlugins(pluginContext, assumedPod, scheduleResult.SuggestedHost)
if !preBindStatus.IsSuccess() {
var reason string
if prebindStatus.Code() == framework.Unschedulable {
if preBindStatus.Code() == framework.Unschedulable {
metrics.PodScheduleFailures.Inc()
reason = v1.PodReasonUnschedulable
} else {
@ -647,7 +647,7 @@ func (sched *Scheduler) scheduleOne() {
}
// trigger un-reserve plugins to clean up state associated with the reserved Pod
fwk.RunUnreservePlugins(pluginContext, assumedPod, scheduleResult.SuggestedHost)
sched.recordSchedulingFailure(assumedPod, prebindStatus.AsError(), reason, prebindStatus.Message())
sched.recordSchedulingFailure(assumedPod, preBindStatus.AsError(), reason, preBindStatus.Message())
return
}

View File

@ -63,10 +63,10 @@ type PostFilterPlugin struct {
failPostFilter bool
}
type PrebindPlugin struct {
numPrebindCalled int
failPrebind bool
rejectPrebind bool
type PreBindPlugin struct {
numPreBindCalled int
failPreBind bool
rejectPreBind bool
}
type BindPlugin struct {
@ -107,7 +107,7 @@ const (
filterPluginName = "filter-plugin"
postFilterPluginName = "postfilter-plugin"
reservePluginName = "reserve-plugin"
prebindPluginName = "prebind-plugin"
preBindPluginName = "prebind-plugin"
unreservePluginName = "unreserve-plugin"
postBindPluginName = "postbind-plugin"
permitPluginName = "permit-plugin"
@ -120,7 +120,7 @@ var _ = framework.ScorePlugin(&ScorePlugin{})
var _ = framework.ScoreWithNormalizePlugin(&ScoreWithNormalizePlugin{})
var _ = framework.ReservePlugin(&ReservePlugin{})
var _ = framework.PostFilterPlugin(&PostFilterPlugin{})
var _ = framework.PrebindPlugin(&PrebindPlugin{})
var _ = framework.PreBindPlugin(&PreBindPlugin{})
var _ = framework.BindPlugin(&BindPlugin{})
var _ = framework.PostBindPlugin(&PostBindPlugin{})
var _ = framework.UnreservePlugin(&UnreservePlugin{})
@ -249,27 +249,27 @@ func (pfp *PostFilterPlugin) reset() {
}
// Name returns name of the plugin.
func (pp *PrebindPlugin) Name() string {
return prebindPluginName
func (pp *PreBindPlugin) Name() string {
return preBindPluginName
}
// Prebind is a test function that returns (true, nil) or errors for testing.
func (pp *PrebindPlugin) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
pp.numPrebindCalled++
if pp.failPrebind {
// PreBind is a test function that returns (true, nil) or errors for testing.
func (pp *PreBindPlugin) PreBind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
pp.numPreBindCalled++
if pp.failPreBind {
return framework.NewStatus(framework.Error, fmt.Sprintf("injecting failure for pod %v", pod.Name))
}
if pp.rejectPrebind {
if pp.rejectPreBind {
return framework.NewStatus(framework.Unschedulable, fmt.Sprintf("reject pod %v", pod.Name))
}
return nil
}
// reset used to reset prebind plugin.
func (pp *PrebindPlugin) reset() {
pp.numPrebindCalled = 0
pp.failPrebind = false
pp.rejectPrebind = false
func (pp *PreBindPlugin) reset() {
pp.numPreBindCalled = 0
pp.failPreBind = false
pp.rejectPreBind = false
}
const bindPluginAnnotation = "bindPluginName"
@ -667,15 +667,15 @@ func TestReservePlugin(t *testing.T) {
// TestPrebindPlugin tests invocation of prebind plugins.
func TestPrebindPlugin(t *testing.T) {
// Create a plugin registry for testing. Register only a prebind plugin.
prebindPlugin := &PrebindPlugin{}
registry := framework.Registry{prebindPluginName: newPlugin(prebindPlugin)}
preBindPlugin := &PreBindPlugin{}
registry := framework.Registry{preBindPluginName: newPlugin(preBindPlugin)}
// Setup initial prebind plugin for testing.
plugins := &schedulerconfig.Plugins{
PreBind: &schedulerconfig.PluginSet{
Enabled: []schedulerconfig.Plugin{
{
Name: prebindPluginName,
Name: preBindPluginName,
},
},
},
@ -683,7 +683,7 @@ func TestPrebindPlugin(t *testing.T) {
// Set reserve prebind config for testing
preBindPluginConfig := []schedulerconfig.PluginConfig{
{
Name: prebindPluginName,
Name: preBindPluginName,
Args: runtime.Unknown{},
},
}
@ -723,8 +723,8 @@ func TestPrebindPlugin(t *testing.T) {
}
for i, test := range tests {
prebindPlugin.failPrebind = test.fail
prebindPlugin.rejectPrebind = test.reject
preBindPlugin.failPreBind = test.fail
preBindPlugin.rejectPreBind = test.reject
// Create a best effort pod.
pod, err := createPausePod(cs,
initPausePod(cs, &pausePodConfig{Name: "test-pod", Namespace: context.ns.Name}))
@ -748,11 +748,11 @@ func TestPrebindPlugin(t *testing.T) {
}
}
if prebindPlugin.numPrebindCalled == 0 {
if preBindPlugin.numPreBindCalled == 0 {
t.Errorf("Expected the prebind plugin to be called.")
}
prebindPlugin.reset()
preBindPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod})
}
}
@ -760,11 +760,11 @@ func TestPrebindPlugin(t *testing.T) {
// TestUnreservePlugin tests invocation of un-reserve plugin
func TestUnreservePlugin(t *testing.T) {
// TODO: register more plugin which would trigger un-reserve plugin
prebindPlugin := &PrebindPlugin{}
preBindPlugin := &PreBindPlugin{}
unreservePlugin := &UnreservePlugin{name: unreservePluginName}
registry := framework.Registry{
unreservePluginName: newPlugin(unreservePlugin),
prebindPluginName: newPlugin(prebindPlugin),
preBindPluginName: newPlugin(preBindPlugin),
}
// Setup initial unreserve and prebind plugin for testing.
@ -779,7 +779,7 @@ func TestUnreservePlugin(t *testing.T) {
PreBind: &schedulerconfig.PluginSet{
Enabled: []schedulerconfig.Plugin{
{
Name: prebindPluginName,
Name: preBindPluginName,
},
},
},
@ -791,7 +791,7 @@ func TestUnreservePlugin(t *testing.T) {
Args: runtime.Unknown{},
},
{
Name: prebindPluginName,
Name: preBindPluginName,
Args: runtime.Unknown{},
},
}
@ -810,30 +810,30 @@ func TestUnreservePlugin(t *testing.T) {
}
tests := []struct {
prebindFail bool
prebindReject bool
preBindFail bool
preBindReject bool
}{
{
prebindFail: false,
prebindReject: false,
preBindFail: false,
preBindReject: false,
},
{
prebindFail: true,
prebindReject: false,
preBindFail: true,
preBindReject: false,
},
{
prebindFail: false,
prebindReject: true,
preBindFail: false,
preBindReject: true,
},
{
prebindFail: true,
prebindReject: true,
preBindFail: true,
preBindReject: true,
},
}
for i, test := range tests {
prebindPlugin.failPrebind = test.prebindFail
prebindPlugin.rejectPrebind = test.prebindReject
preBindPlugin.failPreBind = test.preBindFail
preBindPlugin.rejectPreBind = test.preBindReject
// Create a best effort pod.
pod, err := createPausePod(cs,
@ -842,20 +842,20 @@ func TestUnreservePlugin(t *testing.T) {
t.Errorf("Error while creating a test pod: %v", err)
}
if test.prebindFail {
if test.preBindFail {
if err = wait.Poll(10*time.Millisecond, 30*time.Second, podSchedulingError(cs, pod.Namespace, pod.Name)); err != nil {
t.Errorf("test #%v: Expected a scheduling error, but didn't get it. error: %v", i, err)
}
if unreservePlugin.numUnreserveCalled == 0 || unreservePlugin.numUnreserveCalled != prebindPlugin.numPrebindCalled {
t.Errorf("test #%v: Expected the unreserve plugin to be called %d times, was called %d times.", i, prebindPlugin.numPrebindCalled, unreservePlugin.numUnreserveCalled)
if unreservePlugin.numUnreserveCalled == 0 || unreservePlugin.numUnreserveCalled != preBindPlugin.numPreBindCalled {
t.Errorf("test #%v: Expected the unreserve plugin to be called %d times, was called %d times.", i, preBindPlugin.numPreBindCalled, unreservePlugin.numUnreserveCalled)
}
} else {
if test.prebindReject {
if test.preBindReject {
if err = waitForPodUnschedulable(cs, pod); err != nil {
t.Errorf("test #%v: Didn't expected the pod to be scheduled. error: %v", i, err)
}
if unreservePlugin.numUnreserveCalled == 0 {
t.Errorf("test #%v: Expected the unreserve plugin to be called %d times, was called %d times.", i, prebindPlugin.numPrebindCalled, unreservePlugin.numUnreserveCalled)
t.Errorf("test #%v: Expected the unreserve plugin to be called %d times, was called %d times.", i, preBindPlugin.numPreBindCalled, unreservePlugin.numUnreserveCalled)
}
} else {
if err = waitForPodToSchedule(cs, pod); err != nil {
@ -868,7 +868,7 @@ func TestUnreservePlugin(t *testing.T) {
}
unreservePlugin.reset()
prebindPlugin.reset()
preBindPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod})
}
}
@ -1068,10 +1068,10 @@ func TestBindPlugin(t *testing.T) {
// TestPostBindPlugin tests invocation of postbind plugins.
func TestPostBindPlugin(t *testing.T) {
// Create a plugin registry for testing. Register a prebind and a postbind plugin.
prebindPlugin := &PrebindPlugin{}
preBindPlugin := &PreBindPlugin{}
postBindPlugin := &PostBindPlugin{name: postBindPluginName}
registry := framework.Registry{
prebindPluginName: newPlugin(prebindPlugin),
preBindPluginName: newPlugin(preBindPlugin),
postBindPluginName: newPlugin(postBindPlugin),
}
@ -1080,7 +1080,7 @@ func TestPostBindPlugin(t *testing.T) {
PreBind: &schedulerconfig.PluginSet{
Enabled: []schedulerconfig.Plugin{
{
Name: prebindPluginName,
Name: preBindPluginName,
},
},
},
@ -1095,7 +1095,7 @@ func TestPostBindPlugin(t *testing.T) {
// Set reserve prebind and postbind config for testing
pluginConfig := []schedulerconfig.PluginConfig{
{
Name: prebindPluginName,
Name: preBindPluginName,
Args: runtime.Unknown{},
},
{
@ -1118,30 +1118,30 @@ func TestPostBindPlugin(t *testing.T) {
}
tests := []struct {
prebindFail bool
prebindReject bool
preBindFail bool
preBindReject bool
}{
{
prebindFail: false,
prebindReject: false,
preBindFail: false,
preBindReject: false,
},
{
prebindFail: true,
prebindReject: false,
preBindFail: true,
preBindReject: false,
},
{
prebindFail: false,
prebindReject: true,
preBindFail: false,
preBindReject: true,
},
{
prebindFail: true,
prebindReject: true,
preBindFail: true,
preBindReject: true,
},
}
for i, test := range tests {
prebindPlugin.failPrebind = test.prebindFail
prebindPlugin.rejectPrebind = test.prebindReject
preBindPlugin.failPreBind = test.preBindFail
preBindPlugin.rejectPreBind = test.preBindReject
// Create a best effort pod.
pod, err := createPausePod(cs,
@ -1150,7 +1150,7 @@ func TestPostBindPlugin(t *testing.T) {
t.Errorf("Error while creating a test pod: %v", err)
}
if test.prebindFail {
if test.preBindFail {
if err = wait.Poll(10*time.Millisecond, 30*time.Second, podSchedulingError(cs, pod.Namespace, pod.Name)); err != nil {
t.Errorf("test #%v: Expected a scheduling error, but didn't get it. error: %v", i, err)
}
@ -1158,7 +1158,7 @@ func TestPostBindPlugin(t *testing.T) {
t.Errorf("test #%v: Didn't expected the postbind plugin to be called %d times.", i, postBindPlugin.numPostBindCalled)
}
} else {
if test.prebindReject {
if test.preBindReject {
if err = waitForPodUnschedulable(cs, pod); err != nil {
t.Errorf("test #%v: Didn't expected the pod to be scheduled. error: %v", i, err)
}
@ -1176,7 +1176,7 @@ func TestPostBindPlugin(t *testing.T) {
}
postBindPlugin.reset()
prebindPlugin.reset()
preBindPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod})
}
}