remote API: fix pod top error reporting

Do not return 200 status code before we know if there will be an error.
Delay writing the status code until we send the first response. That way
we can set an error code inside the loop when we get a error on the
first try, i.e. because an invalid descriptor was used.

Fixes #22986

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2024-06-24 13:40:09 +02:00
parent 29ecf5984c
commit e404976d1b
2 changed files with 15 additions and 13 deletions

View File

@ -411,12 +411,8 @@ func PodTop(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)
wroteContent := false
w.Header().Set("Content-Type", "application/json")
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
encoder := json.NewEncoder(w)
@ -428,11 +424,22 @@ loop: // break out of for/select infinite` loop
default:
output, err := pod.GetPodPidInformation([]string{query.PsArgs})
if err != nil {
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err)
break loop
if !wroteContent {
utils.InternalServerError(w, err)
} else {
// ship has sailed, client already got a 200 response and expects valid
// PodTopOKBody json format so we no longer can send the error.
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err)
}
return
}
if len(output) > 0 {
if !wroteContent {
// Write header only first time around
w.WriteHeader(http.StatusOK)
wroteContent = true
}
body := handlers.PodTopOKBody{}
body.Titles = utils.PSTitles(output[0])
for i := range body.Titles {

View File

@ -85,12 +85,7 @@ var _ = Describe("Podman top", func() {
// the wrong input and still print the -ef output instead.
result := podmanTest.Podman([]string{"pod", "top", podid, "-eo", "invalid"})
result.WaitWithDefaultTimeout()
if IsRemote() {
// FIXME: #22986
Expect(result).Should(ExitWithError(125, "unmarshalling into &handlers.PodTopOKBody{ContainerTopOKBody:container.ContainerTopOKBody"))
} else {
Expect(result).Should(ExitWithError(125, "Error: '-eo': unknown descriptor"))
}
Expect(result).Should(ExitWithError(125, "Error: '-eo': unknown descriptor"))
})
It("podman pod top on pod with containers in same pid namespace", func() {