forked from everpeace/k8s-scheduler-extender-example
-
Notifications
You must be signed in to change notification settings - Fork 15
/
routes.go
186 lines (154 loc) · 5.42 KB
/
routes.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
)
func checkBody(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
}
func PredicateRoute(predicate Predicate) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
checkBody(w, r)
var buf bytes.Buffer
body := io.TeeReader(r.Body, &buf)
log.Print("info: ", predicate.Name, " ExtenderArgs = ", buf.String())
var extenderArgs schedulerapi.ExtenderArgs
var extenderFilterResult *schedulerapi.ExtenderFilterResult
if err := json.NewDecoder(body).Decode(&extenderArgs); err != nil {
extenderFilterResult = &schedulerapi.ExtenderFilterResult{
Nodes: nil,
FailedNodes: nil,
Error: err.Error(),
}
} else {
extenderFilterResult = predicate.Handler(extenderArgs)
}
if resultBody, err := json.Marshal(extenderFilterResult); err != nil {
panic(err)
} else {
log.Print("info: ", predicate.Name, " extenderFilterResult = ", string(resultBody))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(resultBody)
}
}
}
func PrioritizeRoute(prioritize Prioritize) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
checkBody(w, r)
var buf bytes.Buffer
body := io.TeeReader(r.Body, &buf)
log.Print("info: ", prioritize.Name, " ExtenderArgs = ", buf.String())
var extenderArgs schedulerapi.ExtenderArgs
var hostPriorityList *schedulerapi.HostPriorityList
if err := json.NewDecoder(body).Decode(&extenderArgs); err != nil {
panic(err)
}
if list, err := prioritize.Handler(extenderArgs); err != nil {
panic(err)
} else {
hostPriorityList = list
}
if resultBody, err := json.Marshal(hostPriorityList); err != nil {
panic(err)
} else {
log.Print("info: ", prioritize.Name, " hostPriorityList = ", string(resultBody))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(resultBody)
}
}
}
func BindRoute(bind Bind) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
checkBody(w, r)
var buf bytes.Buffer
body := io.TeeReader(r.Body, &buf)
log.Print("info: extenderBindingArgs = ", buf.String())
var extenderBindingArgs schedulerapi.ExtenderBindingArgs
var extenderBindingResult *schedulerapi.ExtenderBindingResult
if err := json.NewDecoder(body).Decode(&extenderBindingArgs); err != nil {
extenderBindingResult = &schedulerapi.ExtenderBindingResult{
Error: err.Error(),
}
} else {
extenderBindingResult = bind.Handler(extenderBindingArgs)
}
if resultBody, err := json.Marshal(extenderBindingResult); err != nil {
panic(err)
} else {
log.Print("info: extenderBindingResult = ", string(resultBody))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(resultBody)
}
}
}
func PreemptionRoute(preemption Preemption) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
checkBody(w, r)
var buf bytes.Buffer
body := io.TeeReader(r.Body, &buf)
log.Print("info: extenderPreemptionArgs = ", buf.String())
var extenderPreemptionArgs schedulerapi.ExtenderPreemptionArgs
var extenderPreemptionResult *schedulerapi.ExtenderPreemptionResult
if err := json.NewDecoder(body).Decode(&extenderPreemptionArgs); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
} else {
extenderPreemptionResult = preemption.Handler(extenderPreemptionArgs)
}
if resultBody, err := json.Marshal(extenderPreemptionResult); err != nil {
panic(err)
} else {
log.Print("info: extenderPreemptionResult = ", string(resultBody))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(resultBody)
}
}
}
func VersionRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, fmt.Sprint(version))
}
func AddVersion(router *httprouter.Router) {
router.GET(versionPath, DebugLogging(VersionRoute, versionPath))
}
func DebugLogging(h httprouter.Handle, path string) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
log.Print("debug: ", path, " request body = ", r.Body)
h(w, r, p)
log.Print("debug: ", path, " response=", w)
}
}
func AddPredicate(router *httprouter.Router, predicate Predicate) {
path := predicatesPrefix + "/" + predicate.Name
router.POST(path, DebugLogging(PredicateRoute(predicate), path))
}
func AddPrioritize(router *httprouter.Router, prioritize Prioritize) {
path := prioritiesPrefix + "/" + prioritize.Name
router.POST(path, DebugLogging(PrioritizeRoute(prioritize), path))
}
func AddBind(router *httprouter.Router, bind Bind) {
if handle, _, _ := router.Lookup("POST", bindPath); handle != nil {
log.Print("warning: AddBind was called more then once!")
} else {
router.POST(bindPath, DebugLogging(BindRoute(bind), bindPath))
}
}
func AddPreemption(router *httprouter.Router, preemption Preemption) {
if handle, _, _ := router.Lookup("POST", preemptionPath); handle != nil {
log.Print("warning: AddPreemption was called more then once!")
} else {
router.POST(preemptionPath, DebugLogging(PreemptionRoute(preemption), preemptionPath))
}
}