mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 01:40:07 +00:00
Prevent infinite loop in apiserver errorJSON
This commit is contained in:
parent
7d2a64b2a8
commit
b1b255f90a
@ -19,6 +19,7 @@ package apiserver
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
@ -283,16 +284,14 @@ func APIVersionHandler(versions ...string) restful.RouteFunction {
|
|||||||
func writeJSON(statusCode int, codec runtime.Codec, object runtime.Object, w http.ResponseWriter) {
|
func writeJSON(statusCode int, codec runtime.Codec, object runtime.Object, w http.ResponseWriter) {
|
||||||
output, err := codec.Encode(object)
|
output, err := codec.Encode(object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Note: If codec is broken, this results in an infinite recursion
|
errorJSONFatal(err, codec, w)
|
||||||
errorJSON(err, codec, w)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// PR #2243: Pretty-print JSON by default.
|
// PR #2243: Pretty-print JSON by default.
|
||||||
formatted := &bytes.Buffer{}
|
formatted := &bytes.Buffer{}
|
||||||
err = json.Indent(formatted, output, "", " ")
|
err = json.Indent(formatted, output, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Note: If codec is broken, this results in an infinite recursion
|
errorJSONFatal(err, codec, w)
|
||||||
errorJSON(err, codec, w)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
@ -306,6 +305,20 @@ func errorJSON(err error, codec runtime.Codec, w http.ResponseWriter) {
|
|||||||
writeJSON(status.Code, codec, status, w)
|
writeJSON(status.Code, codec, status, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// errorJSONFatal renders an error to the response, and if codec fails will render plaintext
|
||||||
|
func errorJSONFatal(err error, codec runtime.Codec, w http.ResponseWriter) {
|
||||||
|
status := errToAPIStatus(err)
|
||||||
|
output, err := codec.Encode(status)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(status.Code)
|
||||||
|
fmt.Fprintf(w, "%s: %s", status.Reason, status.Message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status.Code)
|
||||||
|
w.Write(output)
|
||||||
|
}
|
||||||
|
|
||||||
// writeRawJSON writes a non-API object in JSON.
|
// writeRawJSON writes a non-API object in JSON.
|
||||||
func writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
|
func writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
|
||||||
// PR #2243: Pretty-print JSON by default.
|
// PR #2243: Pretty-print JSON by default.
|
||||||
|
Loading…
Reference in New Issue
Block a user