From 043f39b49653e2f604560f382860d4d759eaa5cb Mon Sep 17 00:00:00 2001 From: ravisantoshgudimetla Date: Mon, 13 Mar 2017 14:31:21 -0400 Subject: [PATCH] Changes for removing deadcode in taint_tolerations Code cleanup with some modifications and a test-case in taints and tolerations Code cleanup with some modifications and a test-case in taints and tolerations Removed unnecessary code from my last commit Code cleanup with some modifications and a test-case in taints and tolerations SUggested changes for taints_tolerations Changes for removing deadcode in taint_tolerations small changes again small changes again Small changes for clear documentation. --- .../algorithm/priorities/metadata.go | 7 ++-- .../algorithm/priorities/taint_toleration.go | 32 +++++++------------ .../priorities/taint_toleration_test.go | 20 ++++++++++++ 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/plugin/pkg/scheduler/algorithm/priorities/metadata.go b/plugin/pkg/scheduler/algorithm/priorities/metadata.go index 112501bb313..e0eda3b81b1 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/metadata.go +++ b/plugin/pkg/scheduler/algorithm/priorities/metadata.go @@ -34,13 +34,10 @@ func PriorityMetadata(pod *v1.Pod, nodeNameToInfo map[string]*schedulercache.Nod if pod == nil { return nil } - tolerations, err := getTolerationListFromPod(pod) - if err != nil { - return nil - } + tolerationsPreferNoSchedule := getAllTolerationPreferNoSchedule(pod.Spec.Tolerations) return &priorityMetadata{ nonZeroRequest: getNonZeroRequests(pod), - podTolerations: tolerations, + podTolerations: tolerationsPreferNoSchedule, affinity: schedulercache.ReconcileAffinity(pod), } } diff --git a/plugin/pkg/scheduler/algorithm/priorities/taint_toleration.go b/plugin/pkg/scheduler/algorithm/priorities/taint_toleration.go index b88db85585a..766b7aae39a 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/taint_toleration.go +++ b/plugin/pkg/scheduler/algorithm/priorities/taint_toleration.go @@ -27,56 +27,48 @@ import ( // CountIntolerableTaintsPreferNoSchedule gives the count of intolerable taints of a pod with effect PreferNoSchedule func countIntolerableTaintsPreferNoSchedule(taints []v1.Taint, tolerations []v1.Toleration) (intolerableTaints int) { - for i := range taints { - taint := &taints[i] + for _, taint := range taints { // check only on taints that have effect PreferNoSchedule if taint.Effect != v1.TaintEffectPreferNoSchedule { continue } - if !v1.TolerationsTolerateTaint(tolerations, taint) { + if !v1.TolerationsTolerateTaint(tolerations, &taint) { intolerableTaints++ } } return } -// getAllTolerationEffectPreferNoSchedule gets the list of all Toleration with Effect PreferNoSchedule +// getAllTolerationEffectPreferNoSchedule gets the list of all Tolerations with Effect PreferNoSchedule or with no effect. func getAllTolerationPreferNoSchedule(tolerations []v1.Toleration) (tolerationList []v1.Toleration) { - for i := range tolerations { - toleration := &tolerations[i] + for _, toleration := range tolerations { + // Empty effect means all effects which includes PreferNoSchedule, so we need to collect it as well. if len(toleration.Effect) == 0 || toleration.Effect == v1.TaintEffectPreferNoSchedule { - tolerationList = append(tolerationList, *toleration) + tolerationList = append(tolerationList, toleration) } } return } -func getTolerationListFromPod(pod *v1.Pod) ([]v1.Toleration, error) { - return getAllTolerationPreferNoSchedule(pod.Spec.Tolerations), nil -} - // ComputeTaintTolerationPriority prepares the priority list for all the nodes based on the number of intolerable taints on the node func ComputeTaintTolerationPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (schedulerapi.HostPriority, error) { node := nodeInfo.Node() if node == nil { return schedulerapi.HostPriority{}, fmt.Errorf("node not found") } - - var tolerationList []v1.Toleration + // To hold all the tolerations with Effect PreferNoSchedule + var tolerationsPreferNoSchedule []v1.Toleration if priorityMeta, ok := meta.(*priorityMetadata); ok { - tolerationList = priorityMeta.podTolerations + tolerationsPreferNoSchedule = priorityMeta.podTolerations + } else { - var err error - tolerationList, err = getTolerationListFromPod(pod) - if err != nil { - return schedulerapi.HostPriority{}, err - } + tolerationsPreferNoSchedule = getAllTolerationPreferNoSchedule(pod.Spec.Tolerations) } return schedulerapi.HostPriority{ Host: node.Name, - Score: countIntolerableTaintsPreferNoSchedule(node.Spec.Taints, tolerationList), + Score: countIntolerableTaintsPreferNoSchedule(node.Spec.Taints, tolerationsPreferNoSchedule), }, nil } diff --git a/plugin/pkg/scheduler/algorithm/priorities/taint_toleration_test.go b/plugin/pkg/scheduler/algorithm/priorities/taint_toleration_test.go index aaec6db2065..b36e598a01d 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/taint_toleration_test.go +++ b/plugin/pkg/scheduler/algorithm/priorities/taint_toleration_test.go @@ -204,6 +204,26 @@ func TestTaintAndToleration(t *testing.T) { {Host: "nodeC", Score: 0}, }, }, + { + test: "Default behaviour No taints and tolerations, lands on node with no taints", + //pod without tolerations + pod: podWithTolerations([]v1.Toleration{}), + nodes: []*v1.Node{ + //Node without taints + nodeWithTaints("nodeA", []v1.Taint{}), + nodeWithTaints("nodeB", []v1.Taint{ + { + Key: "cpu-type", + Value: "arm64", + Effect: v1.TaintEffectPreferNoSchedule, + }, + }), + }, + expectedList: []schedulerapi.HostPriority{ + {Host: "nodeA", Score: 10}, + {Host: "nodeB", Score: 0}, + }, + }, } for _, test := range tests { nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes)