mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Simplify helpers
This commit is contained in:
parent
c7d311896e
commit
a2c86e4c97
@ -169,7 +169,7 @@ func (s *APIServer) write(statusCode int, object interface{}, w http.ResponseWri
|
|||||||
func (s *APIServer) writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
|
func (s *APIServer) writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
|
||||||
output, err := json.Marshal(object)
|
output, err := json.Marshal(object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
@ -177,16 +177,6 @@ func (s *APIServer) writeRawJSON(statusCode int, object interface{}, w http.Resp
|
|||||||
w.Write(output)
|
w.Write(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIServer) error(err error, w http.ResponseWriter) {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
fmt.Fprintf(w, "Internal Error: %#v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *APIServer) readBody(req *http.Request) ([]byte, error) {
|
|
||||||
defer req.Body.Close()
|
|
||||||
return ioutil.ReadAll(req.Body)
|
|
||||||
}
|
|
||||||
|
|
||||||
// finishReq finishes up a request, waiting until the operation finishes or, after a timeout, creating an
|
// finishReq finishes up a request, waiting until the operation finishes or, after a timeout, creating an
|
||||||
// Operation to receive the result and returning its ID down the writer.
|
// Operation to receive the result and returning its ID down the writer.
|
||||||
func (s *APIServer) finishReq(out <-chan interface{}, sync bool, timeout time.Duration, w http.ResponseWriter) {
|
func (s *APIServer) finishReq(out <-chan interface{}, sync bool, timeout time.Duration, w http.ResponseWriter) {
|
||||||
@ -247,12 +237,12 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
case 1:
|
case 1:
|
||||||
selector, err := labels.ParseSelector(req.URL.Query().Get("labels"))
|
selector, err := labels.ParseSelector(req.URL.Query().Get("labels"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
list, err := storage.List(selector)
|
list, err := storage.List(selector)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.write(http.StatusOK, list, w)
|
s.write(http.StatusOK, list, w)
|
||||||
@ -263,7 +253,7 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.write(http.StatusOK, item, w)
|
s.write(http.StatusOK, item, w)
|
||||||
@ -275,9 +265,9 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
notFound(w, req)
|
notFound(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
body, err := s.readBody(req)
|
body, err := readBody(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
obj, err := storage.Extract(body)
|
obj, err := storage.Extract(body)
|
||||||
@ -286,7 +276,7 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
out, err := storage.Create(obj)
|
out, err := storage.Create(obj)
|
||||||
@ -295,7 +285,7 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.finishReq(out, sync, timeout, w)
|
s.finishReq(out, sync, timeout, w)
|
||||||
@ -310,7 +300,7 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.finishReq(out, sync, timeout, w)
|
s.finishReq(out, sync, timeout, w)
|
||||||
@ -319,9 +309,9 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
notFound(w, req)
|
notFound(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
body, err := s.readBody(req)
|
body, err := readBody(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
obj, err := storage.Extract(body)
|
obj, err := storage.Extract(body)
|
||||||
@ -330,7 +320,7 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
out, err := storage.Update(obj)
|
out, err := storage.Update(obj)
|
||||||
@ -339,7 +329,7 @@ func (s *APIServer) handleREST(parts []string, req *http.Request, w http.Respons
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.finishReq(out, sync, timeout, w)
|
s.finishReq(out, sync, timeout, w)
|
||||||
@ -408,7 +398,7 @@ func (s *APIServer) handleWatch(w http.ResponseWriter, req *http.Request) {
|
|||||||
watching, err = watcher.WatchAll()
|
watching, err = watcher.WatchAll()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err, w)
|
internalError(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,3 +415,8 @@ func (s *APIServer) handleWatch(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
notFound(w, req)
|
notFound(w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readBody(req *http.Request) ([]byte, error) {
|
||||||
|
defer req.Body.Close()
|
||||||
|
return ioutil.ReadAll(req.Body)
|
||||||
|
}
|
||||||
|
@ -21,6 +21,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func internalError(err error, w http.ResponseWriter) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintf(w, "Internal Error: %#v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// notFound renders a simple not found error
|
// notFound renders a simple not found error
|
||||||
func notFound(w http.ResponseWriter, req *http.Request) {
|
func notFound(w http.ResponseWriter, req *http.Request) {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
Loading…
Reference in New Issue
Block a user