toleration.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>,
  15. // if the two tolerations have same <key,effect,operator,value> combination, regard as they match.
  16. // TODO: uniqueness check for tolerations in api validations.
  17. func (t *Toleration) MatchToleration(tolerationToMatch *Toleration) bool {
  18. return t.Key == tolerationToMatch.Key &&
  19. t.Effect == tolerationToMatch.Effect &&
  20. t.Operator == tolerationToMatch.Operator &&
  21. t.Value == tolerationToMatch.Value
  22. }
  23. // ToleratesTaint checks if the toleration tolerates the taint.
  24. // The matching follows the rules below:
  25. // (1) Empty toleration.effect means to match all taint effects,
  26. // otherwise taint effect must equal to toleration.effect.
  27. // (2) If toleration.operator is 'Exists', it means to match all taint values.
  28. // (3) Empty toleration.key means to match all taint keys.
  29. // If toleration.key is empty, toleration.operator must be 'Exists';
  30. // this combination means to match all taint values and all taint keys.
  31. func (t *Toleration) ToleratesTaint(taint *Taint) bool {
  32. if len(t.Effect) > 0 && t.Effect != taint.Effect {
  33. return false
  34. }
  35. if len(t.Key) > 0 && t.Key != taint.Key {
  36. return false
  37. }
  38. // TODO: Use proper defaulting when Toleration becomes a field of PodSpec
  39. switch t.Operator {
  40. // empty operator means Equal
  41. case "", TolerationOpEqual:
  42. return t.Value == taint.Value
  43. case TolerationOpExists:
  44. return true
  45. default:
  46. return false
  47. }
  48. }