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

Support Weight for NodeOrder Plugin #640

Merged
merged 1 commit into from
Mar 14, 2019
Merged
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
107 changes: 103 additions & 4 deletions pkg/scheduler/plugins/nodeorder/nodeorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package nodeorder

import (
"fmt"
"strconv"

"github.com/golang/glog"

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

const (
// NodeAffinityWeight is the key for providing Node Affinity Priority Weight in YAML
NodeAffinityWeight = "nodeaffinity.weight"
// PodAffinityWeight is the key for providing Pod Affinity Priority Weight in YAML
PodAffinityWeight = "podaffinity.weight"
// LeastRequestedWeight is the key for providing Least Requested Priority Weight in YAML
LeastRequestedWeight = "leastrequested.weight"
// BalancedResourceWeight is the key for providing Balanced Resource Priority Weight in YAML
BalancedResourceWeight = "balancedresource.weight"
)

type nodeOrderPlugin struct {
// Arguments given for the plugin
pluginArguments map[string]string
Expand Down Expand Up @@ -155,9 +167,92 @@ func (pp *nodeOrderPlugin) Name() string {
return "nodeorder"
}

type priorityWeight struct {
leastReqWeight int
nodeAffinityWeight int
podAffinityWeight int
balancedRescourceWeight int
}

func calculateWeight(args map[string]string) 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.

actions: "reclaim, allocate, backfill, preempt"
tiers:
- plugins:
- name: priority
- name: gang
- name: conformance
- plugins:
- name: drf
- name: predicates
- name: proportion
- name: nodeorder
arguments:
nodeaffinity.weight: 2
podaffinity.weight: 2
leastrequested.weight: 2
balancedresource.weight: 2
*/

// Values are initialized to 1.
weight := priorityWeight{
leastReqWeight: 1,
nodeAffinityWeight: 1,
podAffinityWeight: 1,
balancedRescourceWeight: 1,
}

// 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
}
}

// 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
}
}

// 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
}
}

// 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
}
}

return weight
}

func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
nodeOrderFn := func(task *api.TaskInfo, node *api.NodeInfo) (int, error) {

weight := calculateWeight(pp.pluginArguments)

pl := &podLister{
session: ssn,
}
Expand Down Expand Up @@ -188,21 +283,24 @@ func (pp *nodeOrderPlugin) OnSessionOpen(ssn *framework.Session) {
glog.Warningf("Least Requested Priority Failed because of Error: %v", err)
return 0, err
}
score = score + host.Score
// If leastReqWeight in provided, host.Score is multiplied with weight, if not, host.Score is added to total score.
score = score + (host.Score * weight.leastReqWeight)

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

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

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

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