forked from everpeace/k8s-scheduler-extender-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (104 loc) · 2.76 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/comail/colog"
"github.com/julienschmidt/httprouter"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
schedulerapi "k8s.io/kubernetes/pkg/scheduler/apis/extender/v1"
)
const (
versionPath = "/version"
apiPrefix = "/scheduler"
bindPath = apiPrefix + "/bind"
preemptionPath = apiPrefix + "/preemption"
predicatesPrefix = apiPrefix + "/predicates"
prioritiesPrefix = apiPrefix + "/priorities"
)
var (
version string // injected via ldflags at build time
TruePredicate = Predicate{
Name: "always_true",
Func: func(pod v1.Pod, node v1.Node) (bool, error) {
return true, nil
},
}
ZeroPriority = Prioritize{
Name: "zero_score",
Func: func(_ v1.Pod, nodes []v1.Node) (*schedulerapi.HostPriorityList, error) {
var priorityList schedulerapi.HostPriorityList
priorityList = make([]schedulerapi.HostPriority, len(nodes))
for i, node := range nodes {
priorityList[i] = schedulerapi.HostPriority{
Host: node.Name,
Score: 0,
}
}
return &priorityList, nil
},
}
NoBind = Bind{
Func: func(podName string, podNamespace string, podUID types.UID, node string) error {
return fmt.Errorf("This extender doesn't support Bind. Please make 'BindVerb' be empty in your ExtenderConfig.")
},
}
EchoPreemption = Preemption{
Func: func(
_ v1.Pod,
_ map[string]*schedulerapi.Victims,
nodeNameToMetaVictims map[string]*schedulerapi.MetaVictims,
) map[string]*schedulerapi.MetaVictims {
return nodeNameToMetaVictims
},
}
)
func StringToLevel(levelStr string) colog.Level {
switch level := strings.ToUpper(levelStr); level {
case "TRACE":
return colog.LTrace
case "DEBUG":
return colog.LDebug
case "INFO":
return colog.LInfo
case "WARNING":
return colog.LWarning
case "ERROR":
return colog.LError
case "ALERT":
return colog.LAlert
default:
log.Printf("warning: LOG_LEVEL=\"%s\" is empty or invalid, fallling back to \"INFO\".\n", level)
return colog.LInfo
}
}
func main() {
colog.SetDefaultLevel(colog.LInfo)
colog.SetMinLevel(colog.LInfo)
colog.SetFormatter(&colog.StdFormatter{
Colors: true,
Flag: log.Ldate | log.Ltime | log.Lshortfile,
})
colog.Register()
level := StringToLevel(os.Getenv("LOG_LEVEL"))
log.Print("Log level was set to ", strings.ToUpper(level.String()))
colog.SetMinLevel(level)
router := httprouter.New()
AddVersion(router)
predicates := []Predicate{TruePredicate}
for _, p := range predicates {
AddPredicate(router, p)
}
priorities := []Prioritize{ZeroPriority}
for _, p := range priorities {
AddPrioritize(router, p)
}
AddBind(router, NoBind)
log.Print("info: server starting on the port :80")
if err := http.ListenAndServe(":80", router); err != nil {
log.Fatal(err)
}
}