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

Commit a077652

Browse files
Support Weight for NodeOrder Plugin
1 parent 1512f6e commit a077652

File tree

1 file changed

+103
-4
lines changed

1 file changed

+103
-4
lines changed

pkg/scheduler/plugins/nodeorder/nodeorder.go

+103-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package nodeorder
1818

1919
import (
2020
"fmt"
21+
"strconv"
2122

2223
"github.com/golang/glog"
2324

@@ -32,6 +33,17 @@ import (
3233
"github.com/kubernetes-sigs/kube-batch/pkg/scheduler/framework"
3334
)
3435

36+
const (
37+
// NodeAffinityWeight is the key for providing Node Affinity Priority Weight in YAML
38+
NodeAffinityWeight = "nodeaffinity.weight"
39+
// PodAffinityWeight is the key for providing Pod Affinity Priority Weight in YAML
40+
PodAffinityWeight = "podaffinity.weight"
41+
// LeastRequestedWeight is the key for providing Least Requested Priority Weight in YAML
42+
LeastRequestedWeight = "leastrequested.weight"
43+
// BalancedResourceWeight is the key for providing Balanced Resource Priority Weight in YAML
44+
BalancedResourceWeight = "balancedresource.weight"
45+
)
46+
3547
type nodeOrderPlugin struct {
3648
// Arguments given for the plugin
3749
pluginArguments map[string]string
@@ -155,9 +167,92 @@ func (pp *nodeOrderPlugin) Name() string {
155167
return "nodeorder"
156168
}
157169

170+
type priorityWeight struct {
171+
leastReqWeight int
172+
nodeAffinityWeight int
173+
podAffinityWeight int
174+
balancedRescourceWeight int
175+
}
176+
177+
func calculateWeight(args map[string]string) priorityWeight {
178+
/*
179+
User Should give priorityWeight in this format(nodeaffinity.weight, podaffinity.weight, leastrequested.weight, balancedresource.weight).
180+
Currently supported only for nodeaffinity, podaffinity, leastrequested, balancedresouce priorities.
181+
182+
actions: "reclaim, allocate, backfill, preempt"
183+
tiers:
184+
- plugins:
185+
- name: priority
186+
- name: gang
187+
- name: conformance
188+
- plugins:
189+
- name: drf
190+
- name: predicates
191+
- name: proportion
192+
- name: nodeorder
193+
arguments:
194+
nodeaffinity.weight: 2
195+
podaffinity.weight: 2
196+
leastrequested.weight: 2
197+
balancedresource.weight: 2
198+
*/
199+
200+
// Values are initialized to 1.
201+
weight := priorityWeight{
202+
leastReqWeight: 1,
203+
nodeAffinityWeight: 1,
204+
podAffinityWeight: 1,
205+
balancedRescourceWeight: 1,
206+
}
207+
208+
// 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+
}
217+
218+
// 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+
}
227+
228+
// 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+
}
237+
238+
// 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+
}
247+
248+
return weight
249+
}
250+
158251
func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
159252
nodeOrderFn := func(task *api.TaskInfo, node *api.NodeInfo) (int, error) {
160253

254+
weight := calculateWeight(pp.pluginArguments)
255+
161256
pl := &podLister{
162257
session: ssn,
163258
}
@@ -188,21 +283,24 @@ func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
188283
glog.Warningf("Least Requested Priority Failed because of Error: %v", err)
189284
return 0, err
190285
}
191-
score = score + host.Score
286+
// If leastReqWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
287+
score = score + (host.Score * weight.leastReqWeight)
192288

193289
host, err = priorities.BalancedResourceAllocationMap(task.Pod, nil, nodeInfo)
194290
if err != nil {
195291
glog.Warningf("Balanced Resource Allocation Priority Failed because of Error: %v", err)
196292
return 0, err
197293
}
198-
score = score + host.Score
294+
// If balancedRescourceWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
295+
score = score + (host.Score * weight.balancedRescourceWeight)
199296

200297
host, err = priorities.CalculateNodeAffinityPriorityMap(task.Pod, nil, nodeInfo)
201298
if err != nil {
202299
glog.Warningf("Calculate Node Affinity Priority Failed because of Error: %v", err)
203300
return 0, err
204301
}
205-
score = score + host.Score
302+
// If nodeAffinityWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
303+
score = score + (host.Score * weight.nodeAffinityWeight)
206304

207305
mapFn := priorities.NewInterPodAffinityPriority(cn, nl, pl, v1.DefaultHardPodAffinitySymmetricWeight)
208306
interPodAffinityScore, err = mapFn(task.Pod, nodeMap, nodeSlice)
@@ -211,7 +309,8 @@ func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
211309
return 0, err
212310
}
213311
hostScore := getInterPodAffinityScore(node.Name, interPodAffinityScore)
214-
score = score + hostScore
312+
// If podAffinityWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
313+
score = score + (hostScore * weight.podAffinityWeight)
215314

216315
glog.V(4).Infof("Total Score for that node is: %d", score)
217316
return score, nil

0 commit comments

Comments
 (0)