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

Commit 44d5ef1

Browse files
committed
change plugin arguments to special type
1 parent f064e79 commit 44d5ef1

File tree

10 files changed

+144
-53
lines changed

10 files changed

+144
-53
lines changed

pkg/scheduler/framework/arguments.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package framework
18+
19+
import (
20+
"strconv"
21+
22+
"github.com/golang/glog"
23+
)
24+
25+
type Arguments map[string]string
26+
27+
func (a Arguments) GetInt(ptr *int, key string) {
28+
if ptr == nil {
29+
return
30+
}
31+
32+
argv, ok := a[key]
33+
if !ok || argv == "" {
34+
return
35+
}
36+
37+
value, err := strconv.Atoi(argv)
38+
if err != nil {
39+
glog.Warningf("Could not parse argument: %s for key %s, with err %v", argv, key, err)
40+
return
41+
}
42+
43+
*ptr = value
44+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package framework
18+
19+
import (
20+
"testing"
21+
)
22+
23+
type GetIntTestCases struct {
24+
arg Arguments
25+
key string
26+
baseValue int
27+
expectValue int
28+
}
29+
30+
func TestArgumentsGetInt(t *testing.T) {
31+
key1 := "intkey"
32+
33+
cases := []GetIntTestCases{
34+
{
35+
arg: Arguments{
36+
"anotherkey": "12",
37+
},
38+
key: key1,
39+
baseValue: 10,
40+
expectValue: 10,
41+
},
42+
{
43+
arg: Arguments{
44+
key1: "15",
45+
},
46+
key: key1,
47+
baseValue: 10,
48+
expectValue: 15,
49+
},
50+
{
51+
arg: Arguments{
52+
key1: "errorvalue",
53+
},
54+
key: key1,
55+
baseValue: 11,
56+
expectValue: 11,
57+
},
58+
{
59+
arg: Arguments{
60+
key1: "",
61+
},
62+
key: key1,
63+
baseValue: 0,
64+
expectValue: 0,
65+
},
66+
}
67+
68+
for index, c := range cases {
69+
baseValue := c.baseValue
70+
c.arg.GetInt(nil, c.key)
71+
c.arg.GetInt(&baseValue, c.key)
72+
if baseValue != c.expectValue {
73+
t.Errorf("index %d, value should be %v, but not %v", index, c.expectValue, baseValue)
74+
}
75+
}
76+
}

pkg/scheduler/framework/plugins.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import "sync"
2020

2121
var pluginMutex sync.Mutex
2222

23-
type PluginBuilder func(map[string]string) Plugin
23+
type PluginBuilder func(Arguments) Plugin
2424

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

28-
func RegisterPluginBuilder(name string, pc func(map[string]string) Plugin) {
28+
func RegisterPluginBuilder(name string, pc PluginBuilder) {
2929
pluginMutex.Lock()
3030
defer pluginMutex.Unlock()
3131

pkg/scheduler/plugins/conformance/conformance.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package conformance
1818

1919
import (
20-
"k8s.io/apimachinery/pkg/apis/meta/v1"
20+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2121
"k8s.io/kubernetes/pkg/apis/scheduling"
2222

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

2727
type conformancePlugin struct {
2828
// Arguments given for the plugin
29-
pluginArguments map[string]string
29+
pluginArguments framework.Arguments
3030
}
3131

32-
func New(arguments map[string]string) framework.Plugin {
32+
func New(arguments framework.Arguments) framework.Plugin {
3333
return &conformancePlugin{pluginArguments: arguments}
3434
}
3535

pkg/scheduler/plugins/drf/drf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ type drfPlugin struct {
4141
jobOpts map[api.JobID]*drfAttr
4242

4343
// Arguments given for the plugin
44-
pluginArguments map[string]string
44+
pluginArguments framework.Arguments
4545
}
4646

47-
func New(arguments map[string]string) framework.Plugin {
47+
func New(arguments framework.Arguments) framework.Plugin {
4848
return &drfPlugin{
4949
totalResource: api.EmptyResource(),
5050
jobOpts: map[api.JobID]*drfAttr{},

pkg/scheduler/plugins/gang/gang.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ import (
3232

3333
type gangPlugin struct {
3434
// Arguments given for the plugin
35-
pluginArguments map[string]string
35+
pluginArguments framework.Arguments
3636
}
3737

38-
func New(arguments map[string]string) framework.Plugin {
38+
func New(arguments framework.Arguments) framework.Plugin {
3939
return &gangPlugin{pluginArguments: arguments}
4040
}
4141

pkg/scheduler/plugins/nodeorder/nodeorder.go

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ package nodeorder
1818

1919
import (
2020
"fmt"
21-
"strconv"
2221

2322
"github.com/golang/glog"
2423

25-
"k8s.io/api/core/v1"
24+
v1 "k8s.io/api/core/v1"
2625
"k8s.io/apimachinery/pkg/labels"
2726
"k8s.io/kubernetes/pkg/scheduler/algorithm"
2827
"k8s.io/kubernetes/pkg/scheduler/algorithm/priorities"
@@ -46,7 +45,7 @@ const (
4645

4746
type nodeOrderPlugin struct {
4847
// Arguments given for the plugin
49-
pluginArguments map[string]string
48+
pluginArguments framework.Arguments
5049
}
5150

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

161160
//New function returns prioritizePlugin object
162-
func New(aruguments map[string]string) framework.Plugin {
161+
func New(aruguments framework.Arguments) framework.Plugin {
163162
return &nodeOrderPlugin{pluginArguments: aruguments}
164163
}
165164

@@ -174,7 +173,7 @@ type priorityWeight struct {
174173
balancedRescourceWeight int
175174
}
176175

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

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

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

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

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

248219
return weight
249220
}

pkg/scheduler/plugins/predicates/predicates.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/golang/glog"
2323

24-
"k8s.io/api/core/v1"
24+
v1 "k8s.io/api/core/v1"
2525
"k8s.io/apimachinery/pkg/labels"
2626
"k8s.io/kubernetes/pkg/scheduler/algorithm"
2727
"k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
@@ -33,10 +33,10 @@ import (
3333

3434
type predicatesPlugin struct {
3535
// Arguments given for the plugin
36-
pluginArguments map[string]string
36+
pluginArguments framework.Arguments
3737
}
3838

39-
func New(arguments map[string]string) framework.Plugin {
39+
func New(arguments framework.Arguments) framework.Plugin {
4040
return &predicatesPlugin{pluginArguments: arguments}
4141
}
4242

pkg/scheduler/plugins/priority/priority.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424

2525
type priorityPlugin struct {
2626
// Arguments given for the plugin
27-
pluginArguments map[string]string
27+
pluginArguments framework.Arguments
2828
}
2929

30-
func New(arguments map[string]string) framework.Plugin {
30+
func New(arguments framework.Arguments) framework.Plugin {
3131
return &priorityPlugin{pluginArguments: arguments}
3232
}
3333

pkg/scheduler/plugins/proportion/proportion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type proportionPlugin struct {
2828
totalResource *api.Resource
2929
queueOpts map[api.QueueID]*queueAttr
3030
// Arguments given for the plugin
31-
pluginArguments map[string]string
31+
pluginArguments framework.Arguments
3232
}
3333

3434
type queueAttr struct {
@@ -42,7 +42,7 @@ type queueAttr struct {
4242
request *api.Resource
4343
}
4444

45-
func New(arguments map[string]string) framework.Plugin {
45+
func New(arguments framework.Arguments) framework.Plugin {
4646
return &proportionPlugin{
4747
totalResource: api.EmptyResource(),
4848
queueOpts: map[api.QueueID]*queueAttr{},

0 commit comments

Comments
 (0)