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

Commit 6421a63

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

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

pkg/scheduler/plugins/nodeorder/nodeorder.go

+92-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

@@ -155,9 +156,92 @@ func (pp *nodeOrderPlugin) Name() string {
155156
return "nodeorder"
156157
}
157158

159+
type priorityWeight struct {
160+
leastReqWeight int
161+
nodeAffinityWeight int
162+
podAffinityWeight int
163+
balancedRescourceWeight int
164+
}
165+
166+
func calculateWeight(args map[string]string) priorityWeight {
167+
/*
168+
User Should give priorityWeight in this format(nodeaffinity.weight, podaffinity.weight, leastrequested.weight, balancedresource.weight).
169+
Currently supported only for nodeaffinity, podaffinity, leastrequested, balancedresouce priorities.
170+
171+
actions: "reclaim, allocate, backfill, preempt"
172+
tiers:
173+
- plugins:
174+
- name: priority
175+
- name: gang
176+
- name: conformance
177+
- plugins:
178+
- name: drf
179+
- name: predicates
180+
- name: proportion
181+
- name: nodeorder
182+
arguments:
183+
nodeaffinity.weight: 2
184+
podaffinity.weight: 2
185+
leastrequested.weight: 2
186+
balancedresource.weight: 2
187+
*/
188+
189+
// Values are initialized to 1.
190+
weight := priorityWeight{
191+
leastReqWeight: 1,
192+
nodeAffinityWeight: 1,
193+
podAffinityWeight: 1,
194+
balancedRescourceWeight: 1,
195+
}
196+
197+
// Checks whether nodeaffinity.weight is provided or not, if given, modifies the value in weight struct.
198+
if args["nodeaffinity.weight"] != "" {
199+
val, err := strconv.Atoi(args["nodeaffinity.weight"])
200+
if err != nil {
201+
glog.Warningf("Not able to Parse Weight for %v because of error: %v", args["nodeaffinity.weight"], err)
202+
} else {
203+
weight.nodeAffinityWeight = val
204+
}
205+
}
206+
207+
// Checks whether podaffinity.weight is provided or not, if given, modifies the value in weight struct.
208+
if args["podaffinity.weight"] != "" {
209+
val, err := strconv.Atoi(args["podaffinity.weight"])
210+
if err != nil {
211+
glog.Warningf("Not able to Parse Weight for %v because of error: %v", args["podaffinity.weight"], err)
212+
} else {
213+
weight.podAffinityWeight = val
214+
}
215+
}
216+
217+
// Checks whether leastrequested.weight is provided or not, if given, modifies the value in weight struct.
218+
if args["leastrequested.weight"] != "" {
219+
val, err := strconv.Atoi(args["leastrequested.weight"])
220+
if err != nil {
221+
glog.Warningf("Not able to Parse Weight for %v because of error: %v", args["leastrequested.weight"], err)
222+
} else {
223+
weight.leastReqWeight = val
224+
}
225+
}
226+
227+
// Checks whether balancedresource.weight is provided or not, if given, modifies the value in weight struct.
228+
if args["balancedresource.weight"] != "" {
229+
val, err := strconv.Atoi(args["balancedresource.weight"])
230+
if err != nil {
231+
glog.Warningf("Not able to Parse Weight for %v because of error: %v", args["balancedresource.weight"], err)
232+
} else {
233+
weight.balancedRescourceWeight = val
234+
}
235+
}
236+
237+
return weight
238+
}
239+
158240
func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
159241
nodeOrderFn := func(task *api.TaskInfo, node *api.NodeInfo) (int, error) {
160242

243+
weight := calculateWeight(pp.pluginArguments)
244+
161245
pl := &podLister{
162246
session: ssn,
163247
}
@@ -188,21 +272,24 @@ func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
188272
glog.Warningf("Least Requested Priority Failed because of Error: %v", err)
189273
return 0, err
190274
}
191-
score = score + host.Score
275+
// If leastReqWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
276+
score = score + (host.Score * weight.leastReqWeight)
192277

193278
host, err = priorities.BalancedResourceAllocationMap(task.Pod, nil, nodeInfo)
194279
if err != nil {
195280
glog.Warningf("Balanced Resource Allocation Priority Failed because of Error: %v", err)
196281
return 0, err
197282
}
198-
score = score + host.Score
283+
// If balancedRescourceWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
284+
score = score + (host.Score * weight.balancedRescourceWeight)
199285

200286
host, err = priorities.CalculateNodeAffinityPriorityMap(task.Pod, nil, nodeInfo)
201287
if err != nil {
202288
glog.Warningf("Calculate Node Affinity Priority Failed because of Error: %v", err)
203289
return 0, err
204290
}
205-
score = score + host.Score
291+
// If nodeAffinityWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
292+
score = score + (host.Score * weight.nodeAffinityWeight)
206293

207294
mapFn := priorities.NewInterPodAffinityPriority(cn, nl, pl, v1.DefaultHardPodAffinitySymmetricWeight)
208295
interPodAffinityScore, err = mapFn(task.Pod, nodeMap, nodeSlice)
@@ -211,7 +298,8 @@ func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
211298
return 0, err
212299
}
213300
hostScore := getInterPodAffinityScore(node.Name, interPodAffinityScore)
214-
score = score + hostScore
301+
// If podAffinityWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
302+
score = score + (hostScore * weight.podAffinityWeight)
215303

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

0 commit comments

Comments
 (0)