From 629d5cc897e71af6bade2eff57cba12e8667dcb0 Mon Sep 17 00:00:00 2001 From: thandayuthapani Date: Wed, 13 Mar 2019 15:29:18 +0530 Subject: [PATCH] Support Weight for NodeOrder Plugin --- pkg/scheduler/plugins/nodeorder/nodeorder.go | 112 ++++++++++++++++++- 1 file changed, 109 insertions(+), 3 deletions(-) diff --git a/pkg/scheduler/plugins/nodeorder/nodeorder.go b/pkg/scheduler/plugins/nodeorder/nodeorder.go index d2b40f6f1..32b964ac5 100644 --- a/pkg/scheduler/plugins/nodeorder/nodeorder.go +++ b/pkg/scheduler/plugins/nodeorder/nodeorder.go @@ -18,6 +18,7 @@ package nodeorder import ( "fmt" + "strconv" "github.com/golang/glog" @@ -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 { } @@ -153,9 +165,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, } @@ -186,14 +281,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 + } + // 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) @@ -202,7 +307,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