mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-13 11:25:19 +00:00
forgiveness library changes
This commit is contained in:
@@ -20,7 +20,9 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/selection"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@@ -285,6 +287,10 @@ func GetTolerationsFromPodAnnotations(annotations map[string]string) ([]Tolerati
|
||||
return tolerations, nil
|
||||
}
|
||||
|
||||
func GetPodTolerations(pod *Pod) ([]Toleration, error) {
|
||||
return GetTolerationsFromPodAnnotations(pod.Annotations)
|
||||
}
|
||||
|
||||
// GetTaintsFromNodeAnnotations gets the json serialized taints data from Pod.Annotations
|
||||
// and converts it to the []Taint type in api.
|
||||
func GetTaintsFromNodeAnnotations(annotations map[string]string) ([]Taint, error) {
|
||||
@@ -298,35 +304,105 @@ func GetTaintsFromNodeAnnotations(annotations map[string]string) ([]Taint, error
|
||||
return taints, nil
|
||||
}
|
||||
|
||||
// TolerationToleratesTaint checks if the toleration tolerates the taint.
|
||||
func TolerationToleratesTaint(toleration *Toleration, taint *Taint) bool {
|
||||
if len(toleration.Effect) != 0 && toleration.Effect != taint.Effect {
|
||||
func GetNodeTaints(node *Node) ([]Taint, error) {
|
||||
return GetTaintsFromNodeAnnotations(node.Annotations)
|
||||
}
|
||||
|
||||
// ToleratesTaint checks if the toleration tolerates the taint.
|
||||
func (t *Toleration) ToleratesTaint(taint *Taint) bool {
|
||||
// empty toleration effect means match all taint effects
|
||||
if len(t.Effect) > 0 && t.Effect != taint.Effect {
|
||||
return false
|
||||
}
|
||||
|
||||
if toleration.Key != taint.Key {
|
||||
// empty toleration key means match all taint keys
|
||||
if len(t.Key) > 0 && t.Key != taint.Key {
|
||||
return false
|
||||
}
|
||||
// TODO: Use proper defaulting when Toleration becomes a field of PodSpec
|
||||
if (len(toleration.Operator) == 0 || toleration.Operator == TolerationOpEqual) && toleration.Value == taint.Value {
|
||||
return true
|
||||
|
||||
// nil TolerationSeconds means tolerate the taint forever
|
||||
if t.TolerationSeconds != nil {
|
||||
// taint with no added time indicated can only be tolerated
|
||||
// by toleration with no tolerationSeconds.
|
||||
if taint.TimeAdded.IsZero() {
|
||||
return false
|
||||
}
|
||||
|
||||
// TODO: need to take time skew into consideration, make sure toleration won't become out of age ealier than expected.
|
||||
if metav1.Now().After(taint.TimeAdded.Add(time.Second * time.Duration(*t.TolerationSeconds))) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if toleration.Operator == TolerationOpExists {
|
||||
|
||||
// TODO: Use proper defaulting when Toleration becomes a field of PodSpec
|
||||
switch t.Operator {
|
||||
// empty operator means Equal
|
||||
case "", TolerationOpEqual:
|
||||
return t.Value == taint.Value
|
||||
case TolerationOpExists:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// TolerationsTolerateTaint checks if taint is tolerated by any of the tolerations.
|
||||
func TolerationsTolerateTaint(tolerations []Toleration, taint *Taint) bool {
|
||||
for i := range tolerations {
|
||||
if tolerations[i].ToleratesTaint(taint) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TaintToleratedByTolerations checks if taint is tolerated by any of the tolerations.
|
||||
func TaintToleratedByTolerations(taint *Taint, tolerations []Toleration) bool {
|
||||
tolerated := false
|
||||
for i := range tolerations {
|
||||
if TolerationToleratesTaint(&tolerations[i], taint) {
|
||||
tolerated = true
|
||||
break
|
||||
type taintsFilterFunc func(*Taint) bool
|
||||
|
||||
// TolerationsTolerateTaintsWithFilter checks if given tolerations tolerates
|
||||
// all the interesting taints in given taint list.
|
||||
func TolerationsTolerateTaintsWithFilter(tolerations []Toleration, taints []Taint, isInterestingTaint taintsFilterFunc) bool {
|
||||
if len(taints) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
for i := range taints {
|
||||
if isInterestingTaint != nil && !isInterestingTaint(&taints[i]) {
|
||||
continue
|
||||
}
|
||||
|
||||
if !TolerationsTolerateTaint(tolerations, &taints[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return tolerated
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// DeleteTaintsByKey removes all the taints that have the same key to given taintKey
|
||||
func DeleteTaintsByKey(taints []Taint, taintKey string) ([]Taint, bool) {
|
||||
newTaints := []Taint{}
|
||||
deleted := false
|
||||
for i := range taints {
|
||||
if taintKey == taints[i].Key {
|
||||
deleted = true
|
||||
continue
|
||||
}
|
||||
newTaints = append(newTaints, taints[i])
|
||||
}
|
||||
return newTaints, deleted
|
||||
}
|
||||
|
||||
func DeleteTaint(taints []Taint, taintToDelete *Taint) ([]Taint, bool) {
|
||||
newTaints := []Taint{}
|
||||
deleted := false
|
||||
for i := range taints {
|
||||
if taintToDelete.MatchTaint(taints[i]) {
|
||||
deleted = true
|
||||
continue
|
||||
}
|
||||
newTaints = append(newTaints, taints[i])
|
||||
}
|
||||
return newTaints, deleted
|
||||
}
|
||||
|
||||
// MatchTaint checks if the taint matches taintToMatch. Taints are unique by key:effect,
|
||||
|
||||
@@ -280,6 +280,289 @@ func TestMatchTaint(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTolerationToleratesTaint(t *testing.T) {
|
||||
genTolerationSeconds := func(f int64) *int64 {
|
||||
return &f
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
description string
|
||||
toleration Toleration
|
||||
taint Taint
|
||||
expectTolerated bool
|
||||
}{
|
||||
{
|
||||
description: "toleration and taint have the same key and effect, and operator is Exists, and taint has no value, expect tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpExists,
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "toleration and taint have the same key and effect, and operator is Exists, and taint has some value, expect tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpExists,
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Value: "bar",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "toleration and taint have the same effect, toleration has empty key and operator is Exists, means match all taints, expect tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "",
|
||||
Operator: TolerationOpExists,
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Value: "bar",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "toleration and taint have the same key, effect and value, and operator is Equal, expect tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpEqual,
|
||||
Value: "bar",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Value: "bar",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "toleration and taint have the same key and effect, but different values, and operator is Equal, expect not tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpEqual,
|
||||
Value: "value1",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Value: "value2",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
expectTolerated: false,
|
||||
},
|
||||
{
|
||||
description: "toleration and taint have the same key and value, but different effects, and operator is Equal, expect not tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpEqual,
|
||||
Value: "bar",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Value: "bar",
|
||||
Effect: TaintEffectNoExecute,
|
||||
},
|
||||
expectTolerated: false,
|
||||
},
|
||||
{
|
||||
description: "expect toleration with nil tolerationSeconds tolerates taint that is newly added",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpExists,
|
||||
Effect: TaintEffectNoExecute,
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Effect: TaintEffectNoExecute,
|
||||
TimeAdded: metav1.Now(),
|
||||
},
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "forgiveness toleration has not timed out, expect tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpExists,
|
||||
Effect: TaintEffectNoExecute,
|
||||
TolerationSeconds: genTolerationSeconds(300),
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Effect: TaintEffectNoExecute,
|
||||
TimeAdded: metav1.Unix(metav1.Now().Unix()-100, 0),
|
||||
},
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "forgiveness toleration has timed out, expect not tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpExists,
|
||||
Effect: TaintEffectNoExecute,
|
||||
TolerationSeconds: genTolerationSeconds(300),
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Effect: TaintEffectNoExecute,
|
||||
TimeAdded: metav1.Unix(metav1.Now().Unix()-1000, 0),
|
||||
},
|
||||
expectTolerated: false,
|
||||
},
|
||||
{
|
||||
description: "toleration with explicit forgiveness can't tolerate taint with no added time, expect not tolerated",
|
||||
toleration: Toleration{
|
||||
Key: "foo",
|
||||
Operator: TolerationOpExists,
|
||||
Effect: TaintEffectNoExecute,
|
||||
TolerationSeconds: genTolerationSeconds(300),
|
||||
},
|
||||
taint: Taint{
|
||||
Key: "foo",
|
||||
Effect: TaintEffectNoExecute,
|
||||
},
|
||||
expectTolerated: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
if tolerated := tc.toleration.ToleratesTaint(&tc.taint); tc.expectTolerated != tolerated {
|
||||
t.Errorf("[%s] expect %v, got %v: toleration %+v, taint %s", tc.description, tc.expectTolerated, tolerated, tc.toleration, tc.taint.ToString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTolerationsTolerateTaintsWithFilter(t *testing.T) {
|
||||
testCases := []struct {
|
||||
description string
|
||||
tolerations []Toleration
|
||||
taints []Taint
|
||||
isInterestingTaint taintsFilterFunc
|
||||
expectTolerated bool
|
||||
}{
|
||||
{
|
||||
description: "empty tolerations tolerate empty taints",
|
||||
tolerations: []Toleration{},
|
||||
taints: []Taint{},
|
||||
isInterestingTaint: func(t *Taint) bool { return true },
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "non-empty tolerations tolerate empty taints",
|
||||
tolerations: []Toleration{
|
||||
{
|
||||
Key: "foo",
|
||||
Operator: "Exists",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
taints: []Taint{},
|
||||
isInterestingTaint: func(t *Taint) bool { return true },
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "tolerations match all taints, expect tolerated",
|
||||
tolerations: []Toleration{
|
||||
{
|
||||
Key: "foo",
|
||||
Operator: "Exists",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
taints: []Taint{
|
||||
{
|
||||
Key: "foo",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
isInterestingTaint: func(t *Taint) bool { return true },
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "tolerations don't match taints, but no taint is interested, expect tolerated",
|
||||
tolerations: []Toleration{
|
||||
{
|
||||
Key: "foo",
|
||||
Operator: "Exists",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
taints: []Taint{
|
||||
{
|
||||
Key: "bar",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
isInterestingTaint: func(t *Taint) bool { return false },
|
||||
expectTolerated: true,
|
||||
},
|
||||
{
|
||||
description: "no isInterestedTaint indicated, means all taints are interested, tolerations don't match taints, expect untolerated",
|
||||
tolerations: []Toleration{
|
||||
{
|
||||
Key: "foo",
|
||||
Operator: "Exists",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
taints: []Taint{
|
||||
{
|
||||
Key: "bar",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
isInterestingTaint: nil,
|
||||
expectTolerated: false,
|
||||
},
|
||||
{
|
||||
description: "tolerations match interested taints, expect tolerated",
|
||||
tolerations: []Toleration{
|
||||
{
|
||||
Key: "foo",
|
||||
Operator: "Exists",
|
||||
Effect: TaintEffectNoExecute,
|
||||
},
|
||||
},
|
||||
taints: []Taint{
|
||||
{
|
||||
Key: "foo",
|
||||
Effect: TaintEffectNoExecute,
|
||||
},
|
||||
{
|
||||
Key: "bar",
|
||||
Effect: TaintEffectNoSchedule,
|
||||
},
|
||||
},
|
||||
isInterestingTaint: func(t *Taint) bool { return t.Effect == TaintEffectNoExecute },
|
||||
expectTolerated: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
if tc.expectTolerated != TolerationsTolerateTaintsWithFilter(tc.tolerations, tc.taints, tc.isInterestingTaint) {
|
||||
filteredTaints := []Taint{}
|
||||
for _, taint := range tc.taints {
|
||||
if tc.isInterestingTaint != nil && !tc.isInterestingTaint(&taint) {
|
||||
continue
|
||||
}
|
||||
filteredTaints = append(filteredTaints, taint)
|
||||
}
|
||||
t.Errorf("[%s] expect tolerations %+v tolerate filtered taints %+v in taints %+v", tc.description, tc.tolerations, filteredTaints, tc.taints)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAvoidPodsFromNode(t *testing.T) {
|
||||
controllerFlag := true
|
||||
testCases := []struct {
|
||||
|
||||
Reference in New Issue
Block a user