Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Add type Argument with some common parse function #705

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkg/scheduler/framework/arguments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 framework
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add license there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy license from other file.

And I think the license may be outdated now?

Copyright 2018 The Kubernetes Authors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use 2019 for new added files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New added files (arguments.go arguments_test.go) is now in license of 2019.


import (
"strconv"

"github.com/golang/glog"
)

type Arguments map[string]string

func (a Arguments) GetInt(ptr *int, key string) {
if ptr == nil {
return
}

argv, ok := a[key]
if !ok || argv == "" {
return
}

value, err := strconv.Atoi(argv)
if err != nil {
glog.Warningf("Could not parse argument: %s for key %s, with err %v", argv, key, err)
return
Copy link
Contributor

@k82cn k82cn Apr 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the return value is (int, error)

It's ok to me :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. :)

}

*ptr = value
}
76 changes: 76 additions & 0 deletions pkg/scheduler/framework/arguments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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 framework

import (
"testing"
)

type GetIntTestCases struct {
arg Arguments
key string
baseValue int
expectValue int
}

func TestArgumentsGetInt(t *testing.T) {
key1 := "intkey"

cases := []GetIntTestCases{
{
arg: Arguments{
"anotherkey": "12",
},
key: key1,
baseValue: 10,
expectValue: 10,
},
{
arg: Arguments{
key1: "15",
},
key: key1,
baseValue: 10,
expectValue: 15,
},
{
arg: Arguments{
key1: "errorvalue",
},
key: key1,
baseValue: 11,
expectValue: 11,
},
{
arg: Arguments{
key1: "",
},
key: key1,
baseValue: 0,
expectValue: 0,
},
}

for index, c := range cases {
baseValue := c.baseValue
c.arg.GetInt(nil, c.key)
c.arg.GetInt(&baseValue, c.key)
if baseValue != c.expectValue {
t.Errorf("index %d, value should be %v, but not %v", index, c.expectValue, baseValue)
}
}
}
4 changes: 2 additions & 2 deletions pkg/scheduler/framework/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import "sync"

var pluginMutex sync.Mutex

type PluginBuilder func(map[string]string) Plugin
type PluginBuilder func(Arguments) Plugin

// Plugin management
var pluginBuilders = map[string]PluginBuilder{}

