-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from redhatrises/tolerations_feature
feat(node): merge tolerations when injected
- Loading branch information
Showing
2 changed files
with
105 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package k8s_utils | ||
|
||
/* Code from kubernetes project for merging tolerations */ | ||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/equality" | ||
) | ||
|
||
func MergeTolerations(first, second []corev1.Toleration) []corev1.Toleration { | ||
all := append(first, second...) | ||
var merged []corev1.Toleration | ||
|
||
next: | ||
for i, t := range all { | ||
for _, t2 := range merged { | ||
if isSuperset(t2, t) { | ||
continue next // t is redundant; ignore it | ||
} | ||
} | ||
if i+1 < len(all) { | ||
for _, t2 := range all[i+1:] { | ||
// If the tolerations are equal, prefer the first. | ||
if !equality.Semantic.DeepEqual(&t, &t2) && isSuperset(t2, t) /* #nosec G601 */ { | ||
continue next // t is redundant; ignore it | ||
} | ||
} | ||
} | ||
merged = append(merged, t) | ||
} | ||
|
||
return merged | ||
} | ||
|
||
// isSuperset checks whether ss tolerates a superset of t. | ||
func isSuperset(ss, t corev1.Toleration) bool { | ||
if equality.Semantic.DeepEqual(&t, &ss) { | ||
return true | ||
} | ||
|
||
if t.Key != ss.Key && | ||
// An empty key with Exists operator means match all keys & values. | ||
(ss.Key != "" || ss.Operator != corev1.TolerationOpExists) { | ||
return false | ||
} | ||
|
||
// An empty effect means match all effects. | ||
if t.Effect != ss.Effect && ss.Effect != "" { | ||
return false | ||
} | ||
|
||
if ss.Effect == corev1.TaintEffectNoExecute { | ||
if ss.TolerationSeconds != nil { | ||
if t.TolerationSeconds == nil || | ||
*t.TolerationSeconds > *ss.TolerationSeconds { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
switch ss.Operator { | ||
case corev1.TolerationOpEqual, "": // empty operator means Equal | ||
return t.Operator == corev1.TolerationOpEqual && t.Value == ss.Value | ||
case corev1.TolerationOpExists: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
/* End code from kubernetes project for merging tolerations */ |