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 // 32 // 1. Empty toleration.effect means to match all taint effects, 33 // otherwise taint effect must equal to toleration.effect. 34 // 2. If toleration.operator is 'Exists', it means to match all taint values. 35 // 3. Empty toleration.key means to match all taint keys. 36 // If toleration.key is empty, toleration.operator must be 'Exists'; 37 // this combination means to match all taint values and all taint keys. 38 func (t *Toleration) ToleratesTaint(taint *Taint) bool { 39 if len(t.Effect) > 0 && t.Effect != taint.Effect { 40 return false 41 } 42 43 if len(t.Key) > 0 && t.Key != taint.Key { 44 return false 45 } 46 47 // TODO: Use proper defaulting when Toleration becomes a field of PodSpec 48 switch t.Operator { 49 // empty operator means Equal 50 case "", TolerationOpEqual: 51 return t.Value == taint.Value 52 case TolerationOpExists: 53 return true 54 default: 55 return false 56 } 57 } 58