@@ -11,12 +11,20 @@ import (
1111 "k8s.io/klog/v2"
1212)
1313
14+ const (
15+ l2 = 2
16+ readTimeout = 5
17+ writeTimeout = 10
18+ maxHeader = 1000
19+ )
20+
1421// postOnly check if the method type is POST.
1522func postOnly (next http.HandlerFunc ) http.HandlerFunc {
1623 return func (w http.ResponseWriter , r * http.Request ) {
1724 if r .Method != http .MethodPost {
1825 w .WriteHeader (http .StatusMethodNotAllowed )
19- klog .V (2 ).InfoS ("method Type not POST" , "component" , "extender" )
26+ klog .V (l2 ).InfoS ("method Type not POST" , "component" , "extender" )
27+
2028 return
2129 }
2230
@@ -29,7 +37,8 @@ func contentLength(next http.HandlerFunc) http.HandlerFunc {
2937 return func (w http.ResponseWriter , r * http.Request ) {
3038 if r .ContentLength > 1 * 1000 * 1000 * 1000 {
3139 w .WriteHeader (http .StatusInternalServerError )
32- klog .V (2 ).InfoS ("request size too large" , "component" , "extender" )
40+ klog .V (l2 ).InfoS ("request size too large" , "component" , "extender" )
41+
3342 return
3443 }
3544
@@ -43,7 +52,8 @@ func requestContentType(next http.HandlerFunc) http.HandlerFunc {
4352 requestContentType := r .Header .Get ("Content-Type" )
4453 if requestContentType != "application/json" {
4554 w .WriteHeader (http .StatusNotFound )
46- klog .V (2 ).InfoS ("request content type not application/json" , "component" , "extender" )
55+ klog .V (l2 ).InfoS ("request content type not application/json" , "component" , "extender" )
56+
4757 return
4858 }
4959
@@ -76,7 +86,7 @@ func handlerWithMiddleware(handle http.HandlerFunc) http.HandlerFunc {
7686
7787// error handler deals with requests sent to an invalid endpoint and returns a 404.
7888func errorHandler (w http.ResponseWriter , r * http.Request ) {
79- klog .V (2 ).InfoS ("Requested resource: '" + r .URL .Path + "' not found" , "component" , "extender" )
89+ klog .V (l2 ).InfoS ("Requested resource: '" + r .URL .Path + "' not found" , "component" , "extender" )
8090 w .Header ().Add ("Content-Type" , "application/json" )
8191 w .WriteHeader (http .StatusNotFound )
8292}
@@ -89,29 +99,34 @@ func (m Server) StartServer(port string, certFile string, keyFile string, caFile
8999 mx .HandleFunc ("/scheduler/prioritize" , handlerWithMiddleware (m .Prioritize ))
90100 mx .HandleFunc ("/scheduler/filter" , handlerWithMiddleware (m .Filter ))
91101 mx .HandleFunc ("/scheduler/bind" , handlerWithMiddleware (m .Bind ))
102+
92103 var err error
104+
93105 if unsafe {
94- klog .V (2 ).InfoS ("Extender Listening on HTTP " + port , "component" , "extender" )
106+ klog .V (l2 ).InfoS ("Extender Listening on HTTP " + port , "component" , "extender" )
107+
95108 err = http .ListenAndServe (":" + port , mx )
96109 if err != nil {
97- klog .V (2 ).InfoS ("Listening on HTTP failed: " + err .Error (), "component" , "extender" )
110+ klog .V (l2 ).InfoS ("Listening on HTTP failed: " + err .Error (), "component" , "extender" )
98111 }
99112 } else {
100113 srv := configureSecureServer (port , caFile )
101114 srv .Handler = mx
102- klog .V (2 ).InfoS ("Extender Listening on HTTPS " + port , "component" , "extender" )
115+ klog .V (l2 ).InfoS ("Extender Listening on HTTPS " + port , "component" , "extender" )
103116
104117 klog .Fatal (srv .ListenAndServeTLS (certFile , keyFile ))
105118 }
106- klog .V (2 ).InfoS ("Scheduler extender server failed to start " + err .Error (), "component" , "extender" )
119+
120+ klog .V (l2 ).InfoS ("Scheduler extender server failed to start " + err .Error (), "component" , "extender" )
107121}
108122
109123// Configuration values including algorithms etc for the TAS scheduling endpoint.
110124func configureSecureServer (port string , caFile string ) * http.Server {
111125 caCert , err := ioutil .ReadFile (caFile )
112126 if err != nil {
113- klog .V (2 ).InfoS ("caCert read failed: " + err .Error (), "component" , "extender" )
127+ klog .V (l2 ).InfoS ("caCert read failed: " + err .Error (), "component" , "extender" )
114128 }
129+
115130 caCertPool := x509 .NewCertPool ()
116131 caCertPool .AppendCertsFromPEM (caCert )
117132
@@ -133,11 +148,12 @@ func configureSecureServer(port string, caFile string) *http.Server {
133148 srv := & http.Server {
134149 Addr : ":" + port ,
135150 Handler : nil ,
136- ReadHeaderTimeout : 5 * time .Second ,
137- WriteTimeout : 10 * time .Second ,
138- MaxHeaderBytes : 1000 ,
151+ ReadHeaderTimeout : readTimeout * time .Second ,
152+ WriteTimeout : writeTimeout * time .Second ,
153+ MaxHeaderBytes : maxHeader ,
139154 TLSConfig : cfg ,
140155 TLSNextProto : make (map [string ]func (* http.Server , * tls.Conn , http.Handler )),
141156 }
157+
142158 return srv
143159}
0 commit comments