func RegisterPluginBuilder(name string, pc func(map[string]string) Plugin) {
func RegisterPluginBuilder(name string, pc PluginBuilder) {
pluginMutex.Lock()
defer pluginMutex.Unlock()

Expand Down
6 changes: 3 additions & 3 deletions pkg/scheduler/plugins/conformance/conformance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package conformance

import (
"k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/apis/scheduling"

"github.com/kubernetes-sigs/kube-batch/pkg/scheduler/api"
Expand All @@ -26,10 +26,10 @@ import (

type conformancePlugin struct {
// Arguments given for the plugin
pluginArguments map[string]string
pluginArguments framework.Arguments
}

func New(arguments map[string]string) framework.Plugin {
func New(arguments framework.Arguments) framework.Plugin {
return &conformancePlugin{pluginArguments: arguments}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/plugins/drf/drf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ type drfPlugin struct {
jobOpts map[api.JobID]*drfAttr

// Arguments given for the plugin
pluginArguments map[string]string
pluginArguments framework.Arguments
}

func New(arguments map[string]string) framework.Plugin {
func New(arguments framework.Arguments) framework.Plugin {
return &drfPlugin{
totalResource: api.EmptyResource(),
jobOpts: map[api.JobID]*drfAttr{},
Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/plugins/gang/gang.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (

type gangPlugin struct {
// Arguments given for the plugin
pluginArguments map[string]string
pluginArguments framework.Arguments
}

func New(arguments map[string]string) framework.Plugin {
func New(arguments framework.Arguments) framework.Plugin {
return &gangPlugin{pluginArguments: arguments}
}

Expand Down
45 changes: 8 additions & 37 deletions pkg/scheduler/plugins/nodeorder/nodeorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ package nodeorder

import (
"fmt"
"strconv"

"github.com/golang/glog"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/kubernetes/pkg/scheduler/algorithm"
"k8s.io/kubernetes/pkg/scheduler/algorithm/priorities"
Expand All @@ -46,7 +45,7 @@ const (

type nodeOrderPlugin struct {
// Arguments given for the plugin
pluginArguments map[string]string
pluginArguments framework.Arguments
}

func getInterPodAffinityScore(name string, interPodAffinityScore schedulerapi.HostPriorityList) int {
Expand Down Expand Up @@ -159,7 +158,7 @@ func (nl *nodeLister) List() ([]*v1.Node, error) {
}

//New function returns prioritizePlugin object
func New(aruguments map[string]string) framework.Plugin {
func New(aruguments framework.Arguments) framework.Plugin {
return &nodeOrderPlugin{pluginArguments: aruguments}
}

Expand All @@ -174,7 +173,7 @@ type priorityWeight struct {
balancedRescourceWeight int
}

func calculateWeight(args map[string]string) priorityWeight {
func calculateWeight(args framework.Arguments) priorityWeight {
/*
User Should give priorityWeight in this format(nodeaffinity.weight, podaffinity.weight, leastrequested.weight, balancedresource.weight).
Currently supported only for nodeaffinity, podaffinity, leastrequested, balancedresouce priorities.
Expand Down Expand Up @@ -206,44 +205,16 @@ func calculateWeight(args map[string]string) priorityWeight {
}

// Checks whether nodeaffinity.weight is provided or not, if given, modifies the value in weight struct.
if args[NodeAffinityWeight] != "" {
val, err := strconv.Atoi(args[NodeAffinityWeight])
if err != nil {
glog.Warningf("Not able to Parse Weight for %v because of error: %v", args[NodeAffinityWeight], err)
} else {
weight.nodeAffinityWeight = val
}
}
args.GetInt(&weight.nodeAffinityWeight, NodeAffinityWeight)

// Checks whether podaffinity.weight is provided or not, if given, modifies the value in weight struct.
if args[PodAffinityWeight] != "" {
val, err := strconv.Atoi(args[PodAffinityWeight])
if err != nil {
glog.Warningf("Not able to Parse Weight for %v because of error: %v", args[PodAffinityWeight], err)
} else {
weight.podAffinityWeight = val
}
}
args.GetInt(&weight.podAffinityWeight, PodAffinityWeight)

// Checks whether leastrequested.weight is provided or not, if given, modifies the value in weight struct.
if args[LeastRequestedWeight] != "" {
val, err := strconv.Atoi(args[LeastRequestedWeight])
if err != nil {
glog.Warningf("Not able to Parse Weight for %v because of error: %v", args[LeastRequestedWeight], err)
} else {
weight.leastReqWeight = val
}
}
args.GetInt(&weight.leastReqWeight, LeastRequestedWeight)

// Checks whether balancedresource.weight is provided or not, if given, modifies the value in weight struct.
if args[BalancedResourceWeight] != "" {
val, err := strconv.Atoi(args[BalancedResourceWeight])
if err != nil {
glog.Warningf("Not able to Parse Weight for %v because of error: %v", args[BalancedResourceWeight], err)
} else {
weight.balancedRescourceWeight = val
}
}
args.GetInt(&weight.balancedRescourceWeight, BalancedResourceWeight)

return weight
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/scheduler/plugins/predicates/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/golang/glog"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/kubernetes/pkg/scheduler/algorithm"
"k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
Expand All @@ -33,10 +33,10 @@ import (

type predicatesPlugin struct {
// Arguments given for the plugin
pluginArguments map[string]string
pluginArguments framework.Arguments
}

func New(arguments map[string]string) framework.Plugin {
func New(arguments framework.Arguments) framework.Plugin {
return &predicatesPlugin{pluginArguments: arguments}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/plugins/priority/priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (

type priorityPlugin struct {
// Arguments given for the plugin
pluginArguments map[string]string
pluginArguments framework.Arguments
}

func New(arguments map[string]string) framework.Plugin {
func New(arguments framework.Arguments) framework.Plugin {
return &priorityPlugin{pluginArguments: arguments}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/plugins/proportion/proportion.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type proportionPlugin struct {
totalResource *api.Resource
queueOpts map[api.QueueID]*queueAttr
// Arguments given for the plugin
pluginArguments map[string]string
pluginArguments framework.Arguments
}

type queueAttr struct {
Expand All @@ -42,7 +42,7 @@ type queueAttr struct {
request *api.Resource
}

func New(arguments map[string]string) framework.Plugin {
func New(arguments framework.Arguments) framework.Plugin {
return &proportionPlugin{
totalResource: api.EmptyResource(),
queueOpts: map[api.QueueID]*queueAttr{},
Expand Down