@@ -19,11 +19,11 @@ package routes
1919import (
2020 "bytes"
2121 "encoding/json"
22- "fmt"
2322 "io"
2423 "net/http"
2524
2625 "github.com/julienschmidt/httprouter"
26+ "github.com/unrolled/render"
2727 "k8s.io/apimachinery/pkg/types"
2828 "k8s.io/klog/v2"
2929 extenderv1 "k8s.io/kube-scheduler/extender/v1"
@@ -65,15 +65,12 @@ func PredicateRoute(s *scheduler.Scheduler) httprouter.Handle {
6565 }
6666 }
6767
68- if resultBody , err := json .Marshal (extenderFilterResult ); err != nil {
69- klog .ErrorS (err , "Failed to marshal extender filter result" , "result" , extenderFilterResult )
70- w .Header ().Set ("Content-Type" , "application/json" )
71- w .WriteHeader (http .StatusInternalServerError )
72- w .Write ([]byte (err .Error ()))
73- } else {
74- w .Header ().Set ("Content-Type" , "application/json" )
75- w .WriteHeader (http .StatusOK )
76- w .Write (resultBody )
68+ klog .V (5 ).InfoS ("Returning predicate response" , "result" , extenderFilterResult )
69+
70+ err := render .New (render.Options {IndentJSON : true }).JSON (w , http .StatusOK , extenderFilterResult )
71+ if err != nil {
72+ klog .ErrorS (err , "Failed to write JSON response" )
73+ return
7774 }
7875 }
7976}
@@ -101,17 +98,12 @@ func Bind(s *scheduler.Scheduler) httprouter.Handle {
10198 }
10299 }
103100
104- if response , err := json .Marshal (extenderBindingResult ); err != nil {
105- klog .ErrorS (err , "Failed to marshal binding result" , "result" , extenderBindingResult )
106- w .Header ().Set ("Content-Type" , "application/json" )
107- w .WriteHeader (http .StatusInternalServerError )
108- errMsg := fmt .Sprintf ("{'error':'%s'}" , err .Error ())
109- w .Write ([]byte (errMsg ))
110- } else {
111- klog .V (5 ).InfoS ("Returning bind response" , "result" , extenderBindingResult )
112- w .Header ().Set ("Content-Type" , "application/json" )
113- w .WriteHeader (http .StatusOK )
114- w .Write (response )
101+ klog .V (5 ).InfoS ("Returning bind response" , "result" , extenderBindingResult )
102+
103+ err := render .New (render.Options {IndentJSON : true }).JSON (w , http .StatusOK , extenderBindingResult )
104+ if err != nil {
105+ klog .ErrorS (err , "Failed to write JSON response" )
106+ return
115107 }
116108 }
117109}
0 commit comments