tolerations.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 tolerations
  14. import (
  15. api "k8s.io/kubernetes/pkg/apis/core"
  16. )
  17. type key struct {
  18. tolerationKey string
  19. effect api.TaintEffect
  20. }
  21. func convertTolerationToKey(in api.Toleration) key {
  22. return key{in.Key, in.Effect}
  23. }
  24. // VerifyAgainstWhitelist checks if the provided tolerations
  25. // satisfy the provided whitelist and returns true, otherwise returns false
  26. func VerifyAgainstWhitelist(tolerations []api.Toleration, whitelist []api.Toleration) bool {
  27. if len(whitelist) == 0 {
  28. return true
  29. }
  30. t := ConvertTolerationToAMap(tolerations)
  31. w := ConvertTolerationToAMap(whitelist)
  32. for k1, v1 := range t {
  33. if v2, ok := w[k1]; !ok || !AreEqual(v1, v2) {
  34. return false
  35. }
  36. }
  37. return true
  38. }
  39. // IsConflict returns true if the key of two tolerations match
  40. // but one or more other fields differ, otherwise returns false
  41. func IsConflict(first []api.Toleration, second []api.Toleration) bool {
  42. firstMap := ConvertTolerationToAMap(first)
  43. secondMap := ConvertTolerationToAMap(second)
  44. for k1, v1 := range firstMap {
  45. if v2, ok := secondMap[k1]; ok && !AreEqual(v1, v2) {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. // MergeTolerations merges two sets of tolerations into one
  52. // it does not check for conflicts
  53. func MergeTolerations(first []api.Toleration, second []api.Toleration) []api.Toleration {
  54. var mergedTolerations []api.Toleration
  55. mergedTolerations = append(mergedTolerations, second...)
  56. firstMap := ConvertTolerationToAMap(first)
  57. secondMap := ConvertTolerationToAMap(second)
  58. // preserve order of first when merging
  59. for _, v := range first {
  60. k := convertTolerationToKey(v)
  61. // if first contains key conflicts, the last one takes precedence
  62. if _, ok := secondMap[k]; !ok && firstMap[k] == v {
  63. mergedTolerations = append(mergedTolerations, v)
  64. }
  65. }
  66. return mergedTolerations
  67. }
  68. // EqualTolerations returns true if two sets of tolerations are equal, otherwise false
  69. // it assumes no duplicates in individual set of tolerations
  70. func EqualTolerations(first []api.Toleration, second []api.Toleration) bool {
  71. if len(first) != len(second) {
  72. return false
  73. }
  74. firstMap := ConvertTolerationToAMap(first)
  75. secondMap := ConvertTolerationToAMap(second)
  76. for k1, v1 := range firstMap {
  77. if v2, ok := secondMap[k1]; !ok || !AreEqual(v1, v2) {
  78. return false
  79. }
  80. }
  81. return true
  82. }
  83. // ConvertTolerationToAMap converts toleration list into a map[string]api.Toleration
  84. func ConvertTolerationToAMap(in []api.Toleration) map[key]api.Toleration {
  85. out := map[key]api.Toleration{}
  86. for _, v := range in {
  87. out[convertTolerationToKey(v)] = v
  88. }
  89. return out
  90. }
  91. // AreEqual checks if two provided tolerations are equal or not.
  92. func AreEqual(first, second api.Toleration) bool {
  93. if first.Key == second.Key &&
  94. first.Operator == second.Operator &&
  95. first.Value == second.Value &&
  96. first.Effect == second.Effect &&
  97. AreTolerationSecondsEqual(first.TolerationSeconds, second.TolerationSeconds) {
  98. return true
  99. }
  100. return false
  101. }
  102. // AreTolerationSecondsEqual checks if two provided TolerationSeconds are equal or not.
  103. func AreTolerationSecondsEqual(ts1, ts2 *int64) bool {
  104. if ts1 == ts2 {
  105. return true
  106. }
  107. if ts1 != nil && ts2 != nil && *ts1 == *ts2 {
  108. return true
  109. }
  110. return false
  111. }