Merge pull request #18983 from Luap99/api-top

pkg/api: top return error to client
This commit is contained in:
OpenShift Merge Robot 2023-06-26 08:12:29 +02:00 committed by GitHub
commit 1bca2d6a1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 8 deletions

View File

@ -48,12 +48,8 @@ func TopContainer(w http.ResponseWriter, r *http.Request) {
return
}
// We are committed now - all errors logged but not reported to client, ship has sailed
w.WriteHeader(http.StatusOK)
statusWritten := false
w.Header().Set("Content-Type", "application/json")
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
encoder := json.NewEncoder(w)
@ -65,7 +61,11 @@ loop: // break out of for/select infinite` loop
default:
output, err := c.Top(strings.Split(query.PsArgs, ","))
if err != nil {
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err)
if !statusWritten {
utils.InternalServerError(w, err)
} else {
logrus.Errorf("From %s %q : %v", r.Method, r.URL, err)
}
break loop
}
@ -86,9 +86,15 @@ loop: // break out of for/select infinite` loop
}
if err := encoder.Encode(body); err != nil {
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err)
if !statusWritten {
utils.InternalServerError(w, err)
} else {
logrus.Errorf("From %s %q : %v", r.Method, r.URL, err)
}
break loop
}
// after the first write we can no longer send a different status code
statusWritten = true
if f, ok := w.(http.Flusher); ok {
f.Flush()
}

View File

@ -56,7 +56,9 @@ func (s *APIServer) apiWrapper(h http.HandlerFunc, w http.ResponseWriter, r *htt
}
if buffer {
w = newBufferedResponseWriter(w)
bw := newBufferedResponseWriter(w)
defer bw.b.Flush()
w = bw
}
h(w, r)

View File

@ -132,6 +132,15 @@ if root; then
podman rm -f $CTRNAME
fi
CTRNAME=test123
podman run --name $CTRNAME -d $IMAGE top
t GET libpod/containers/$CTRNAME/top?ps_args=--invalid 500 \
.cause~".*unrecognized option.*"
t GET containers/$CTRNAME/top?ps_args=--invalid 500 \
.cause~".*unrecognized option.*"
podman rm -f $CTRNAME
# Issue #15765: make sure the memory limit is capped
if root; then
CTRNAME=ctr-with-limit