Skip to content

Commit

Permalink
Merge pull request kubernetes-retired#698 from hex108/plugin
Browse files Browse the repository at this point in the history
Add support for setting default value to the configuration
  • Loading branch information
k8s-ci-robot authored Apr 2, 2019
2 parents 6399ae9 + 690e569 commit 3644240
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 26 deletions.
9 changes: 7 additions & 2 deletions pkg/scheduler/actions/allocate/allocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,19 @@ func TestAllocate(t *testing.T) {
schedulerCache.AddQueue(q)
}

trueValue := true
ssn := framework.OpenSession(schedulerCache, []conf.Tier{
{
Plugins: []conf.PluginOption{
{
Name: "drf",
Name: "drf",
EnabledPreemptable: &trueValue,
EnabledJobOrder: &trueValue,
},
{
Name: "proportion",
Name: "proportion",
EnabledQueueOrder: &trueValue,
EnabledReclaimable: &trueValue,
},
},
},
Expand Down
32 changes: 16 additions & 16 deletions pkg/scheduler/conf/scheduler_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ type Tier struct {
type PluginOption struct {
// The name of Plugin
Name string `yaml:"name"`
// JobOrderDisabled defines whether jobOrderFn is disabled
JobOrderDisabled bool `yaml:"disableJobOrder"`
// JobReadyDisabled defines whether jobReadyFn is disabled
JobReadyDisabled bool `yaml:"disableJobReady"`
// TaskOrderDisabled defines whether taskOrderFn is disabled
TaskOrderDisabled bool `yaml:"disableTaskOrder"`
// PreemptableDisabled defines whether preemptableFn is disabled
PreemptableDisabled bool `yaml:"disablePreemptable"`
// ReclaimableDisabled defines whether reclaimableFn is disabled
ReclaimableDisabled bool `yaml:"disableReclaimable"`
// QueueOrderDisabled defines whether queueOrderFn is disabled
QueueOrderDisabled bool `yaml:"disableQueueOrder"`
// PredicateDisabled defines whether predicateFn is disabled
PredicateDisabled bool `yaml:"disablePredicate"`
// NodeOrderDisabled defines whether NodeOrderFn is disabled
NodeOrderDisabled bool `yaml:"disableNodeOrder"`
// EnabledJobOrder defines whether jobOrderFn is enabled
EnabledJobOrder *bool `yaml:"enableJobOrder"`
// EnabledJobReady defines whether jobReadyFn is enabled
EnabledJobReady *bool `yaml:"enableJobReady"`
// EnabledTaskOrder defines whether taskOrderFn is enabled
EnabledTaskOrder *bool `yaml:"enableTaskOrder"`
// EnabledPreemptable defines whether preemptableFn is enabled
EnabledPreemptable *bool `yaml:"enablePreemptable"`
// EnabledReclaimable defines whether reclaimableFn is enabled
EnabledReclaimable *bool `yaml:"enableReclaimable"`
// EnabledQueueOrder defines whether queueOrderFn is enabled
EnabledQueueOrder *bool `yaml:"enableQueueOrder"`
// EnabledPredicate defines whether predicateFn is enabled
EnabledPredicate *bool `yaml:"enablePredicate"`
// EnabledNodeOrder defines whether NodeOrderFn is enabled
EnabledNodeOrder *bool `yaml:"enableNodeOrder"`
// Arguments defines the different arguments that can be given to different plugins
Arguments map[string]string `yaml:"arguments"`
}
20 changes: 12 additions & 8 deletions pkg/scheduler/framework/session_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (ssn *Session) Reclaimable(reclaimer *api.TaskInfo, reclaimees []*api.TaskI

for _, tier := range ssn.Tiers {
for _, plugin := range tier.Plugins {
if plugin.ReclaimableDisabled {
if !isEnabled(plugin.EnabledReclaimable) {
continue
}
rf, found := ssn.reclaimableFns[plugin.Name]
Expand Down Expand Up @@ -107,7 +107,7 @@ func (ssn *Session) Preemptable(preemptor *api.TaskInfo, preemptees []*api.TaskI

for _, tier := range ssn.Tiers {
for _, plugin := range tier.Plugins {
if plugin.PreemptableDisabled {
if !isEnabled(plugin.EnabledPreemptable) {
continue
}

Expand Down Expand Up @@ -162,7 +162,7 @@ func (ssn *Session) Overused(queue *api.QueueInfo) bool {
func (ssn *Session) JobReady(obj interface{}) bool {
for _, tier := range ssn.Tiers {
for _, plugin := range tier.Plugins {
if plugin.JobReadyDisabled {
if !isEnabled(plugin.EnabledJobReady) {
continue
}
jrf, found := ssn.jobReadyFns[plugin.Name]
Expand Down Expand Up @@ -200,7 +200,7 @@ func (ssn *Session) JobValid(obj interface{}) *api.ValidateResult {
func (ssn *Session) JobOrderFn(l, r interface{}) bool {
for _, tier := range ssn.Tiers {
for _, plugin := range tier.Plugins {
if plugin.JobOrderDisabled {
if !isEnabled(plugin.EnabledJobOrder) {
continue
}
jof, found := ssn.jobOrderFns[plugin.Name]
Expand All @@ -226,7 +226,7 @@ func (ssn *Session) JobOrderFn(l, r interface{}) bool {
func (ssn *Session) QueueOrderFn(l, r interface{}) bool {
for _, tier := range ssn.Tiers {
for _, plugin := range tier.Plugins {
if plugin.QueueOrderDisabled {
if !isEnabled(plugin.EnabledQueueOrder) {
continue
}
qof, found := ssn.queueOrderFns[plugin.Name]
Expand All @@ -253,7 +253,7 @@ func (ssn *Session) QueueOrderFn(l, r interface{}) bool {
func (ssn *Session) TaskCompareFns(l, r interface{}) int {
for _, tier := range ssn.Tiers {
for _, plugin := range tier.Plugins {
if plugin.TaskOrderDisabled {
if !isEnabled(plugin.EnabledTaskOrder) {
continue
}
tof, found := ssn.taskOrderFns[plugin.Name]
Expand Down Expand Up @@ -287,7 +287,7 @@ func (ssn *Session) TaskOrderFn(l, r interface{}) bool {
func (ssn *Session) PredicateFn(task *api.TaskInfo, node *api.NodeInfo) error {
for _, tier := range ssn.Tiers {
for _, plugin := range tier.Plugins {
if plugin.PredicateDisabled {
if !isEnabled(plugin.EnabledPredicate) {
continue
}
pfn, found := ssn.predicateFns[plugin.Name]
Expand All @@ -307,7 +307,7 @@ func (ssn *Session) NodeOrderFn(task *api.TaskInfo, node *api.NodeInfo) (int, er
priorityScore := 0
for _, tier := range ssn.Tiers {
for _, plugin := range tier.Plugins {
if plugin.NodeOrderDisabled {
if !isEnabled(plugin.EnabledNodeOrder) {
continue
}
pfn, found := ssn.nodeOrderFns[plugin.Name]
Expand All @@ -324,3 +324,7 @@ func (ssn *Session) NodeOrderFn(task *api.TaskInfo, node *api.NodeInfo) (int, er
}
return priorityScore, nil
}

func isEnabled(enabled *bool) bool {
return enabled != nil && *enabled
}
49 changes: 49 additions & 0 deletions pkg/scheduler/plugins/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2019 The Kubernetes 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 plugins

import "github.com/kubernetes-sigs/kube-batch/pkg/scheduler/conf"

// ApplyPluginConfDefaults sets option's filed to its default value if not set
func ApplyPluginConfDefaults(option *conf.PluginOption) {
t := true

if option.EnabledJobOrder == nil {
option.EnabledJobOrder = &t
}
if option.EnabledJobReady == nil {
option.EnabledJobReady = &t
}
if option.EnabledTaskOrder == nil {
option.EnabledTaskOrder = &t
}
if option.EnabledPreemptable == nil {
option.EnabledPreemptable = &t
}
if option.EnabledReclaimable == nil {
option.EnabledReclaimable = &t
}
if option.EnabledQueueOrder == nil {
option.EnabledQueueOrder = &t
}
if option.EnabledPredicate == nil {
option.EnabledPredicate = &t
}
if option.EnabledNodeOrder == nil {
option.EnabledNodeOrder = &t
}
}
9 changes: 9 additions & 0 deletions pkg/scheduler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/kubernetes-sigs/kube-batch/pkg/scheduler/conf"
"github.com/kubernetes-sigs/kube-batch/pkg/scheduler/framework"
"github.com/kubernetes-sigs/kube-batch/pkg/scheduler/plugins"
)

var defaultSchedulerConf = `
Expand All @@ -51,6 +52,14 @@ func loadSchedulerConf(confStr string) ([]framework.Action, []conf.Tier, error)
if err := yaml.Unmarshal(buf, schedulerConf); err != nil {
return nil, nil, err
}

// Set default settings for each plugin if not set
for i, tier := range schedulerConf.Tiers {
for j := range tier.Plugins {
plugins.ApplyPluginConfDefaults(&schedulerConf.Tiers[i].Plugins[j])
}
}

actionNames := strings.Split(schedulerConf.Actions, ",")
for _, actionName := range actionNames {
if action, found := framework.GetAction(strings.TrimSpace(actionName)); found {
Expand Down
139 changes: 139 additions & 0 deletions pkg/scheduler/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright 2019 The Kubernetes 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 scheduler

import (
"reflect"
"testing"

_ "github.com/kubernetes-sigs/kube-batch/pkg/scheduler/actions"
"github.com/kubernetes-sigs/kube-batch/pkg/scheduler/conf"
)

func TestLoadSchedulerConf(t *testing.T) {
configuration := `
actions: "allocate, backfill"
tiers:
- plugins:
- name: priority
- name: gang
- name: conformance
- plugins:
- name: drf
- name: predicates
- name: proportion
- name: nodeorder
`

trueValue := true
expectedTiers := []conf.Tier{
{
Plugins: []conf.PluginOption{
{
Name: "priority",
EnabledJobOrder: &trueValue,
EnabledJobReady: &trueValue,
EnabledTaskOrder: &trueValue,
EnabledPreemptable: &trueValue,
EnabledReclaimable: &trueValue,
EnabledQueueOrder: &trueValue,
EnabledPredicate: &trueValue,
EnabledNodeOrder: &trueValue,
},
{
Name: "gang",
EnabledJobOrder: &trueValue,
EnabledJobReady: &trueValue,
EnabledTaskOrder: &trueValue,
EnabledPreemptable: &trueValue,
EnabledReclaimable: &trueValue,
EnabledQueueOrder: &trueValue,
EnabledPredicate: &trueValue,
EnabledNodeOrder: &trueValue,
},
{
Name: "conformance",
EnabledJobOrder: &trueValue,
EnabledJobReady: &trueValue,
EnabledTaskOrder: &trueValue,
EnabledPreemptable: &trueValue,
EnabledReclaimable: &trueValue,
EnabledQueueOrder: &trueValue,
EnabledPredicate: &trueValue,
EnabledNodeOrder: &trueValue,
},
},
},
{
Plugins: []conf.PluginOption{
{
Name: "drf",
EnabledJobOrder: &trueValue,
EnabledJobReady: &trueValue,
EnabledTaskOrder: &trueValue,
EnabledPreemptable: &trueValue,
EnabledReclaimable: &trueValue,
EnabledQueueOrder: &trueValue,
EnabledPredicate: &trueValue,
EnabledNodeOrder: &trueValue,
},
{
Name: "predicates",
EnabledJobOrder: &trueValue,
EnabledJobReady: &trueValue,
EnabledTaskOrder: &trueValue,
EnabledPreemptable: &trueValue,
EnabledReclaimable: &trueValue,
EnabledQueueOrder: &trueValue,
EnabledPredicate: &trueValue,
EnabledNodeOrder: &trueValue,
},
{
Name: "proportion",
EnabledJobOrder: &trueValue,
EnabledJobReady: &trueValue,
EnabledTaskOrder: &trueValue,
EnabledPreemptable: &trueValue,
EnabledReclaimable: &trueValue,
EnabledQueueOrder: &trueValue,
EnabledPredicate: &trueValue,
EnabledNodeOrder: &trueValue,
},
{
Name: "nodeorder",
EnabledJobOrder: &trueValue,
EnabledJobReady: &trueValue,
EnabledTaskOrder: &trueValue,
EnabledPreemptable: &trueValue,
EnabledReclaimable: &trueValue,
EnabledQueueOrder: &trueValue,
EnabledPredicate: &trueValue,
EnabledNodeOrder: &trueValue,
},
},
},
}

_, tiers, err := loadSchedulerConf(configuration)
if err != nil {
t.Errorf("Failed to load scheduler configuration: %v", err)
}
if !reflect.DeepEqual(tiers, expectedTiers) {
t.Errorf("Failed to set default settings for plugins, expected: %+v, got %+v",
expectedTiers, tiers)
}
}

0 comments on commit 3644240

Please sign in to comment.