Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package v1 |
| 18 | |
| 19 | // MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>, |
| 20 | // if the two tolerations have same <key,effect,operator,value> combination, regard as they match. |
| 21 | // TODO: uniqueness check for tolerations in api validations. |
| 22 | func (t *Toleration) MatchToleration(tolerationToMatch *Toleration) bool { |
| 23 | return t.Key == tolerationToMatch.Key && |
| 24 | t.Effect == tolerationToMatch.Effect && |
| 25 | t.Operator == tolerationToMatch.Operator && |
| 26 | t.Value == tolerationToMatch.Value |
| 27 | } |
| 28 | |
| 29 | // ToleratesTaint checks if the toleration tolerates the taint. |
| 30 | // The matching follows the rules below: |
| 31 | // (1) Empty toleration.effect means to match all taint effects, |
| 32 | // otherwise taint effect must equal to toleration.effect. |
| 33 | // (2) If toleration.operator is 'Exists', it means to match all taint values. |
| 34 | // (3) Empty toleration.key means to match all taint keys. |
| 35 | // If toleration.key is empty, toleration.operator must be 'Exists'; |
| 36 | // this combination means to match all taint values and all taint keys. |
| 37 | func (t *Toleration) ToleratesTaint(taint *Taint) bool { |
| 38 | if len(t.Effect) > 0 && t.Effect != taint.Effect { |
| 39 | return false |
| 40 | } |
| 41 | |
| 42 | if len(t.Key) > 0 && t.Key != taint.Key { |
| 43 | return false |
| 44 | } |
| 45 | |
| 46 | // TODO: Use proper defaulting when Toleration becomes a field of PodSpec |
| 47 | switch t.Operator { |
| 48 | // empty operator means Equal |
| 49 | case "", TolerationOpEqual: |
| 50 | return t.Value == taint.Value |
| 51 | case TolerationOpExists: |
| 52 | return true |
| 53 | default: |
| 54 | return false |
| 55 | } |
| 56 | } |