Skip to content

Commit 2cd685a

Browse files
authored
Merge pull request kubernetes#87250 from starizard/adding-taint-toleration-error-reasons
Adding taint toleration error reasons
2 parents 98f63ee + 22bd26f commit 2cd685a

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

pkg/apis/core/v1/helper/helpers.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,22 +395,37 @@ type taintsFilterFunc func(*v1.Taint) bool
395395

396396
// TolerationsTolerateTaintsWithFilter checks if given tolerations tolerates
397397
// all the taints that apply to the filter in given taint list.
398+
// DEPRECATED: Please use FindMatchingUntoleratedTaint instead.
398399
func TolerationsTolerateTaintsWithFilter(tolerations []v1.Toleration, taints []v1.Taint, applyFilter taintsFilterFunc) bool {
399-
if len(taints) == 0 {
400-
return true
401-
}
400+
_, isUntolerated := FindMatchingUntoleratedTaint(taints, tolerations, applyFilter)
401+
return !isUntolerated
402+
}
402403

403-
for i := range taints {
404-
if applyFilter != nil && !applyFilter(&taints[i]) {
405-
continue
404+
// FindMatchingUntoleratedTaint checks if the given tolerations tolerates
405+
// all the filtered taints, and returns the first taint without a toleration
406+
func FindMatchingUntoleratedTaint(taints []v1.Taint, tolerations []v1.Toleration, inclusionFilter taintsFilterFunc) (v1.Taint, bool) {
407+
filteredTaints := getFilteredTaints(taints, inclusionFilter)
408+
for _, taint := range filteredTaints {
409+
if !TolerationsTolerateTaint(tolerations, &taint) {
410+
return taint, true
406411
}
412+
}
413+
return v1.Taint{}, false
414+
}
407415

408-
if !TolerationsTolerateTaint(tolerations, &taints[i]) {
409-
return false
416+
// getFilteredTaints returns a list of taints satisfying the filter predicate
417+
func getFilteredTaints(taints []v1.Taint, inclusionFilter taintsFilterFunc) []v1.Taint {
418+
if inclusionFilter == nil {
419+
return taints
420+
}
421+
filteredTaints := []v1.Taint{}
422+
for _, taint := range taints {
423+
if !inclusionFilter(&taint) {
424+
continue
410425
}
426+
filteredTaints = append(filteredTaints, taint)
411427
}
412-
413-
return true
428+
return filteredTaints
414429
}
415430

416431
// Returns true and list of Tolerations matching all Taints if all are tolerated, or false otherwise.

pkg/scheduler/framework/plugins/tainttoleration/taint_toleration.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,19 @@ func (pl *TaintToleration) Filter(ctx context.Context, state *framework.CycleSta
6262
return framework.NewStatus(framework.Error, err.Error())
6363
}
6464

65-
if v1helper.TolerationsTolerateTaintsWithFilter(pod.Spec.Tolerations, taints, func(t *v1.Taint) bool {
65+
filterPredicate := func(t *v1.Taint) bool {
6666
// PodToleratesNodeTaints is only interested in NoSchedule and NoExecute taints.
6767
return t.Effect == v1.TaintEffectNoSchedule || t.Effect == v1.TaintEffectNoExecute
68-
}) {
68+
}
69+
70+
taint, isUntolerated := v1helper.FindMatchingUntoleratedTaint(taints, pod.Spec.Tolerations, filterPredicate)
71+
if !isUntolerated {
6972
return nil
7073
}
7174

72-
return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonNotMatch)
75+
errReason := fmt.Sprintf("node(s) had taint {%s: %s}, that the pod didn't tolerate",
76+
taint.Key, taint.Value)
77+
return framework.NewStatus(framework.UnschedulableAndUnresolvable, errReason)
7378
}
7479

7580
// postFilterState computed at PostFilter and used at Score.

pkg/scheduler/framework/plugins/tainttoleration/taint_toleration_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,30 @@ func TestTaintTolerationScore(t *testing.T) {
260260
}
261261

262262
func TestTaintTolerationFilter(t *testing.T) {
263-
unschedulable := framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonNotMatch)
264263
tests := []struct {
265264
name string
266265
pod *v1.Pod
267266
node *v1.Node
268267
wantStatus *framework.Status
269268
}{
270269
{
271-
name: "A pod having no tolerations can't be scheduled onto a node with nonempty taints",
272-
pod: podWithTolerations("pod1", []v1.Toleration{}),
273-
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "dedicated", Value: "user1", Effect: "NoSchedule"}}),
274-
wantStatus: unschedulable,
270+
name: "A pod having no tolerations can't be scheduled onto a node with nonempty taints",
271+
pod: podWithTolerations("pod1", []v1.Toleration{}),
272+
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "dedicated", Value: "user1", Effect: "NoSchedule"}}),
273+
wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable,
274+
"node(s) had taint {dedicated: user1}, that the pod didn't tolerate"),
275275
},
276276
{
277277
name: "A pod which can be scheduled on a dedicated node assigned to user1 with effect NoSchedule",
278278
pod: podWithTolerations("pod1", []v1.Toleration{{Key: "dedicated", Value: "user1", Effect: "NoSchedule"}}),
279279
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "dedicated", Value: "user1", Effect: "NoSchedule"}}),
280280
},
281281
{
282-
name: "A pod which can't be scheduled on a dedicated node assigned to user2 with effect NoSchedule",
283-
pod: podWithTolerations("pod1", []v1.Toleration{{Key: "dedicated", Operator: "Equal", Value: "user2", Effect: "NoSchedule"}}),
284-
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "dedicated", Value: "user1", Effect: "NoSchedule"}}),
285-
wantStatus: unschedulable,
282+
name: "A pod which can't be scheduled on a dedicated node assigned to user2 with effect NoSchedule",
283+
pod: podWithTolerations("pod1", []v1.Toleration{{Key: "dedicated", Operator: "Equal", Value: "user2", Effect: "NoSchedule"}}),
284+
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "dedicated", Value: "user1", Effect: "NoSchedule"}}),
285+
wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable,
286+
"node(s) had taint {dedicated: user1}, that the pod didn't tolerate"),
286287
},
287288
{
288289
name: "A pod can be scheduled onto the node, with a toleration uses operator Exists that tolerates the taints on the node",
@@ -303,9 +304,10 @@ func TestTaintTolerationFilter(t *testing.T) {
303304
{
304305
name: "A pod has a toleration that keys and values match the taint on the node, but (non-empty) effect doesn't match, " +
305306
"can't be scheduled onto the node",
306-
pod: podWithTolerations("pod1", []v1.Toleration{{Key: "foo", Operator: "Equal", Value: "bar", Effect: "PreferNoSchedule"}}),
307-
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "foo", Value: "bar", Effect: "NoSchedule"}}),
308-
wantStatus: unschedulable,
307+
pod: podWithTolerations("pod1", []v1.Toleration{{Key: "foo", Operator: "Equal", Value: "bar", Effect: "PreferNoSchedule"}}),
308+
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "foo", Value: "bar", Effect: "NoSchedule"}}),
309+
wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable,
310+
"node(s) had taint {foo: bar}, that the pod didn't tolerate"),
309311
},
310312
{
311313
name: "The pod has a toleration that keys and values match the taint on the node, the effect of toleration is empty, " +
@@ -315,13 +317,13 @@ func TestTaintTolerationFilter(t *testing.T) {
315317
},
316318
{
317319
name: "The pod has a toleration that key and value don't match the taint on the node, " +
318-
"but the effect of taint on node is PreferNochedule. Pod can be scheduled onto the node",
320+
"but the effect of taint on node is PreferNoSchedule. Pod can be scheduled onto the node",
319321
pod: podWithTolerations("pod1", []v1.Toleration{{Key: "dedicated", Operator: "Equal", Value: "user2", Effect: "NoSchedule"}}),
320322
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "dedicated", Value: "user1", Effect: "PreferNoSchedule"}}),
321323
},
322324
{
323325
name: "The pod has no toleration, " +
324-
"but the effect of taint on node is PreferNochedule. Pod can be scheduled onto the node",
326+
"but the effect of taint on node is PreferNoSchedule. Pod can be scheduled onto the node",
325327
pod: podWithTolerations("pod1", []v1.Toleration{}),
326328
node: nodeWithTaints("nodeA", []v1.Taint{{Key: "dedicated", Value: "user1", Effect: "PreferNoSchedule"}}),
327329
},

0 commit comments

Comments
 (0)