-
Notifications
You must be signed in to change notification settings - Fork 0
/
toleration.go-528d333813c8779497327020cce02d7222f95c79184f6974551e3fc719947730
100 lines (85 loc) · 3.01 KB
/
toleration.go-528d333813c8779497327020cce02d7222f95c79184f6974551e3fc719947730
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// PUBLISH-CODE
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package toleration
// This code is taken from github.com/kubernetes and modified.
import (
core "go.tectonic.network/lib/k8s/imp/api/core/v1"
)
// from api/core/v1/toleration.go
// 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.
func ToleratesTaint(t core.Toleration, taint *core.Taint) bool {
if len(t.Effect) > 0 && t.Effect != taint.Effect {
return false
}
if len(t.Key) > 0 && t.Key != taint.Key {
return false
}
switch t.Operator {
// empty operator means Equal
case "", core.TolerationOpEqual:
return t.Value == taint.Value
case core.TolerationOpExists:
return true
default:
return false
}
}
// from component-helpers/scheduling/corev1/helpers.go
// TolerationsTolerateTaint checks if taint is tolerated by any of the tolerations.
func TolerationsTolerateTaint(tolerations []core.Toleration, taint *core.Taint) bool {
for i := range tolerations {
if ToleratesTaint(tolerations[i], taint) {
return true
}
}
return false
}
type taintsFilterFunc func(*core.Taint) bool
// FindMatchingUntoleratedTaint checks if the given tolerations tolerates
// all the filtered taints, and returns the first taint without a toleration
// Returns true if there is an untolerated taint
// Returns false if all taints are tolerated
func FindMatchingUntoleratedTaint(taints []core.Taint, tolerations []core.Toleration, inclusionFilter taintsFilterFunc) (core.Taint, bool) {
filteredTaints := getFilteredTaints(taints, inclusionFilter)
for _, taint := range filteredTaints {
if !TolerationsTolerateTaint(tolerations, &taint) {
return taint, true
}
}
return core.Taint{}, false
}
// getFilteredTaints returns a list of taints satisfying the filter predicate
func getFilteredTaints(taints []core.Taint, inclusionFilter taintsFilterFunc) []core.Taint {
if inclusionFilter == nil {
return taints
}
filteredTaints := []core.Taint{}
for _, taint := range taints {
if !inclusionFilter(&taint) {
continue
}
filteredTaints = append(filteredTaints, taint)
}
return filteredTaints
}