@@ -18,6 +18,7 @@ package nodeorder
18
18
19
19
import (
20
20
"fmt"
21
+ "strconv"
21
22
22
23
"github.com/golang/glog"
23
24
@@ -32,7 +33,20 @@ import (
32
33
"github.com/kubernetes-sigs/kube-batch/pkg/scheduler/framework"
33
34
)
34
35
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
+
35
47
type nodeOrderPlugin struct {
48
+ // Arguments given for the plugin
49
+ pluginArguments map [string ]string
36
50
}
37
51
38
52
func getInterPodAffinityScore (name string , interPodAffinityScore schedulerapi.HostPriorityList ) int {
@@ -145,17 +159,100 @@ func (nl *nodeLister) List() ([]*v1.Node, error) {
145
159
}
146
160
147
161
//New function returns prioritizePlugin object
148
- func New () framework.Plugin {
149
- return & nodeOrderPlugin {}
162
+ func New (aruguments map [ string ] string ) framework.Plugin {
163
+ return & nodeOrderPlugin {pluginArguments : aruguments }
150
164
}
151
165
152
166
func (pp * nodeOrderPlugin ) Name () string {
153
167
return "nodeorder"
154
168
}
155
169
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
+
156
251
func (pp * nodeOrderPlugin ) OnSessionOpen (ssn * framework.Session ) {
157
252
nodeOrderFn := func (task * api.TaskInfo , node * api.NodeInfo ) (int , error ) {
158
253
254
+ weight := calculateWeight (pp .pluginArguments )
255
+
159
256
pl := & podLister {
160
257
session : ssn ,
161
258
}
@@ -186,14 +283,24 @@ func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
186
283
glog .Warningf ("Least Requested Priority Failed because of Error: %v" , err )
187
284
return 0 , err
188
285
}
189
- 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 )
288
+
289
+ host , err = priorities .BalancedResourceAllocationMap (task .Pod , nil , nodeInfo )
290
+ if err != nil {
291
+ glog .Warningf ("Balanced Resource Allocation Priority Failed because of Error: %v" , err )
292
+ return 0 , err
293
+ }
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 )
190
296
191
297
host , err = priorities .CalculateNodeAffinityPriorityMap (task .Pod , nil , nodeInfo )
192
298
if err != nil {
193
299
glog .Warningf ("Calculate Node Affinity Priority Failed because of Error: %v" , err )
194
300
return 0 , err
195
301
}
196
- 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 )
197
304
198
305
mapFn := priorities .NewInterPodAffinityPriority (cn , nl , pl , v1 .DefaultHardPodAffinitySymmetricWeight )
199
306
interPodAffinityScore , err = mapFn (task .Pod , nodeMap , nodeSlice )
@@ -202,7 +309,8 @@ func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
202
309
return 0 , err
203
310
}
204
311
hostScore := getInterPodAffinityScore (node .Name , interPodAffinityScore )
205
- 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 )
206
314
207
315
glog .V (4 ).Infof ("Total Score for that node is: %d" , score )
208
316
return score , nil
0 commit comments