toleration.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v1
  14. import (
  15. "errors"
  16. "strconv"
  17. "strings"
  18. "k8s.io/apimachinery/pkg/api/validate/content"
  19. "k8s.io/klog/v2"
  20. )
  21. // MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>,
  22. // if the two tolerations have same <key,effect,operator,value> combination, regard as they match.
  23. // TODO: uniqueness check for tolerations in api validations.
  24. func (t *Toleration) MatchToleration(tolerationToMatch *Toleration) bool {
  25. return t.Key == tolerationToMatch.Key &&
  26. t.Effect == tolerationToMatch.Effect &&
  27. t.Operator == tolerationToMatch.Operator &&
  28. t.Value == tolerationToMatch.Value
  29. }
  30. // ToleratesTaint checks if the toleration tolerates the taint.
  31. // The matching follows the rules below:
  32. //
  33. // 1. Empty toleration.effect means to match all taint effects,
  34. // otherwise taint effect must equal to toleration.effect.
  35. // 2. If toleration.operator is 'Exists', it means to match all taint values.
  36. // 3. Empty toleration.key means to match all taint keys.
  37. // If toleration.key is empty, toleration.operator must be 'Exists';
  38. // this combination means to match all taint values and all taint keys.
  39. // 4. If toleration.operator is 'Lt' or 'Gt', numeric comparison is performed
  40. // between toleration.value and taint.value.
  41. // 5. If enableComparisonOperators is false and the toleration uses 'Lt' or 'Gt'
  42. // operators, the toleration does not match (returns false).
  43. func (t *Toleration) ToleratesTaint(logger klog.Logger, taint *Taint, enableComparisonOperators bool) bool {
  44. if len(t.Effect) > 0 && t.Effect != taint.Effect {
  45. return false
  46. }
  47. if len(t.Key) > 0 && t.Key != taint.Key {
  48. return false
  49. }
  50. // TODO: Use proper defaulting when Toleration becomes a field of PodSpec
  51. switch t.Operator {
  52. // empty operator means Equal
  53. case "", TolerationOpEqual:
  54. return t.Value == taint.Value
  55. case TolerationOpExists:
  56. return true
  57. case TolerationOpLt, TolerationOpGt:
  58. // If comparison operators are disabled, this toleration doesn't match
  59. if !enableComparisonOperators {
  60. return false
  61. }
  62. return compareNumericValues(logger, t.Value, taint.Value, t.Operator)
  63. default:
  64. return false
  65. }
  66. }
  67. // compareNumericValues performs numeric comparison between toleration and taint values
  68. func compareNumericValues(logger klog.Logger, tolerationVal, taintVal string, op TolerationOperator) bool {
  69. errorMsgs := content.IsDecimalInteger(tolerationVal)
  70. if len(errorMsgs) > 0 {
  71. logger.Error(errors.New(strings.Join(errorMsgs, ",")), "failed to parse toleration value as int64", "toleration", tolerationVal)
  72. return false
  73. }
  74. tVal, err := strconv.ParseInt(tolerationVal, 10, 64)
  75. if err != nil {
  76. logger.Error(err, "failed to parse toleration value as int64", "toleration", tolerationVal)
  77. return false
  78. }
  79. errorMsgs = content.IsDecimalInteger(taintVal)
  80. if len(errorMsgs) > 0 {
  81. logger.Error(errors.New(strings.Join(errorMsgs, ",")), "failed to parse taint value as int64", "taint", taintVal)
  82. return false
  83. }
  84. tntVal, err := strconv.ParseInt(taintVal, 10, 64)
  85. if err != nil {
  86. logger.Error(err, "failed to parse taint value as int64", "taint", taintVal)
  87. return false
  88. }
  89. switch op {
  90. case TolerationOpLt:
  91. return tntVal < tVal
  92. case TolerationOpGt:
  93. return tntVal > tVal
  94. default:
  95. return false
  96. }
  97. }