-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
executable file
·135 lines (114 loc) · 2.87 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/comail/colog"
"github.com/julienschmidt/httprouter"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
)
const (
versionPath = "/version"
apiPrefix = "/scheduler"
bindPath = apiPrefix + "/bind"
preemptionPath = apiPrefix + "/preemption"
predicatesPrefix = apiPrefix + "/predicates"
prioritiesPrefix = apiPrefix + "/priorities"
defaultHTTPPort = "80"
keyHTTPPort = "HTTP_PORT"
)
var (
// injected via ldflags at build time
version string
// switch to initialize operation only once
initialized = false
// the http router
router *httprouter.Router
// NoBind :
NoBind = Bind{
Func: func(podName string, podNamespace string, podUID types.UID, node string) error {
return fmt.Errorf("Extender doesn't support Bind: make 'BindVerb' be empty in your ExtenderConfig")
},
}
// EchoPreemption :
EchoPreemption = Preemption{
Func: func(
_ v1.Pod,
_ map[string]*schedulerapi.Victims,
nodeNameToMetaVictims map[string]*schedulerapi.MetaVictims,
) map[string]*schedulerapi.MetaVictims {
return nodeNameToMetaVictims
},
}
)
func init() {
InitMain()
}
// InitMain : initialize main - should be done before any other init()
func InitMain() {
if !initialized {
// init logger
initLogger()
// init router
router = httprouter.New()
AddVersion(router)
initialized = true
}
}
// Initialize logger
func initLogger() {
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)
}
// StringToLevel : determine log level
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
}
}
// AddRouterPredicate : add a predicate to the router
func AddRouterPredicate(p Predicate) {
AddPredicate(router, p)
}
// AddRouterPriority : add a priority function to the router
func AddRouterPriority(p Prioritize) {
AddPrioritize(router, p)
}
func main() {
AddBind(router, NoBind)
// start http server
hostPort := defaultHTTPPort
if hp := os.Getenv(keyHTTPPort); len(hp) > 0 {
hostPort = hp
}
log.Printf("info: server starting on port %s \n", hostPort)
if err := http.ListenAndServe(":"+hostPort, router); err != nil {
log.Fatal(err)
}
}