address review comments
This commit is contained in:
parent
36dcb57407
commit
6a2ef1646b
@ -309,20 +309,28 @@ func GetNodeTaints(node *Node) ([]Taint, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToleratesTaint checks if the toleration tolerates the taint.
|
// ToleratesTaint checks if the toleration tolerates the taint.
|
||||||
|
// The matching follows the rules below:
|
||||||
|
// (1) Empty toleration.effect means to match all taint effects,
|
||||||
|
// otherwise taint effect must equal to toleration.effect.
|
||||||
|
// (2) If toleration.operator is 'Exists', it means to match all taint values.
|
||||||
|
// (3) Empty toleration.key means to match all taint keys.
|
||||||
|
// If toleration.key is empty, toleration.operator must be 'Exists';
|
||||||
|
// this combination means to match all taint values and all taint keys.
|
||||||
|
// (4) Nil toleration.tolerationSeconds means to match taints with
|
||||||
|
// any value of taint.timeAdded.
|
||||||
|
// (5) Non-nil positive toleration.tolerationSeconds means to
|
||||||
|
// match the taint for only a duration that starts at taint.timeAdded.
|
||||||
func (t *Toleration) ToleratesTaint(taint *Taint) bool {
|
func (t *Toleration) ToleratesTaint(taint *Taint) bool {
|
||||||
// empty toleration effect means match all taint effects
|
|
||||||
if len(t.Effect) > 0 && t.Effect != taint.Effect {
|
if len(t.Effect) > 0 && t.Effect != taint.Effect {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// empty toleration key means match all taint keys
|
|
||||||
if len(t.Key) > 0 && t.Key != taint.Key {
|
if len(t.Key) > 0 && t.Key != taint.Key {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// nil TolerationSeconds means tolerate the taint forever
|
|
||||||
if t.TolerationSeconds != nil {
|
if t.TolerationSeconds != nil {
|
||||||
// taint with no added time indicated can only be tolerated
|
// taint with no timeAdded indicated can only be tolerated
|
||||||
// by toleration with no tolerationSeconds.
|
// by toleration with no tolerationSeconds.
|
||||||
if taint.TimeAdded.IsZero() {
|
if taint.TimeAdded.IsZero() {
|
||||||
return false
|
return false
|
||||||
@ -359,14 +367,14 @@ func TolerationsTolerateTaint(tolerations []Toleration, taint *Taint) bool {
|
|||||||
type taintsFilterFunc func(*Taint) bool
|
type taintsFilterFunc func(*Taint) bool
|
||||||
|
|
||||||
// TolerationsTolerateTaintsWithFilter checks if given tolerations tolerates
|
// TolerationsTolerateTaintsWithFilter checks if given tolerations tolerates
|
||||||
// all the interesting taints in given taint list.
|
// all the taints that apply to the filter in given taint list.
|
||||||
func TolerationsTolerateTaintsWithFilter(tolerations []Toleration, taints []Taint, isInterestingTaint taintsFilterFunc) bool {
|
func TolerationsTolerateTaintsWithFilter(tolerations []Toleration, taints []Taint, applyFilter taintsFilterFunc) bool {
|
||||||
if len(taints) == 0 {
|
if len(taints) == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range taints {
|
for i := range taints {
|
||||||
if isInterestingTaint != nil && !isInterestingTaint(&taints[i]) {
|
if applyFilter != nil && !applyFilter(&taints[i]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -445,18 +445,18 @@ func TestTolerationToleratesTaint(t *testing.T) {
|
|||||||
|
|
||||||
func TestTolerationsTolerateTaintsWithFilter(t *testing.T) {
|
func TestTolerationsTolerateTaintsWithFilter(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
description string
|
description string
|
||||||
tolerations []Toleration
|
tolerations []Toleration
|
||||||
taints []Taint
|
taints []Taint
|
||||||
isInterestingTaint taintsFilterFunc
|
applyFilter taintsFilterFunc
|
||||||
expectTolerated bool
|
expectTolerated bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
description: "empty tolerations tolerate empty taints",
|
description: "empty tolerations tolerate empty taints",
|
||||||
tolerations: []Toleration{},
|
tolerations: []Toleration{},
|
||||||
taints: []Taint{},
|
taints: []Taint{},
|
||||||
isInterestingTaint: func(t *Taint) bool { return true },
|
applyFilter: func(t *Taint) bool { return true },
|
||||||
expectTolerated: true,
|
expectTolerated: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "non-empty tolerations tolerate empty taints",
|
description: "non-empty tolerations tolerate empty taints",
|
||||||
@ -467,9 +467,9 @@ func TestTolerationsTolerateTaintsWithFilter(t *testing.T) {
|
|||||||
Effect: TaintEffectNoSchedule,
|
Effect: TaintEffectNoSchedule,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
taints: []Taint{},
|
taints: []Taint{},
|
||||||
isInterestingTaint: func(t *Taint) bool { return true },
|
applyFilter: func(t *Taint) bool { return true },
|
||||||
expectTolerated: true,
|
expectTolerated: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "tolerations match all taints, expect tolerated",
|
description: "tolerations match all taints, expect tolerated",
|
||||||
@ -486,11 +486,11 @@ func TestTolerationsTolerateTaintsWithFilter(t *testing.T) {
|
|||||||
Effect: TaintEffectNoSchedule,
|
Effect: TaintEffectNoSchedule,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
isInterestingTaint: func(t *Taint) bool { return true },
|
applyFilter: func(t *Taint) bool { return true },
|
||||||
expectTolerated: true,
|
expectTolerated: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "tolerations don't match taints, but no taint is interested, expect tolerated",
|
description: "tolerations don't match taints, but no taints apply to the filter, expect tolerated",
|
||||||
tolerations: []Toleration{
|
tolerations: []Toleration{
|
||||||
{
|
{
|
||||||
Key: "foo",
|
Key: "foo",
|
||||||
@ -504,11 +504,11 @@ func TestTolerationsTolerateTaintsWithFilter(t *testing.T) {
|
|||||||
Effect: TaintEffectNoSchedule,
|
Effect: TaintEffectNoSchedule,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
isInterestingTaint: func(t *Taint) bool { return false },
|
applyFilter: func(t *Taint) bool { return false },
|
||||||
expectTolerated: true,
|
expectTolerated: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "no isInterestedTaint indicated, means all taints are interested, tolerations don't match taints, expect untolerated",
|
description: "no filterFunc indicated, means all taints apply to the filter, tolerations don't match taints, expect untolerated",
|
||||||
tolerations: []Toleration{
|
tolerations: []Toleration{
|
||||||
{
|
{
|
||||||
Key: "foo",
|
Key: "foo",
|
||||||
@ -522,11 +522,11 @@ func TestTolerationsTolerateTaintsWithFilter(t *testing.T) {
|
|||||||
Effect: TaintEffectNoSchedule,
|
Effect: TaintEffectNoSchedule,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
isInterestingTaint: nil,
|
applyFilter: nil,
|
||||||
expectTolerated: false,
|
expectTolerated: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "tolerations match interested taints, expect tolerated",
|
description: "tolerations match taints, expect tolerated",
|
||||||
tolerations: []Toleration{
|
tolerations: []Toleration{
|
||||||
{
|
{
|
||||||
Key: "foo",
|
Key: "foo",
|
||||||
@ -544,16 +544,16 @@ func TestTolerationsTolerateTaintsWithFilter(t *testing.T) {
|
|||||||
Effect: TaintEffectNoSchedule,
|
Effect: TaintEffectNoSchedule,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
isInterestingTaint: func(t *Taint) bool { return t.Effect == TaintEffectNoExecute },
|
applyFilter: func(t *Taint) bool { return t.Effect == TaintEffectNoExecute },
|
||||||
expectTolerated: true,
|
expectTolerated: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
if tc.expectTolerated != TolerationsTolerateTaintsWithFilter(tc.tolerations, tc.taints, tc.isInterestingTaint) {
|
if tc.expectTolerated != TolerationsTolerateTaintsWithFilter(tc.tolerations, tc.taints, tc.applyFilter) {
|
||||||
filteredTaints := []Taint{}
|
filteredTaints := []Taint{}
|
||||||
for _, taint := range tc.taints {
|
for _, taint := range tc.taints {
|
||||||
if tc.isInterestingTaint != nil && !tc.isInterestingTaint(&taint) {
|
if tc.applyFilter != nil && !tc.applyFilter(&taint) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
filteredTaints = append(filteredTaints, taint)
|
filteredTaints = append(filteredTaints, taint)
|
||||||
|
Loading…
Reference in New Issue
Block a user