-
Notifications
You must be signed in to change notification settings - Fork 44
/
scheduler.go
170 lines (142 loc) · 5.08 KB
/
scheduler.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
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
// Package extender contains types and logic to respond to requests from a Kubernetes http scheduler extender.
package extender
import (
"crypto/tls"
"crypto/x509"
"net/http"
"os"
"time"
"k8s.io/klog/v2"
)
const (
l2 = 2
readTimeout = 5
writeTimeout = 10
maxHeader = 1000
)
// postOnly check if the method type is POST.
func postOnly(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
klog.V(l2).InfoS("method Type not POST", "component", "extender")
return
}
next.ServeHTTP(w, r)
}
}
// contentLength check the if the request size is adequate.
func contentLength(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.ContentLength > 1*1000*1000*1000 {
w.WriteHeader(http.StatusInternalServerError)
klog.V(l2).InfoS("request size too large", "component", "extender")
return
}
next.ServeHTTP(w, r)
}
}
// requestContentType verify the content type of the request.
func requestContentType(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
requestContentType := r.Header.Get("Content-Type")
if requestContentType != "application/json" {
w.WriteHeader(http.StatusNotFound)
klog.V(l2).InfoS("request content type not application/json", "component", "extender")
return
}
next.ServeHTTP(w, r)
}
}
/*
handlerWithMiddleware runs each function in sequence starting from the outermost function. These middleware functions
are pass/fail checks on the scheduling request. If a check fails the response with an appropriate error is immediately
written and returned.
i.e. with this version of the code:
return requestContentType(
contentLength(
postOnly(handle),
),
)
if the content type is not correct - i.e. NOT application/json - the response will be written. contentLength or postOnly
will not run.
*/
// handlerWithMiddleware is handler wrapped with middleware to serve the prechecks at endpoint.
func handlerWithMiddleware(handle http.HandlerFunc) http.HandlerFunc {
return requestContentType(
contentLength(
postOnly(handle),
),
)
}
// error handler deals with requests sent to an invalid endpoint and returns a 404.
func errorHandler(w http.ResponseWriter, r *http.Request) {
klog.V(l2).InfoS("Requested resource: '"+r.URL.Path+"' not found", "component", "extender")
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
}
// StartServer starts the HTTP server needed for the scheduler extender.
// It registers the handlers and checks for existing telemetry policies.
func (m Server) StartServer(port string, certFile string, keyFile string, caFile string, unsafe bool) {
mx := http.NewServeMux()
mx.HandleFunc("/", handlerWithMiddleware(errorHandler))
mx.HandleFunc("/scheduler/prioritize", handlerWithMiddleware(m.Prioritize))
mx.HandleFunc("/scheduler/filter", handlerWithMiddleware(m.Filter))
mx.HandleFunc("/scheduler/bind", handlerWithMiddleware(m.Bind))
var err error
if unsafe {
klog.V(l2).InfoS("Extender Listening on HTTP "+port, "component", "extender")
srv := &http.Server{
Addr: ":" + port,
Handler: mx,
ReadHeaderTimeout: readTimeout * time.Second,
WriteTimeout: writeTimeout * time.Second,
MaxHeaderBytes: maxHeader,
}
err = srv.ListenAndServe()
if err != nil {
klog.V(l2).InfoS("Listening on HTTP failed: "+err.Error(), "component", "extender")
}
} else {
srv := configureSecureServer(port, caFile)
srv.Handler = mx
klog.V(l2).InfoS("Extender Listening on HTTPS "+port, "component", "extender")
klog.Fatal(srv.ListenAndServeTLS(certFile, keyFile))
}
klog.V(l2).InfoS("Scheduler extender server failed to start "+err.Error(), "component", "extender")
}
// Configuration values including algorithms etc for the TAS scheduling endpoint.
func configureSecureServer(port string, caFile string) *http.Server {
caCert, err := os.ReadFile(caFile)
if err != nil {
klog.V(l2).InfoS("caCert read failed: "+err.Error(), "component", "extender")
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
PreferServerCipherSuites: true,
InsecureSkipVerify: false,
CipherSuites: []uint16{
// tls 1.2
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
// tls 1.3 configuration not supported
},
}
srv := &http.Server{
Addr: ":" + port,
Handler: nil,
ReadHeaderTimeout: readTimeout * time.Second,
WriteTimeout: writeTimeout * time.Second,
MaxHeaderBytes: maxHeader,
TLSConfig: cfg,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
return srv
}