blob: b203d335b6bef2797941d44170d7462e54e2ec16 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package 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.
22func (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.
37func (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}