-
Notifications
You must be signed in to change notification settings - Fork 48
/
routers.go
62 lines (54 loc) · 1.65 KB
/
routers.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
schedulerapi "k8s.io/kube-scheduler/extender/v1"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome to sample-scheduler-extender!\n")
}
func Filter(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var buf bytes.Buffer
body := io.TeeReader(r.Body, &buf)
var extenderArgs schedulerapi.ExtenderArgs
var extenderFilterResult *schedulerapi.ExtenderFilterResult
if err := json.NewDecoder(body).Decode(&extenderArgs); err != nil {
extenderFilterResult = &schedulerapi.ExtenderFilterResult{
Error: err.Error(),
}
} else {
extenderFilterResult = filter(extenderArgs)
}
if response, err := json.Marshal(extenderFilterResult); err != nil {
log.Fatalln(err)
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(response)
}
}
func Prioritize(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var buf bytes.Buffer
body := io.TeeReader(r.Body, &buf)
var extenderArgs schedulerapi.ExtenderArgs
var hostPriorityList *schedulerapi.HostPriorityList
if err := json.NewDecoder(body).Decode(&extenderArgs); err != nil {
log.Println(err)
hostPriorityList = &schedulerapi.HostPriorityList{}
} else {
hostPriorityList = prioritize(extenderArgs)
}
if response, err := json.Marshal(hostPriorityList); err != nil {
log.Fatalln(err)
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(response)
}
}
// func Bind(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {}