Debian Codegraph @

scheduling/corev1/helpers.go

raw symbols SCIP
1 /*
2 Copyright 2020 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 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 // PodPriority returns priority of the given pod.
27 func PodPriority(pod *v1.Pod) int32 {
28 if pod.Spec.Priority != nil {
29 return *pod.Spec.Priority
30 }
31 // When priority of a running pod is nil, it means it was created at a time
32 // that there was no global default priority class and the priority class
33 // name of the pod was empty. So, we resolve to the static default priority.
34 return 0
35 }
36
37 // MatchNodeSelectorTerms checks whether the node labels and fields match node selector terms in ORed;
38 // nil or empty term matches no objects.
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 // GetAvoidPodsFromNodeAnnotations scans the list of annotations and
50 // returns the pods that needs to be avoided for this node from scheduling
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 // TolerationsTolerateTaint checks if taint is tolerated by any of the tolerations.
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 // FindMatchingUntoleratedTaint checks if the given tolerations tolerates
75 // all the filtered taints, and returns the first taint without a toleration
76 // Returns true if there is an untolerated taint
77 // Returns false if all taints are tolerated
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 // getFilteredTaints returns a list of taints satisfying the filter predicate
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 }