Skip to content

Commit 16a1b56

Browse files
helayotyk8s-publishing-bot
authored andcommitted
KEP-5471: Extend tolerations operators (#134665)
* Add numeric operations to tolerations Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * code review feedback Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * add default feature gate Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * Add integration tests Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * Add toleration value validation Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * Add validate options for new operators Signed-off-by: helayoty <heelayot@microsoft.com> * Remove log Signed-off-by: helayoty <heelayot@microsoft.com> * Update feature gate check Signed-off-by: helayoty <heelayot@microsoft.com> * emove IsValidNumericString func Signed-off-by: helayoty <heelayot@microsoft.com> * Implement IsDecimalInteger Signed-off-by: helayoty <heelayot@microsoft.com> * code review feedback Signed-off-by: helayoty <heelayot@microsoft.com> * Add logs to v1/toleration Signed-off-by: Heba Elayoty <heelayot@microsoft.com> Signed-off-by: helayoty <heelayot@microsoft.com> * Update integration tests and address code review feedback Signed-off-by: helayoty <heelayot@microsoft.com> * Add feature gate to the scheduler framework Signed-off-by: helayoty <heelayot@microsoft.com> * Remove extra test Signed-off-by: helayoty <heelayot@microsoft.com> * Fix integration test Signed-off-by: helayoty <heelayot@microsoft.com> * pass feature gate via TolerationsTolerateTaint Signed-off-by: helayoty <heelayot@microsoft.com> --------- Signed-off-by: Heba Elayoty <heelayot@microsoft.com> Signed-off-by: helayoty <heelayot@microsoft.com> Kubernetes-commit: aceb89debc2632c5c9956c8b7ef591426a485447
1 parent f173724 commit 16a1b56

File tree

7 files changed

+452
-14
lines changed

7 files changed

+452
-14
lines changed

core/v1/generated.proto

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/v1/toleration.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ limitations under the License.
1616

1717
package v1
1818

19+
import (
20+
"errors"
21+
"strconv"
22+
"strings"
23+
24+
"k8s.io/apimachinery/pkg/api/validate/content"
25+
26+
"k8s.io/klog/v2"
27+
)
28+
1929
// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>,
2030
// if the two tolerations have same <key,effect,operator,value> combination, regard as they match.
2131
// TODO: uniqueness check for tolerations in api validations.
@@ -35,7 +45,11 @@ func (t *Toleration) MatchToleration(tolerationToMatch *Toleration) bool {
3545
// 3. Empty toleration.key means to match all taint keys.
3646
// If toleration.key is empty, toleration.operator must be 'Exists';
3747
// this combination means to match all taint values and all taint keys.
38-
func (t *Toleration) ToleratesTaint(taint *Taint) bool {
48+
// 4. If toleration.operator is 'Lt' or 'Gt', numeric comparison is performed
49+
// between toleration.value and taint.value.
50+
// 5. If enableComparisonOperators is false and the toleration uses 'Lt' or 'Gt'
51+
// operators, the toleration does not match (returns false).
52+
func (t *Toleration) ToleratesTaint(logger klog.Logger, taint *Taint, enableComparisonOperators bool) bool {
3953
if len(t.Effect) > 0 && t.Effect != taint.Effect {
4054
return false
4155
}
@@ -51,6 +65,47 @@ func (t *Toleration) ToleratesTaint(taint *Taint) bool {
5165
return t.Value == taint.Value
5266
case TolerationOpExists:
5367
return true
68+
case TolerationOpLt, TolerationOpGt:
69+
// If comparison operators are disabled, this toleration doesn't match
70+
if !enableComparisonOperators {
71+
return false
72+
}
73+
return compareNumericValues(logger, t.Value, taint.Value, t.Operator)
74+
default:
75+
return false
76+
}
77+
}
78+
79+
// compareNumericValues performs numeric comparison between toleration and taint values
80+
func compareNumericValues(logger klog.Logger, tolerationVal, taintVal string, op TolerationOperator) bool {
81+
82+
errorMsgs := content.IsDecimalInteger(tolerationVal)
83+
if len(errorMsgs) > 0 {
84+
logger.Error(errors.New(strings.Join(errorMsgs, ",")), "failed to parse toleration value as int64", "toleration", tolerationVal)
85+
return false
86+
}
87+
tVal, err := strconv.ParseInt(tolerationVal, 10, 64)
88+
if err != nil {
89+
logger.Error(err, "failed to parse toleration value as int64", "toleration", tolerationVal)
90+
return false
91+
}
92+
93+
errorMsgs = content.IsDecimalInteger(taintVal)
94+
if len(errorMsgs) > 0 {
95+
logger.Error(errors.New(strings.Join(errorMsgs, ",")), "failed to parse taint value as int64", "taint", taintVal)
96+
return false
97+
}
98+
tntVal, err := strconv.ParseInt(taintVal, 10, 64)
99+
if err != nil {
100+
logger.Error(err, "failed to parse taint value as int64", "taint", taintVal)
101+
return false
102+
}
103+
104+
switch op {
105+
case TolerationOpLt:
106+
return tntVal < tVal
107+
case TolerationOpGt:
108+
return tntVal > tVal
54109
default:
55110
return false
56111
}

0 commit comments

Comments
 (0)