| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package corev1 |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
|
| 22 |
v1 "k8s.io/api/core/v1" |
| 23 |
"k8s.io/component-helpers/scheduling/corev1/nodeaffinity" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
func PodPriority(pod *v1.Pod) int32 { |
| 28 |
if pod.Spec.Priority != nil { |
| 29 |
return *pod.Spec.Priority |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
return 0 |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
func MatchNodeSelectorTerms( |
| 40 |
node *v1.Node, |
| 41 |
nodeSelector *v1.NodeSelector, |
| 42 |
) (bool, error) { |
| 43 |
if node == nil { |
| 44 |
return false, nil |
| 45 |
} |
| 46 |
return nodeaffinity.NewLazyErrorNodeSelector(nodeSelector).Match(node) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func GetAvoidPodsFromNodeAnnotations(annotations map[string]string) (v1.AvoidPods, error) { |
| 52 |
var avoidPods v1.AvoidPods |
| 53 |
if len(annotations) > 0 && annotations[v1.PreferAvoidPodsAnnotationKey] != "" { |
| 54 |
err := json.Unmarshal([]byte(annotations[v1.PreferAvoidPodsAnnotationKey]), &avoidPods) |
| 55 |
if err != nil { |
| 56 |
return avoidPods, err |
| 57 |
} |
| 58 |
} |
| 59 |
return avoidPods, nil |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func TolerationsTolerateTaint(tolerations []v1.Toleration, taint *v1.Taint) bool { |
| 64 |
for i := range tolerations { |
| 65 |
if tolerations[i].ToleratesTaint(taint) { |
| 66 |
return true |
| 67 |
} |
| 68 |
} |
| 69 |
return false |
| 70 |
} |
| 71 |
|
| 72 |
type taintsFilterFunc func(*v1.Taint) bool |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
func FindMatchingUntoleratedTaint(taints []v1.Taint, tolerations []v1.Toleration, inclusionFilter taintsFilterFunc) (v1.Taint, bool) { |
| 79 |
filteredTaints := getFilteredTaints(taints, inclusionFilter) |
| 80 |
for _, taint := range filteredTaints { |
| 81 |
if !TolerationsTolerateTaint(tolerations, &taint) { |
| 82 |
return taint, true |
| 83 |
} |
| 84 |
} |
| 85 |
return v1.Taint{}, false |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func getFilteredTaints(taints []v1.Taint, inclusionFilter taintsFilterFunc) []v1.Taint { |
| 90 |
if inclusionFilter == nil { |
| 91 |
return taints |
| 92 |
} |
| 93 |
filteredTaints := []v1.Taint{} |
| 94 |
for _, taint := range taints { |
| 95 |
if !inclusionFilter(&taint) { |
| 96 |
continue |
| 97 |
} |
| 98 |
filteredTaints = append(filteredTaints, taint) |
| 99 |
} |
| 100 |
return filteredTaints |
| 101 |
} |