Skip to content

Commit

Permalink
koord-scheduler: implemented PreBindExtensions to support one-time co…
Browse files Browse the repository at this point in the history
…mmit patches

Signed-off-by: Joseph <joseph.t.lee@outlook.com>
  • Loading branch information
eahydra committed May 23, 2023
1 parent 9449d56 commit d817eeb
Show file tree
Hide file tree
Showing 18 changed files with 689 additions and 347 deletions.
3 changes: 3 additions & 0 deletions cmd/koord-scheduler/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
schedulerserverconfig "github.com/koordinator-sh/koordinator/cmd/koord-scheduler/app/config"
"github.com/koordinator-sh/koordinator/cmd/koord-scheduler/app/options"
"github.com/koordinator-sh/koordinator/pkg/scheduler/frameworkext"
"github.com/koordinator-sh/koordinator/pkg/scheduler/frameworkext/defaultprofile"
"github.com/koordinator-sh/koordinator/pkg/scheduler/frameworkext/eventhandlers"
"github.com/koordinator-sh/koordinator/pkg/scheduler/frameworkext/services"
utilroutes "github.com/koordinator-sh/koordinator/pkg/util/routes"
Expand Down Expand Up @@ -336,6 +337,8 @@ func Setup(ctx context.Context, opts *options.Options, outOfTreeRegistryOptions
// Get the completed config
cc := c.Complete()

defaultprofile.AppendDefaultPlugins(cc.ComponentConfig.Profiles)

// NOTE(joseph): K8s scheduling framework does not provide extension point for initialization.
// Currently, only by copying the initialization code and implementing custom initialization.
frameworkExtenderFactory, err := frameworkext.NewFrameworkExtenderFactory(
Expand Down
2 changes: 2 additions & 0 deletions cmd/koord-scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/batchresource"
"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/compatibledefaultpreemption"
"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/coscheduling"
"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/defaultprebind"
"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/deviceshare"
"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/elasticquota"
"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/loadaware"
Expand All @@ -49,6 +50,7 @@ var koordinatorPlugins = map[string]frameworkruntime.PluginFactory{
deviceshare.Name: deviceshare.New,
elasticquota.Name: elasticquota.New,
compatibledefaultpreemption.Name: compatibledefaultpreemption.New,
defaultprebind.Name: defaultprebind.New,
}

func flatten(plugins map[string]frameworkruntime.PluginFactory) []app.Option {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (e *SoftEvictor) Evict(ctx context.Context, job *sev1alpha1.PodMigrationJob
newPod.Annotations[extension.AnnotationSoftEviction] = string(evictionSpecData)

return util.RetryOnConflictOrTooManyRequests(func() error {
_, err1 := util.NewPatch().WithClientset(e.client).PatchPod(ctx, pod, newPod)
_, err1 := util.PatchPod(ctx, e.client, pod, newPod)
return err1
})
}
55 changes: 55 additions & 0 deletions pkg/scheduler/frameworkext/defaultprofile/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2022 The Koordinator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package defaultprofile

import (
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"

"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/defaultprebind"
)

func AppendDefaultPlugins(profiles []kubeschedulerconfig.KubeSchedulerProfile) {
for i := range profiles {
p := &profiles[i]

if p.Plugins == nil {
continue
}

hasDisabled := false
for _, disabled := range p.Plugins.PreBind.Disabled {
if disabled.Name == "*" || disabled.Name == defaultprebind.Name {
hasDisabled = true
break
}
}

found := false
for _, enabled := range p.Plugins.PreBind.Enabled {
if enabled.Name == defaultprebind.Name {
found = true
break
}
}

if !found && !hasDisabled {
p.Plugins.PreBind.Enabled = append(p.Plugins.PreBind.Enabled, kubeschedulerconfig.Plugin{
Name: defaultprebind.Name,
})
}
}
}
129 changes: 129 additions & 0 deletions pkg/scheduler/frameworkext/defaultprofile/profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright 2022 The Koordinator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package defaultprofile

import (
"testing"

"github.com/stretchr/testify/assert"
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"

"github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/defaultprebind"
)

func TestAppendDefaultPlugins(t *testing.T) {
tests := []struct {
name string
profiles []kubeschedulerconfig.KubeSchedulerProfile
wantProfiles []kubeschedulerconfig.KubeSchedulerProfile
}{
{
name: "empty profile",
profiles: []kubeschedulerconfig.KubeSchedulerProfile{
{},
},
wantProfiles: []kubeschedulerconfig.KubeSchedulerProfile{
{},
},
},
{
name: "append defaultPreBindExtension plugin",
profiles: []kubeschedulerconfig.KubeSchedulerProfile{
{
Plugins: &kubeschedulerconfig.Plugins{},
},
},
wantProfiles: []kubeschedulerconfig.KubeSchedulerProfile{
{
Plugins: &kubeschedulerconfig.Plugins{
PreBind: kubeschedulerconfig.PluginSet{
Enabled: []kubeschedulerconfig.Plugin{
{
Name: defaultprebind.Name,
},
},
},
},
},
},
},
{
name: "disable defaultPreBindExtension plugin",
profiles: []kubeschedulerconfig.KubeSchedulerProfile{
{
Plugins: &kubeschedulerconfig.Plugins{
PreBind: kubeschedulerconfig.PluginSet{
Disabled: []kubeschedulerconfig.Plugin{
{
Name: defaultprebind.Name,
},
},
},
},
},
},
wantProfiles: []kubeschedulerconfig.KubeSchedulerProfile{
{
Plugins: &kubeschedulerconfig.Plugins{
PreBind: kubeschedulerconfig.PluginSet{
Disabled: []kubeschedulerconfig.Plugin{
{
Name: defaultprebind.Name,
},
},
},
},
},
},
},
{
name: "disable all prebind plugins",
profiles: []kubeschedulerconfig.KubeSchedulerProfile{
{
Plugins: &kubeschedulerconfig.Plugins{
PreBind: kubeschedulerconfig.PluginSet{
Disabled: []kubeschedulerconfig.Plugin{
{
Name: "*",
},
},
},
},
},
},
wantProfiles: []kubeschedulerconfig.KubeSchedulerProfile{
{
Plugins: &kubeschedulerconfig.Plugins{
PreBind: kubeschedulerconfig.PluginSet{
Disabled: []kubeschedulerconfig.Plugin{
{
Name: "*",
},
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
AppendDefaultPlugins(tt.profiles)
assert.Equal(t, tt.wantProfiles, tt.profiles)
})
}
}
41 changes: 39 additions & 2 deletions pkg/scheduler/frameworkext/framework_extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"unsafe"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/scheduler/framework"

Expand Down Expand Up @@ -53,6 +54,8 @@ type frameworkExtenderImpl struct {
reservationNominatorPlugins []ReservationNominator
reservationPreBindPlugins []ReservationPreBindPlugin
reservationRestorePlugins []ReservationRestorePlugin

preBindExtensionsPlugins map[string]PreBindExtensions
}

func NewFrameworkExtender(f *FrameworkExtenderFactory, fw framework.Framework) FrameworkExtender {
Expand All @@ -68,6 +71,7 @@ func NewFrameworkExtender(f *FrameworkExtenderFactory, fw framework.Framework) F
snapshotGeneration: snapshotGeneration,
koordinatorClientSet: f.KoordinatorClientSet(),
koordinatorSharedInformerFactory: f.koordinatorSharedInformerFactory,
preBindExtensionsPlugins: map[string]PreBindExtensions{},
}
frameworkExtender.updateTransformer(f.defaultTransformers...)
return frameworkExtender
Expand Down Expand Up @@ -127,6 +131,9 @@ func (ext *frameworkExtenderImpl) updatePlugins(pl framework.Plugin) {
if r, ok := pl.(ReservationRestorePlugin); ok {
ext.reservationRestorePlugins = append(ext.reservationRestorePlugins, r)
}
if p, ok := pl.(PreBindExtensions); ok {
ext.preBindExtensionsPlugins[p.Name()] = p
}
}

func (ext *frameworkExtenderImpl) KoordinatorClientSet() koordinatorclientset.Interface {
Expand Down Expand Up @@ -257,7 +264,13 @@ func (ext *frameworkExtenderImpl) RunReservePluginsReserve(ctx context.Context,
// RunPreBindPlugins supports PreBindReservation for Reservation
func (ext *frameworkExtenderImpl) RunPreBindPlugins(ctx context.Context, state *framework.CycleState, pod *corev1.Pod, nodeName string) *framework.Status {
if !reservationutil.IsReservePod(pod) {
return ext.Framework.RunPreBindPlugins(ctx, state, pod, nodeName)
original := pod
pod = pod.DeepCopy()
status := ext.Framework.RunPreBindPlugins(ctx, state, pod, nodeName)
if !status.IsSuccess() {
return status
}
return ext.runPreBindExtensionPlugins(ctx, state, original, pod)
}

reservationLister := ext.koordinatorSharedInformerFactory.Scheduling().V1alpha1().Reservations().Lister()
Expand All @@ -267,9 +280,9 @@ func (ext *frameworkExtenderImpl) RunPreBindPlugins(ctx context.Context, state *
return framework.AsStatus(err)
}

original := reservation
reservation = reservation.DeepCopy()
reservation.Status.NodeName = nodeName

for _, pl := range ext.reservationPreBindPlugins {
status := pl.PreBindReservation(ctx, state, reservation, nodeName)
if !status.IsSuccess() {
Expand All @@ -278,6 +291,30 @@ func (ext *frameworkExtenderImpl) RunPreBindPlugins(ctx context.Context, state *
return framework.AsStatus(fmt.Errorf("running ReservationPreBindPlugin plugin %q: %w", pl.Name(), err))
}
}
return ext.runPreBindExtensionPlugins(ctx, state, original, reservation)
}

func (ext *frameworkExtenderImpl) runPreBindExtensionPlugins(ctx context.Context, cycleState *framework.CycleState, originalObj, modifiedObj metav1.Object) *framework.Status {
plugins := ext.Framework.ListPlugins()
if plugins == nil {
return nil
}
for _, plugin := range plugins.PreBind.Enabled {
pl := ext.preBindExtensionsPlugins[plugin.Name]
if pl == nil {
continue
}
status := pl.ApplyPatch(ctx, cycleState, originalObj, modifiedObj)
if status != nil && status.Code() == framework.Skip {
continue
}
if !status.IsSuccess() {
err := status.AsError()
klog.ErrorS(err, "Failed running PreBindExtension plugin", "plugin", pl.Name(), "pod", klog.KObj(originalObj))
return framework.AsStatus(fmt.Errorf("running PreBindExtension plugin %q: %w", pl.Name(), err))
}
return status
}
return nil
}

Expand Down

0 comments on commit d817eeb

Please sign in to comment.