-
Notifications
You must be signed in to change notification settings - Fork 48
/
predicates.go
77 lines (65 loc) · 1.95 KB
/
predicates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"log"
"math/rand"
"strings"
v1 "k8s.io/api/core/v1"
extender "k8s.io/kube-scheduler/extender/v1"
)
const (
// LuckyPred rejects a node if you're not lucky ¯\_(ツ)_/¯
LuckyPred = "Lucky"
LuckyPredFailMsg = "Well, you're not lucky"
)
var predicatesFuncs = map[string]FitPredicate{
LuckyPred: LuckyPredicate,
}
type FitPredicate func(pod *v1.Pod, node v1.Node) (bool, []string, error)
var predicatesSorted = []string{LuckyPred}
// filter filters nodes according to predicates defined in this extender
// it's webhooked to pkg/scheduler/core/generic_scheduler.go#findNodesThatFitPod()
func filter(args extender.ExtenderArgs) *extender.ExtenderFilterResult {
var filteredNodes []v1.Node
failedNodes := make(extender.FailedNodesMap)
pod := args.Pod
// TODO: parallelize this
// TODO: handle error
for _, node := range args.Nodes.Items {
fits, failReasons, _ := podFitsOnNode(pod, node)
if fits {
filteredNodes = append(filteredNodes, node)
} else {
failedNodes[node.Name] = strings.Join(failReasons, ",")
}
}
result := extender.ExtenderFilterResult{
Nodes: &v1.NodeList{
Items: filteredNodes,
},
FailedNodes: failedNodes,
Error: "",
}
return &result
}
func podFitsOnNode(pod *v1.Pod, node v1.Node) (bool, []string, error) {
fits := true
var failReasons []string
for _, predicateKey := range predicatesSorted {
fit, failures, err := predicatesFuncs[predicateKey](pod, node)
if err != nil {
return false, nil, err
}
fits = fits && fit
failReasons = append(failReasons, failures...)
}
return fits, failReasons, nil
}
func LuckyPredicate(pod *v1.Pod, node v1.Node) (bool, []string, error) {
lucky := rand.Intn(2) == 0
if lucky {
log.Printf("pod %v/%v is lucky to fit on node %v\n", pod.Name, pod.Namespace, node.Name)
return true, nil, nil
}
log.Printf("pod %v/%v is unlucky to fit on node %v\n", pod.Name, pod.Namespace, node.Name)
return false, []string{LuckyPredFailMsg}, nil
}